Allow creation of cache classes associated with a context
This will allow heat.object implementations to create a single object per context to manage its particular context caching needs. Change-Id: I9b626efee45164617a73b790bfad4808172d2c12 Related-Bug: #1578854
This commit is contained in:
parent
c62e1b325a
commit
5da09eae94
|
@ -111,6 +111,17 @@ class RequestContext(context.RequestContext):
|
|||
else:
|
||||
self.is_admin = is_admin
|
||||
|
||||
# context scoped cache dict where the key is a class of the type of
|
||||
# object being cached and the value is the cache implementation class
|
||||
self._object_cache = {}
|
||||
|
||||
def cache(self, cache_cls):
|
||||
cache = self._object_cache.get(cache_cls)
|
||||
if not cache:
|
||||
cache = cache_cls()
|
||||
self._object_cache[cache_cls] = cache
|
||||
return cache
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
if self._session is None:
|
||||
|
|
|
@ -206,6 +206,29 @@ class TestRequestContext(common.HeatTestCase):
|
|||
auth_url='http://abc/v3',
|
||||
trust_id=None)
|
||||
|
||||
def test_cache(self):
|
||||
ctx = context.RequestContext.from_dict(self.ctx)
|
||||
|
||||
class Class1(object):
|
||||
pass
|
||||
|
||||
class Class2(object):
|
||||
pass
|
||||
|
||||
self.assertEqual(0, len(ctx._object_cache))
|
||||
|
||||
cache1 = ctx.cache(Class1)
|
||||
self.assertIsInstance(cache1, Class1)
|
||||
self.assertEqual(1, len(ctx._object_cache))
|
||||
|
||||
cache1a = ctx.cache(Class1)
|
||||
self.assertEqual(cache1, cache1a)
|
||||
self.assertEqual(1, len(ctx._object_cache))
|
||||
|
||||
cache2 = ctx.cache(Class2)
|
||||
self.assertIsInstance(cache2, Class2)
|
||||
self.assertEqual(2, len(ctx._object_cache))
|
||||
|
||||
|
||||
class RequestContextMiddlewareTest(common.HeatTestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue