Commit dced13a8 by Nimisha Asthagiri

Merge pull request #12397 from edx/tnl/block-transformer-memcache

Set infinite timeout for Block Structure cache entries
parents e8d2a626 68173b6d
...@@ -41,13 +41,19 @@ class BlockStructureCache(object): ...@@ -41,13 +41,19 @@ class BlockStructureCache(object):
data_to_cache = ( data_to_cache = (
block_structure._block_relations, block_structure._block_relations,
block_structure._transformer_data, block_structure._transformer_data,
block_structure._block_data_map block_structure._block_data_map,
) )
zp_data_to_cache = zpickle(data_to_cache) zp_data_to_cache = zpickle(data_to_cache)
# Set the timeout value for the cache to None. This caches the
# value forever. The expectation is that the caller will delete
# the cached value once it is outdated.
self._cache.set( self._cache.set(
self._encode_root_cache_key(block_structure.root_block_usage_key), self._encode_root_cache_key(block_structure.root_block_usage_key),
zp_data_to_cache zp_data_to_cache,
timeout=None,
) )
logger.info( logger.info(
"Wrote BlockStructure %s to cache, size: %s", "Wrote BlockStructure %s to cache, size: %s",
block_structure.root_block_usage_key, block_structure.root_block_usage_key,
......
...@@ -78,13 +78,15 @@ class MockCache(object): ...@@ -78,13 +78,15 @@ class MockCache(object):
# An in-memory map of cache keys to cache values. # An in-memory map of cache keys to cache values.
self.map = {} self.map = {}
self.set_call_count = 0 self.set_call_count = 0
self.timeout_from_last_call = 0
def set(self, key, val): def set(self, key, val, timeout):
""" """
Associates the given key with the given value in the cache. Associates the given key with the given value in the cache.
""" """
self.set_call_count += 1 self.set_call_count += 1
self.map[key] = val self.map[key] = val
self.timeout_from_last_call = timeout
def get(self, key, default=None): def get(self, key, default=None):
""" """
......
...@@ -11,13 +11,14 @@ from .helpers import ChildrenMapTestMixin, MockCache, MockTransformer ...@@ -11,13 +11,14 @@ from .helpers import ChildrenMapTestMixin, MockCache, MockTransformer
@attr('shard_2') @attr('shard_2')
class TestBlockStructureCache(ChildrenMapTestMixin, TestCase): class TestBlockStructureCache(ChildrenMapTestMixin, TestCase):
""" """
Tests for BlockStructureFactory Tests for BlockStructureCache
""" """
def setUp(self): def setUp(self):
super(TestBlockStructureCache, self).setUp() super(TestBlockStructureCache, self).setUp()
self.children_map = self.SIMPLE_CHILDREN_MAP self.children_map = self.SIMPLE_CHILDREN_MAP
self.block_structure = self.create_block_structure(self.children_map) self.block_structure = self.create_block_structure(self.children_map)
self.cache = BlockStructureCache(MockCache()) self.mock_cache = MockCache()
self.block_structure_cache = BlockStructureCache(self.mock_cache)
def add_transformers(self): def add_transformers(self):
""" """
...@@ -30,22 +31,26 @@ class TestBlockStructureCache(ChildrenMapTestMixin, TestCase): ...@@ -30,22 +31,26 @@ class TestBlockStructureCache(ChildrenMapTestMixin, TestCase):
usage_key=0, transformer=transformer, key='test', value='{} val'.format(transformer.name()) usage_key=0, transformer=transformer, key='test', value='{} val'.format(transformer.name())
) )
def test_add(self): def test_add_and_get(self):
self.assertEquals(self.mock_cache.timeout_from_last_call, 0)
self.add_transformers() self.add_transformers()
self.cache.add(self.block_structure) self.block_structure_cache.add(self.block_structure)
cached_value = self.cache.get(self.block_structure.root_block_usage_key) self.assertEquals(self.mock_cache.timeout_from_last_call, None)
cached_value = self.block_structure_cache.get(self.block_structure.root_block_usage_key)
self.assertIsNotNone(cached_value) self.assertIsNotNone(cached_value)
self.assert_block_structure(cached_value, self.children_map) self.assert_block_structure(cached_value, self.children_map)
def test_get_none(self): def test_get_none(self):
self.assertIsNone( self.assertIsNone(
self.cache.get(self.block_structure.root_block_usage_key) self.block_structure_cache.get(self.block_structure.root_block_usage_key)
) )
def test_delete(self): def test_delete(self):
self.add_transformers() self.add_transformers()
self.cache.add(self.block_structure) self.block_structure_cache.add(self.block_structure)
self.cache.delete(self.block_structure.root_block_usage_key) self.block_structure_cache.delete(self.block_structure.root_block_usage_key)
self.assertIsNone( self.assertIsNone(
self.cache.get(self.block_structure.root_block_usage_key) self.block_structure_cache.get(self.block_structure.root_block_usage_key)
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment