I am starting to use memcache more frequently to avoid having to recalculate things between page requests. When the memcache periodically clears, as it is designed to do, I have to start all over rebuilding various items that I have placed in memcache. What I would like to do is create a very simple model that enables me to periodically save the items that I put into memcache based on the memcache keys that I’m using along with a datetime that is related to the data being memcached. What is the best way to do this?
I’m looking for something like this:
class MemcacheRecord(db.Model):
key = db.StringProperty(required=True)
value = #Something that can store whatever memcache can
validThru = db.DateTimeProperty(required=True)
def set(self, key, value, validThru):
#Save a new memcache record
newMemcacheRecord = MemcacheRecord(key=key, value=value, validThru=validThru)
..
return True # or False
def get_latest(self, key):
#Get the memcache record with the most recent validThru datetime
latestMemcacheRecord = MemcacheRecord.all().order('-validThru').get()
return {'validThru':latestMemcacheRecord.validThru, 'value':latestMemcachRecord.value}
There is probably not much benefit in trying to keep track of what you have saved in memcache, because even if you had the list, there is no guarantee that the data has really been preserved.
I think the only way to work with memcache is to try to get data out of it, with the expectation that it might not be there, even if you put it in before. You have to build your algorithms around that.