Commit b82e898f authored by Jim Fulton's avatar Jim Fulton

Added a method to clear the cache.

parent 8ef7056b
......@@ -208,7 +208,7 @@ class ClientCache(object):
self.f.write(magic+z64)
logger.info("created temporary cache file %r", self.f.name)
self._initfile(self.f, fsize)
self._initfile(fsize)
# Statistics: _n_adds, _n_added_bytes,
# _n_evicts, _n_evicted_bytes,
......@@ -225,18 +225,24 @@ class ClientCache(object):
def fc(self):
return self
def clear(self):
self.f.seek(ZEC_HEADER_SIZE)
self.f.truncate()
self._initfile(ZEC_HEADER_SIZE)
##
# Scan the current contents of the cache file, calling `install`
# for each object found in the cache. This method should only
# be called once to initialize the cache from disk.
def _initfile(self, f, fsize):
def _initfile(self, fsize):
maxsize = self.maxsize
f = self.f
read = f.read
seek = f.seek
write = f.write
seek(0)
if read(4) != magic:
raise ValueError("unexpected magic number: %r" % _magic)
raise ValueError("unexpected magic number: %r" % magic)
self.tid = read(8)
if len(self.tid) != 8:
raise ValueError("cache file too small -- no tid at start")
......
......@@ -231,6 +231,20 @@ class CacheTests(ZODB.tests.util.TestCase):
# VM).
del testVeryLargeCaches
del testConversionOfLargeFreeBlocks
def test_clear_zeo_cache(self):
cache = self.cache
for i in range(10):
cache.store(p64(i), n2, None, str(i))
cache.store(p64(i), n1, n2, str(i)+'old')
self.assertEqual(len(cache), 20)
self.assertEqual(cache.load(n3), ('3', n2))
self.assertEqual(cache.loadBefore(n3, n2), ('3old', n1, n2))
cache.clear()
self.assertEqual(len(cache), 0)
self.assertEqual(cache.load(n3), None)
self.assertEqual(cache.loadBefore(n3, n2), None)
def testChangingCacheSize(self):
# start with a small cache
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment