title: What is the best way to do AppEngine Model Memcaching?
body: <p>Currently my application caches models in memcache like this: </p>
  
  <pre><code>memcache.set("somekey", aModel)
  </code></pre>
  
  <p>But Nicks' post at <a href="http://blog.notdot.net/2009/9/Efficient-model-memcaching" rel="noreferrer">http://blog.notdot.net/2009/9/Efficient-model-memcaching</a> suggests that first converting it to protobuffers is a lot more efficient. But after running some tests I found out it's indeed smaller in size, but actually <em>slower</em> (~10%).</p>
  
  <p>Do others have the same experience or am I doing something wrong?</p>
  
  <p>Test results: <a href="http://1.latest.sofatest.appspot.com/?times=1000" rel="noreferrer">http://1.latest.sofatest.appspot.com/?times=1000</a></p>
  
  <pre><code>import pickle
  import time
  import uuid
  
  from google.appengine.ext import webapp
  from google.appengine.ext import db
  from google.appengine.ext.webapp import util
  from google.appengine.datastore import entity_pb
  from google.appengine.api import memcache
  
  class Person(db.Model):
   name = db.StringProperty()
  
  times = 10000
  
  class MainHandler(webapp.RequestHandler):
  
   def get(self):
  
    self.response.headers['Content-Type'] = 'text/plain'
  
    m = Person(name='Koen Bok')
  
    t1 = time.time()
  
    for i in xrange(int(self.request.get('times', 1))):
     key = uuid.uuid4().hex
     memcache.set(key, m)
     r = memcache.get(key)
  
    self.response.out.write('Pickle took: %.2f' % (time.time() - t1))
  
  
    t1 = time.time()
  
    for i in xrange(int(self.request.get('times', 1))):
     key = uuid.uuid4().hex
     memcache.set(key, db.model_to_protobuf(m).Encode())
     r = db.model_from_protobuf(entity_pb.EntityProto(memcache.get(key)))
  
  
    self.response.out.write('Proto took: %.2f' % (time.time() - t1))
  
  
  def main():
   application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
   util.run_wsgi_app(application)
  
  
  if __name__ == '__main__':
   main()
  </code></pre>