Implement fact cache flushing alongside a command-line argument to invoke it.

This commit is contained in:
Josh Drake
2014-07-03 14:02:26 -05:00
committed by Michael DeHaan
parent aa419044c4
commit 917e868f65
7 changed files with 32 additions and 7 deletions

View File

@@ -50,10 +50,12 @@ class FactCache(MutableMapping):
return len(self._plugin.keys())
def copy(self):
"""
Return a primitive copy of the keys and values from the cache.
"""
""" Return a primitive copy of the keys and values from the cache. """
return dict([(k, v) for (k, v) in self.iteritems()])
def keys(self):
return self._plugin.keys()
def flush(self):
""" Flush the fact cache of all keys. """
self._plugin.flush()

View File

@@ -13,3 +13,6 @@ class BaseCacheModule(object):
def delete(self, key):
raise NotImplementedError("Subclasses of {} must implement the '{}' method".format(self.__class__.__name__, self.__name__))
def flush(self):
raise NotImplementedError("Subclasses of {} must implement the '{}' method".format(self.__class__.__name__, self.__name__))

View File

@@ -47,13 +47,13 @@ class CacheModule(MemoryCacheModule):
def set(self, *args, **kwargs):
super(CacheModule, self).set(*args, **kwargs)
self.flush()
self.fsync()
def delete(self, *args, **kwargs):
super(CacheModule, self).delete(*args, **kwargs)
self.flush()
self.fsync()
def flush(self):
def fsync(self):
temp = tempfile.TemporaryFile('r+b')
try:
@@ -63,3 +63,7 @@ class CacheModule(MemoryCacheModule):
shutil.copyfileobj(temp, f)
finally:
temp.close()
def flush(self):
super(CacheModule, self).flush()
self.fsync()

View File

@@ -110,3 +110,7 @@ class CacheModule(BaseCacheModule):
def delete(self, key):
self._cache.delete(self._make_key(key))
self._keys.discard(key)
def flush(self):
for key in self.keys():
self.delete(key)

View File

@@ -35,3 +35,6 @@ class CacheModule(object):
def delete(self, key):
del self._cache[key]
def flush(self):
self._cache = {}

View File

@@ -106,3 +106,7 @@ class CacheModule(BaseCacheModule):
def delete(self, key):
self._cache.delete(self._make_key(key))
self._cache.zrem(self._keys_set, key)
def flush(self):
for key in self.keys():
self.delete(key)