I'm moving a Google App Engine web application outside "the cloud" to a standard web framework (webpy) and I would like to know how to implement a memcache feature available on Gae.
In my app I just use this cache to store a bunch of data retrieved from a remote api every X hours; in other words I don't stress this cache too much.
I've naively implemented something like this:
class TinyCache():
class _Container():
def __init__(self, value, seconds):
self.value = value
self.cache_age = datetime.now()
self.cache_time = timedelta(seconds = seconds)
def is_stale(self):
return self.cache_age + self.cache_time < datetime.now()
def __init__(self):
self.dict_cache={}
def add(self, key, value, seconds = 7200):
self.dict_cache[key] = self._Container(value, seconds)
def get(self, key):
if key in self.dict_cache:
if self.dict_cache[key].is_stale():
del self.dict_cache[key]
return None
else:
return self.dict_cache[key].value
else:
return None
A typical usage would be:
data = tinycache.get("remote_api_data")
if data is not None:
return data
else:
data = self.api_call()
tinycache.add("remote_api_data", data, 7200)
return data
How could I improve it?
Do I need to make it Thread-Safe?