def test_cache_bulk_invalidation_via_identifier_only_is_paramount(self): # ie, if there is a supplementary identifier, a page will still # get zapped if the main lookup_identifier is invoked # Put three pages into the cache, using the same view, # but 1 + 2 supplementary identifiers. request_1 = self.factory.get('/foo/', SERVER_NAME="example1.com") request_1_cache_key = get_cache_key(request_1.get_host(), request_1.get_full_path()) my_view_cached = cache_page_nginx( self.my_view, lookup_identifier="testsite", supplementary_identifier="category-1") self.assertEqual(my_view_cached(request_1).content, 'content') assert cache.get(request_1_cache_key) request_2 = self.factory.get('/bar/', SERVER_NAME="example1.com") request_2_cache_key = get_cache_key(request_2.get_host(), request_2.get_full_path()) my_view_cached = cache_page_nginx( self.my_view, lookup_identifier="testsite", supplementary_identifier="category-2") self.assertEqual(my_view_cached(request_2).content, 'content') assert cache.get(request_2_cache_key) request_3 = self.factory.get('/baz/', SERVER_NAME="example1.com") request_3_cache_key = get_cache_key(request_3.get_host(), request_3.get_full_path()) my_view_cached = cache_page_nginx( self.my_view, lookup_identifier="testsite", supplementary_identifier="category-2" # repeated identifier ) self.assertEqual(my_view_cached(request_3).content, 'content') assert cache.get(request_3_cache_key) self.assertEqual(CachedPageRecord.objects.count(), 3) # show that invalidating on a bogus lookup identifier does nothing bulk_invalidate('demosite') self.assertNotEqual(cache.get(request_1_cache_key), None) self.assertNotEqual(cache.get(request_2_cache_key), None) self.assertNotEqual(cache.get(request_3_cache_key), None) # now invalidate based on the real lookup identifier bulk_invalidate('testsite') self.assertEqual(cache.get(request_1_cache_key), None) self.assertEqual(cache.get(request_2_cache_key), None) self.assertEqual(cache.get(request_3_cache_key), None) # we should still have those three records present in the DB self.assertEqual(CachedPageRecord.objects.count(), 3)
def test_cache_bulk_invalidation_via_identifier(self): # Put two pages into the cache, using the same view, but two separate # paths, so they count as separate pages request_1 = self.factory.get('/foo/', SERVER_NAME="example1.com") request_1_cache_key = get_cache_key(request_1.get_host(), request_1.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, lookup_identifier="test1") self.assertEqual(my_view_cached(request_1).content, 'content') assert cache.get(request_1_cache_key) request_2 = self.factory.get('/bar/', SERVER_NAME="example1.com") request_2_cache_key = get_cache_key(request_2.get_host(), request_2.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, lookup_identifier="test1") self.assertEqual(my_view_cached(request_2).content, 'content') assert cache.get(request_2_cache_key) # And a repeated path, but with a different identifier # (and on a separate domain, for test realism) request_3 = self.factory.get('/foo/', SERVER_NAME="example2.com") request_3_cache_key = get_cache_key(request_3.get_host(), request_3.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, lookup_identifier="test2") self.assertEqual(my_view_cached(request_3).content, 'content') assert cache.get(request_3_cache_key) self.assertEqual(CachedPageRecord.objects.count(), 3) # now invalidate based on the test1 identifier bulk_invalidate('test1') self.assertEqual(cache.get(request_1_cache_key), None) self.assertEqual(cache.get(request_2_cache_key), None) self.assertNotEqual(cache.get(request_3_cache_key), None) # we should still have those three records present in the DB self.assertEqual(CachedPageRecord.objects.count(), 3) # now invalidate based on the test2 identifier bulk_invalidate('test2') self.assertEqual(cache.get(request_1_cache_key), None) self.assertEqual(cache.get(request_2_cache_key), None) self.assertEqual(cache.get(request_3_cache_key), None) # we should still have those three records present in the DB self.assertEqual(CachedPageRecord.objects.count(), 3)
def test_cache_bulk_invalidation_via_hostname_known_to_be_identifier(self): # V similar to above, but proving that the same thing applies # if you don't set a lookup_identifier and make do with the # default of the hostname as the identifier request_1 = self.factory.get('/foo/', SERVER_NAME="example1.com") request_1_cache_key = get_cache_key(request_1.get_host(), request_1.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, ) self.assertEqual(my_view_cached(request_1).content, 'content') assert cache.get(request_1_cache_key) request_2 = self.factory.get('/bar/', SERVER_NAME="example1.com") request_2_cache_key = get_cache_key(request_2.get_host(), request_2.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, ) self.assertEqual(my_view_cached(request_2).content, 'content') assert cache.get(request_2_cache_key) # And a repeated path, but with a different identifier # (and on a separate domain, for test realism) request_3 = self.factory.get('/foo/', SERVER_NAME="example2.com") request_3_cache_key = get_cache_key(request_3.get_host(), request_3.get_full_path()) my_view_cached = cache_page_nginx(self.my_view, ) self.assertEqual(my_view_cached(request_3).content, 'content') assert cache.get(request_3_cache_key) self.assertEqual(CachedPageRecord.objects.count(), 3) # now invalidate based on the example1.com host/identifier bulk_invalidate('example1.com') self.assertEqual(cache.get(request_1_cache_key), None) self.assertEqual(cache.get(request_2_cache_key), None) self.assertNotEqual(cache.get(request_3_cache_key), None) # we should still have those three records present in the DB self.assertEqual(CachedPageRecord.objects.count(), 3) # now invalidate based on the example2.com host/identifier bulk_invalidate('example2.com') self.assertEqual(cache.get(request_1_cache_key), None) self.assertEqual(cache.get(request_2_cache_key), None) self.assertEqual(cache.get(request_3_cache_key), None) # we should still have those three records present in the DB self.assertEqual(CachedPageRecord.objects.count(), 3)