def test_cache_expire_during_get(self): # create a new cache folder EXPIRE_CACHE_DIRECTORY = os.path.join(TEST_DIRECTORY, "test_cache_expire_during") os.makedirs(EXPIRE_CACHE_DIRECTORY, exist_ok=True) # create two files, the first won't have its modified time changed and the # second will have its last modified time to be more than 60 seconds ago html_string = "<html></html>".encode("utf-8") first_url = "http://www.example.com/testpage.html" first_domain, first_filename = dir_domain(first_url), clean_url_hash(first_url) # the two urls share the same domain, so only need to create first full_domain_path = os.path.join(EXPIRE_CACHE_DIRECTORY, first_domain) os.makedirs(full_domain_path, exist_ok=True) first_path = os.path.join(full_domain_path, first_filename) with open(first_path, "wb") as fp: fp.write(html_string) self.assertTrue(os.path.isfile(first_path)) c = Cache(EXPIRE_CACHE_DIRECTORY, max_age=60) self.assertIsNotNone(c.get(first_url)) # modify the last modified time of the file to two minutes ago now = int(time.time()) then = now - 120 os.utime(first_path, (then, then)) # trying to get it again should remove it self.assertIsNone(c.get(first_url)) self.assertFalse(os.path.isfile(first_path)) shutil.rmtree(EXPIRE_CACHE_DIRECTORY)
def test_cache_get(self): c = Cache(CACHE_DIRECTORY) exists = [ "http://www.example.com/somepage.html", "http://www.example.com/otherpage" ] for filename in exists: self.assertIsNotNone(c.get(filename)) does_not_exist = [ "http://www.example.com/doesnotexist.html", "https://en.wikipedia.org/wiki/Python_(programming_language)" ] for filename in does_not_exist: self.assertIsNone(c.get(filename))