Merge pull request #12397 from edx/tnl/block-transformer-memcache
Set infinite timeout for Block Structure cache entries
This commit is contained in:
@@ -41,13 +41,19 @@ class BlockStructureCache(object):
|
||||
data_to_cache = (
|
||||
block_structure._block_relations,
|
||||
block_structure._transformer_data,
|
||||
block_structure._block_data_map
|
||||
block_structure._block_data_map,
|
||||
)
|
||||
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._encode_root_cache_key(block_structure.root_block_usage_key),
|
||||
zp_data_to_cache
|
||||
zp_data_to_cache,
|
||||
timeout=None,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Wrote BlockStructure %s to cache, size: %s",
|
||||
block_structure.root_block_usage_key,
|
||||
|
||||
@@ -78,13 +78,15 @@ class MockCache(object):
|
||||
# An in-memory map of cache keys to cache values.
|
||||
self.map = {}
|
||||
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.
|
||||
"""
|
||||
self.set_call_count += 1
|
||||
self.map[key] = val
|
||||
self.timeout_from_last_call = timeout
|
||||
|
||||
def get(self, key, default=None):
|
||||
"""
|
||||
|
||||
@@ -11,13 +11,14 @@ from .helpers import ChildrenMapTestMixin, MockCache, MockTransformer
|
||||
@attr('shard_2')
|
||||
class TestBlockStructureCache(ChildrenMapTestMixin, TestCase):
|
||||
"""
|
||||
Tests for BlockStructureFactory
|
||||
Tests for BlockStructureCache
|
||||
"""
|
||||
def setUp(self):
|
||||
super(TestBlockStructureCache, self).setUp()
|
||||
self.children_map = self.SIMPLE_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):
|
||||
"""
|
||||
@@ -30,22 +31,26 @@ class TestBlockStructureCache(ChildrenMapTestMixin, TestCase):
|
||||
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.cache.add(self.block_structure)
|
||||
cached_value = self.cache.get(self.block_structure.root_block_usage_key)
|
||||
self.block_structure_cache.add(self.block_structure)
|
||||
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.assert_block_structure(cached_value, self.children_map)
|
||||
|
||||
def test_get_none(self):
|
||||
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):
|
||||
self.add_transformers()
|
||||
self.cache.add(self.block_structure)
|
||||
self.cache.delete(self.block_structure.root_block_usage_key)
|
||||
self.block_structure_cache.add(self.block_structure)
|
||||
self.block_structure_cache.delete(self.block_structure.root_block_usage_key)
|
||||
self.assertIsNone(
|
||||
self.cache.get(self.block_structure.root_block_usage_key)
|
||||
self.block_structure_cache.get(self.block_structure.root_block_usage_key)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user