class FileBasedCacheTests(unittest.TestCase): """ Specific test cases for the file-based cache. """ def setUp(self): self.dirname = tempfile.mktemp() os.mkdir(self.dirname) self.cache = FileCache(self.dirname, {}) def tearDown(self): shutil.rmtree(self.dirname) def test_hashing(self): """Test that keys are hashed into subdirectories correctly""" self.cache.set("foo", "bar") keyhash = md5.new("foo").hexdigest() keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) self.assertTrue(os.path.exists(keypath)) def test_subdirectory_removal(self): """ Make sure that the created subdirectories are correctly removed when empty. """ self.cache.set("foo", "bar") keyhash = md5.new("foo").hexdigest() keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) self.assertTrue(os.path.exists(keypath)) self.cache.delete("foo") self.assertTrue(not os.path.exists(keypath)) self.assertTrue(not os.path.exists(os.path.dirname(keypath))) self.assertTrue(not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
class FileBasedCacheTests(unittest.TestCase): """ Specific test cases for the file-based cache. """ def setUp(self): self.dirname = tempfile.mktemp() os.mkdir(self.dirname) self.cache = FileCache(self.dirname, {}) def tearDown(self): shutil.rmtree(self.dirname) def test_hashing(self): """Test that keys are hashed into subdirectories correctly""" self.cache.set("foo", "bar") keyhash = md5.new("foo").hexdigest() keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) self.assert_(os.path.exists(keypath)) def test_subdirectory_removal(self): """ Make sure that the created subdirectories are correctly removed when empty. """ self.cache.set("foo", "bar") keyhash = md5.new("foo").hexdigest() keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) self.assert_(os.path.exists(keypath)) self.cache.delete("foo") self.assert_(not os.path.exists(keypath)) self.assert_(not os.path.exists(os.path.dirname(keypath))) self.assert_( not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
def load_tmp_file(name): ''' 加载之前保存的临时文件 ''' return CacheClass(tmpDir(), {"max_entries":1024}).get(name)
def save_tmp_file(name, object): CacheClass(tmpDir(), {"max_entries":1024}).set(name, object)
def setUp(self): self.dirname = tempfile.mktemp() os.mkdir(self.dirname) self.cache = FileCache(self.dirname, {})
def pos_load_tmp_file(name): return CacheClass(tmpDir(), {"max_entries":1024}).get(name)