Merge "cache_utils: fixed cache misses for the new (oslo.cache) configuration"

This commit is contained in:
Jenkins 2016-07-14 11:39:39 +00:00 committed by Gerrit Code Review
commit 4f36d4e3fe
3 changed files with 38 additions and 11 deletions

View File

@ -65,7 +65,6 @@ def _get_cache_region_for_legacy(url):
backend = parsed.scheme backend = parsed.scheme
if backend == 'memory': if backend == 'memory':
backend = 'oslo_cache.dict'
query = parsed.query query = parsed.query
# NOTE(fangzhen): The following NOTE and code is from legacy # NOTE(fangzhen): The following NOTE and code is from legacy
# oslo-incubator cache module. Previously reside in neutron at # oslo-incubator cache module. Previously reside in neutron at
@ -78,11 +77,17 @@ def _get_cache_region_for_legacy(url):
if not query and '?' in parsed.path: if not query and '?' in parsed.path:
query = parsed.path.split('?', 1)[-1] query = parsed.path.split('?', 1)[-1]
parameters = parse.parse_qs(query) parameters = parse.parse_qs(query)
expiration_time = int(parameters.get('default_ttl', [0])[0])
region = cache.create_region() conf = cfg.ConfigOpts()
region.configure(backend, expiration_time=expiration_time) register_oslo_configs(conf)
return region cache_conf_dict = {
'enabled': True,
'backend': 'oslo_cache.dict',
'expiration_time': int(parameters.get('default_ttl', [0])[0]),
}
for k, v in cache_conf_dict.items():
conf.set_override(k, v, group='cache')
return _get_cache_region(conf)
else: else:
raise RuntimeError(_('Old style configuration can use only memory ' raise RuntimeError(_('Old style configuration can use only memory '
'(dict) backend')) '(dict) backend'))
@ -108,6 +113,8 @@ class cache_method_results(object):
key = (func_name,) + args key = (func_name,) + args
if kwargs: if kwargs:
key += utils.dict2tuple(kwargs) key += utils.dict2tuple(kwargs)
# oslo.cache expects a string or a buffer
key = str(key)
try: try:
item = target_self._cache.get(key) item = target_self._cache.get(key)
except TypeError: except TypeError:

View File

@ -51,6 +51,16 @@ class CacheConfFixture(ConfFixture):
self.config(cache_url='memory://?default_ttl=5') self.config(cache_url='memory://?default_ttl=5')
class NewCacheConfFixture(ConfFixture):
def setUp(self):
super(NewCacheConfFixture, self).setUp()
self.config(
group='cache',
enabled=True,
backend='oslo_cache.dict',
expiration_time=5)
class TestMetadataProxyHandlerBase(base.BaseTestCase): class TestMetadataProxyHandlerBase(base.BaseTestCase):
fake_conf = cfg.CONF fake_conf = cfg.CONF
fake_conf_fixture = ConfFixture(fake_conf) fake_conf_fixture = ConfFixture(fake_conf)
@ -96,9 +106,7 @@ class TestMetadataProxyHandlerRpc(TestMetadataProxyHandlerBase):
self.assertEqual(expected, ports) self.assertEqual(expected, ports)
class TestMetadataProxyHandlerCache(TestMetadataProxyHandlerBase): class _TestMetadataProxyHandlerCacheMixin(object):
fake_conf = cfg.CONF
fake_conf_fixture = CacheConfFixture(fake_conf)
def test_call(self): def test_call(self):
req = mock.Mock() req = mock.Mock()
@ -411,6 +419,18 @@ class TestMetadataProxyHandlerCache(TestMetadataProxyHandlerBase):
) )
class TestMetadataProxyHandlerCache(TestMetadataProxyHandlerBase,
_TestMetadataProxyHandlerCacheMixin):
fake_conf = cfg.CONF
fake_conf_fixture = CacheConfFixture(fake_conf)
class TestMetadataProxyHandlerNewCache(TestMetadataProxyHandlerBase,
_TestMetadataProxyHandlerCacheMixin):
fake_conf = cfg.CONF
fake_conf_fixture = NewCacheConfFixture(fake_conf)
class TestMetadataProxyHandlerNoCache(TestMetadataProxyHandlerCache): class TestMetadataProxyHandlerNoCache(TestMetadataProxyHandlerCache):
fake_conf = cfg.CONF fake_conf = cfg.CONF
fake_conf_fixture = ConfFixture(fake_conf) fake_conf_fixture = ConfFixture(fake_conf)

View File

@ -100,7 +100,7 @@ class TestCachingDecorator(base.BaseTestCase):
self.decor._cache.get.return_value = self.not_cached self.decor._cache.get.return_value = self.not_cached
retval = self.decor.func(*args, **kwargs) retval = self.decor.func(*args, **kwargs)
self.decor._cache.set.assert_called_once_with( self.decor._cache.set.assert_called_once_with(
expected_key, self.decor.func_retval) str(expected_key), self.decor.func_retval)
self.assertEqual(self.decor.func_retval, retval) self.assertEqual(self.decor.func_retval, retval)
def test_cache_hit(self): def test_cache_hit(self):
@ -110,7 +110,7 @@ class TestCachingDecorator(base.BaseTestCase):
retval = self.decor.func(*args, **kwargs) retval = self.decor.func(*args, **kwargs)
self.assertFalse(self.decor._cache.set.called) self.assertFalse(self.decor._cache.set.called)
self.assertEqual(self.decor._cache.get.return_value, retval) self.assertEqual(self.decor._cache.get.return_value, retval)
self.decor._cache.get.assert_called_once_with(expected_key) self.decor._cache.get.assert_called_once_with(str(expected_key))
def test_get_unhashable(self): def test_get_unhashable(self):
expected_key = (self.func_name, [1], 2) expected_key = (self.func_name, [1], 2)
@ -118,7 +118,7 @@ class TestCachingDecorator(base.BaseTestCase):
retval = self.decor.func([1], 2) retval = self.decor.func([1], 2)
self.assertFalse(self.decor._cache.set.called) self.assertFalse(self.decor._cache.set.called)
self.assertEqual(self.decor.func_retval, retval) self.assertEqual(self.decor.func_retval, retval)
self.decor._cache.get.assert_called_once_with(expected_key) self.decor._cache.get.assert_called_once_with(str(expected_key))
def test_missing_cache(self): def test_missing_cache(self):
delattr(self.decor, '_cache') delattr(self.decor, '_cache')