From 68173b6dceeff273dc972ed7a4a56208cbdcc369 Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Fri, 6 May 2016 16:49:02 -0400 Subject: [PATCH] Set infinite timeout for Block Structure cache entries --- openedx/core/lib/block_structure/cache.py | 10 ++++++-- .../core/lib/block_structure/tests/helpers.py | 4 +++- .../lib/block_structure/tests/test_cache.py | 23 +++++++++++-------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/openedx/core/lib/block_structure/cache.py b/openedx/core/lib/block_structure/cache.py index 87cdfc4287..fda6e04fc0 100644 --- a/openedx/core/lib/block_structure/cache.py +++ b/openedx/core/lib/block_structure/cache.py @@ -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, diff --git a/openedx/core/lib/block_structure/tests/helpers.py b/openedx/core/lib/block_structure/tests/helpers.py index 3d232ca50e..e692c836f5 100644 --- a/openedx/core/lib/block_structure/tests/helpers.py +++ b/openedx/core/lib/block_structure/tests/helpers.py @@ -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): """ diff --git a/openedx/core/lib/block_structure/tests/test_cache.py b/openedx/core/lib/block_structure/tests/test_cache.py index c6dd917e01..8d3bc3b620 100644 --- a/openedx/core/lib/block_structure/tests/test_cache.py +++ b/openedx/core/lib/block_structure/tests/test_cache.py @@ -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) )