Chris
Chris

Reputation: 4317

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

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}

Upvotes: 2

Views: 480

Answers (2)

Anentropic
Anentropic

Reputation: 33843

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.

Upvotes: 1

Thilo
Thilo

Reputation: 262534

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.

Upvotes: 4

Related Questions