Tutorial :In Google App Engine, what is the simplest way to keep a record of items that you have put into memcache?



Question:

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}  


Solution:1

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.


Solution:2

import pickle    memcacherecord.value = pickle.dumps(mydict)  memcacherecord.put()    mydict = pickle.loads(memcacherecord.value)  

What you describe you're trying to do sounds like an anti-pattern.

Despite being able to give an expiry date, whether or not your key is still saved in memcache is unpredictable. Trying to store that info in the datastore will be slow and sometimes wrong.

As @Thilo said, just attempt to get from memcache without trying to know if it's already there. If it fails, fetch from the datastore and put it in memcache for the next requester.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »