2017-12-14 19:30:59 +00:00
|
|
|
import logging
|
|
|
|
|
2017-12-14 18:36:51 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from six import add_metaclass
|
|
|
|
|
|
|
|
from util.expiresdict import ExpiresDict
|
|
|
|
from util.timedeltastring import convert_to_timedelta
|
|
|
|
|
2017-12-14 19:30:59 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-12-14 18:36:51 +00:00
|
|
|
def is_not_none(value):
|
|
|
|
return value is not None
|
|
|
|
|
|
|
|
|
|
|
|
@add_metaclass(ABCMeta)
|
|
|
|
class DataModelCache(object):
|
|
|
|
""" Defines an interface for cache storing and returning tuple data model objects. """
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def retrieve(self, cache_key, loader, should_cache=is_not_none):
|
|
|
|
""" Checks the cache for the specified cache key and returns the value found (if any). If none
|
|
|
|
found, the loader is called to get a result and populate the cache.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NoopDataModelCache(DataModelCache):
|
|
|
|
""" Implementation of the data model cache which does nothing. """
|
|
|
|
|
|
|
|
def retrieve(self, cache_key, loader, should_cache=is_not_none):
|
|
|
|
return loader()
|
|
|
|
|
|
|
|
|
|
|
|
class InMemoryDataModelCache(DataModelCache):
|
|
|
|
""" Implementation of the data model cache backed by an in-memory dictionary. """
|
|
|
|
def __init__(self):
|
|
|
|
self.cache = ExpiresDict(rebuilder=lambda: {})
|
|
|
|
|
|
|
|
def retrieve(self, cache_key, loader, should_cache=is_not_none):
|
|
|
|
not_found = [None]
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Checking cache for key %s', cache_key.key)
|
2017-12-14 18:36:51 +00:00
|
|
|
result = self.cache.get(cache_key.key, default_value=not_found)
|
|
|
|
if result != not_found:
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Found result in cache for key %s: %s', cache_key.key, result)
|
2017-12-14 18:36:51 +00:00
|
|
|
return result
|
|
|
|
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Found no result in cache for key %s; calling loader', cache_key.key)
|
2017-12-14 18:36:51 +00:00
|
|
|
result = loader()
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Got loaded result for key %s: %s', cache_key.key, result)
|
2017-12-14 18:36:51 +00:00
|
|
|
if should_cache(result):
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Caching loaded result for key %s with expiration %s: %s', cache_key.key,
|
|
|
|
result, cache_key.expiration)
|
2017-12-14 18:36:51 +00:00
|
|
|
expires = convert_to_timedelta(cache_key.expiration) + datetime.now()
|
|
|
|
self.cache.set(cache_key.key, result, expires=expires)
|
2017-12-14 19:30:59 +00:00
|
|
|
logger.debug('Cached loaded result for key %s with expiration %s: %s', cache_key.key,
|
|
|
|
result, cache_key.expiration)
|
|
|
|
else:
|
|
|
|
logger.debug('Not caching loaded result for key %s: %s', cache_key.key, result)
|
2017-12-14 18:36:51 +00:00
|
|
|
|
|
|
|
return result
|