Ejemplo n.º 1
0
    def test_dependents(self):
        dependent_cache_name = self.check_cache_gone('dependent')
        dependent_cache = PersistentCache(dependent_cache_name, cache_manager=self.manager)

        parent_cache_name = self.check_cache_gone('parent')
        parent_cache = PersistentCache(parent_cache_name, cache_manager=self.manager, dependents=[dependent_cache])

        dependent_cache['foo'] = 'bar'
        parent_cache.save(True)
        dependent_cache['foo'] = 'saved'

        parent_cache.load(True)
        self.assertEqual(dependent_cache['foo'], 'bar')

        dependent_cache['foo'] = 'invalid'
        parent_cache.invalidate(True)
        self.assertEqual(dependent_cache['foo'], 'bar')

        parent_cache.delete_saved_content(True)
        parent_cache.load(True)
        self.assertIsNone(dependent_cache.contents)

        parent_cache.load_or_build(True)
        self.assertDictEqual(dependent_cache.contents, {})
        dependent_cache['foo'] = 'bar'
        dependent_cache.save()

        parent_cache.invalidate_and_rebuild(True)
        self.assertDictEqual(dependent_cache.contents, {})
Ejemplo n.º 2
0
    def test_invalidate_and_rebuild(self):
        cache_name = self.check_cache_gone('invalidate_rebuild')
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents={ 'foo': 'bar' })
        cache.save()

        cache.invalidate_and_rebuild()
        self.assert_contents_equal(cache, {})

        cache.load() # Saved content should get replaced
        self.assert_contents_equal(cache, {})
Ejemplo n.º 3
0
    def test_content_driven_cache_wrap(self):
        cache_name = self.check_cache_gone('content')

        # Pass a non-empty list as content
        cache = PersistentCache(cache_name, cache_manager=self.manager, contents=[''],
            builder=lambda *args: [])
        cache[0] = 'foo'
        cache.append('bar')
        cache.save()

        cache.load()
        self.assertTrue(isinstance(cache, PersistentCache))
        self.assertTrue(isinstance(cache.contents, list))
        self.check_cache(cache_name, True)
        self.assertEqual(cache[0], 'foo')
        self.assertEqual(cache[1], 'bar')

        cache.invalidate_and_rebuild()
        self.assertTrue(isinstance(cache, PersistentCache))
        self.assertTrue(isinstance(cache.contents, list))
        self.assert_contents_equal(cache, [])