def clear_cache(self): """Go through each cacheable page, and show which are cached and which are NOT""" for node_type in ['Topic', 'Video', 'Exercise']: self.stdout.write("Clearing %ss:\n" % node_type) for narr in topic_tools.get_node_cache(node_type).values(): for n in narr: if caching.has_cache_key(path=n["path"]): self.stdout.write("\t%s\n" % n["path"]) caching.expire_page(path=n["path"])
def test_cache_across_clients(self): """Show that caching is accessible across all clients (i.e. that different clients don't generate different cache keys)""" # Get a random video id n_videos = len(self.video_cache) video_id = random.choice(self.video_cache.keys()) sys.stdout.write("Testing on video_id = %s\n" % video_id) video_path = self.video_cache[video_id][0]['path'] # Clean the cache for this item caching.expire_page(path=video_path, failure_ok=True) self.assertTrue(not caching.has_cache_key(path=video_path), "expect: No cache key after expiring the page") # Set up the cache with Django client Client().get(video_path) self.assertTrue(caching.has_cache_key(path=video_path), "expect: Cache key exists after Django Client get") caching.expire_page(path=video_path) # clean cache self.assertTrue(not caching.has_cache_key(path=video_path), "expect: No cache key after expiring the page") # Get the same cache key when getting with urllib, and make sure the cache is created again urllib.urlopen(self.live_server_url + video_path).close() self.assertTrue(caching.has_cache_key(path=video_path), "expect: Cache key exists after urllib get") caching.expire_page(path=video_path) # clean cache self.assertTrue(not caching.has_cache_key(path=video_path), "expect: No cache key after expiring the page") # Same deal, now using requests library requests.get(self.live_server_url + video_path) self.assertTrue(caching.has_cache_key(path=video_path), "expect: Cache key exists after requestsget") caching.expire_page(path=video_path) # clean cache self.assertTrue(not caching.has_cache_key(path=video_path), "expect: No cache key after expiring the page")
def test_cache_invalidation(self): """Create the cache item, then invalidate it and show that it is deleted.""" # Get a random video id n_videos = len(self.video_cache) video_id = self.video_cache.keys()[10]#random.choice(self.video_cache.keys()) sys.stdout.write("Testing on video_id = %s\n" % video_id) video_path = self.video_cache[video_id][0]['path'] # Clean the cache for this item caching.expire_page(path=video_path, failure_ok=True) # Create the cache item, and check it self.assertTrue(not caching.has_cache_key(path=video_path), "expect: no cache key after expiring the page") caching.regenerate_all_pages_related_to_videos(video_ids=[video_id]) self.assertTrue(caching.has_cache_key(path=video_path), "expect: Cache key exists after Django Client get") # Invalidate the cache item, and check it caching.invalidate_all_caches() # test the convenience function self.assertTrue(not caching.has_cache_key(path=video_path), "expect: no cache key after expiring the page")
def create_page_cache(self, path, force=False): """Go through each cacheable page, and either: (a) Cache each page that is not or (b) Kill and regenerate each page""" if force: if caching.has_cache_key(path=path): caching.expire_page(path=path) self.stdout.write("[Redo]\t%s\n" % path) else: self.stdout.write("[Miss]\t%s\n" % path) caching.create_cache(path=path) else: if not caching.has_cache_key(path=path): caching.create_cache(path=path) self.stdout.write("[Miss]\t%s\n" % path) if not caching.has_cache_key(path=path): # should never get here! self.stdout.write("%s%s\n" % ("?"*10, path))
def test_cache_invalidation(self): """Create the cache item, then invalidate it and show that it is deleted.""" # Get a random video id n_videos = len(self.video_cache) video_id = self.video_cache.keys()[ 10] #random.choice(self.video_cache.keys()) sys.stdout.write("Testing on video_id = %s\n" % video_id) video_path = self.video_cache[video_id][0]['path'] # Clean the cache for this item caching.expire_page(path=video_path, failure_ok=True) # Create the cache item, and check it self.assertTrue(not caching.has_cache_key(path=video_path), "expect: no cache key after expiring the page") caching.regenerate_all_pages_related_to_videos(video_ids=[video_id]) self.assertTrue(caching.has_cache_key(path=video_path), "expect: Cache key exists after Django Client get") # Invalidate the cache item, and check it caching.invalidate_all_caches() # test the convenience function self.assertTrue(not caching.has_cache_key(path=video_path), "expect: no cache key after expiring the page")