do not preload cache to show or delete items

Change-Id: I0cf8f597c4c9da6739da3d11661c4b2811ca508a
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2018-05-05 20:02:38 -04:00
parent b1848f2e8d
commit e1ae7cb94a
3 changed files with 24 additions and 13 deletions

View File

@ -29,13 +29,14 @@ class Cache:
"""
def __init__(self, filename):
def __init__(self, filename, preload=True):
self._shelf = shelve.open(filename)
self._memory = {}
LOG.debug('loading cache into RAM')
for key in self._shelf:
self._memory[key] = self._shelf[key]
LOG.debug('loaded %d items from cache', len(self._memory))
if preload:
LOG.debug('loading cache into RAM')
for key in self._shelf:
self._memory[key] = self._shelf[key]
LOG.debug('loaded %d items from cache', len(self._memory))
self._data = collections.ChainMap(self._memory, self._shelf)
def __contains__(self, key):

View File

@ -37,7 +37,8 @@ class CacheRemove(command.Command):
return parser
def take_action(self, parsed_args):
del self.app.cache[(parsed_args.type, parsed_args.id)]
cache = self.app._load_cache_file(preload=False)
del cache[(parsed_args.type, parsed_args.id)]
class CacheShow(command.Command):
@ -57,5 +58,6 @@ class CacheShow(command.Command):
return parser
def take_action(self, parsed_args):
data = self.app.cache[(parsed_args.type, parsed_args.id)]
cache = self.app._load_cache_file(preload=False)
data = cache[(parsed_args.type, parsed_args.id)]
pprint.pprint(data)

View File

@ -50,13 +50,21 @@ class WhoHelped(app.App):
def initialize_app(self, argv):
# Quiet the urllib3 module output coming out of requests.
logging.getLogger('urllib3').setLevel(logging.WARNING)
self._cache = None
# Open the cache file.
if self.options.cache_file:
self.cache = caching.Cache(self.options.cache_file)
else:
# Use a dictionary for a memory cache.
self.cache = {}
def _load_cache_file(self, preload=True):
return caching.Cache(self.options.cache_file, preload=preload)
@property
def cache(self):
if self._cache is None:
# Open the cache file.
if self.options.cache_file:
self._cache = self._load_cache_file()
else:
# Use a dictionary for a memory cache.
self._cache = {}
return self._cache
def main(argv=sys.argv[1:]):