Memcache: Should we save Model instance or protocol buffer?

45 views
Skip to first unread message

Nickolas Daskalou

unread,
Feb 4, 2010, 4:43:26 AM2/4/10
to google-a...@googlegroups.com
Is it better/safer to store a Model instance into Memcache directly (Method 1 below), or should we convert it to a protocol buffer first, then store it (Method 2 below)?

Method 1:

memcache.set(cache_key, entity)
...
entity = memcache.get(cache_key)


Method 2:

memcache.set(cache_key, db.model_to_protobuf(entity))
...
entity = db.protobuf_to_model(memcache.get(cache_key))


I'm assuming Method 2 results in a smaller Memcache footprint, yes?

Nick

Sylvain

unread,
Feb 4, 2010, 11:11:48 AM2/4/10
to Google App Engine
I think the answer (and more) is here :
http://blog.notdot.net/2009/9/Efficient-model-memcaching

Nickolas Daskalou

unread,
Feb 4, 2010, 11:34:52 AM2/4/10
to google-a...@googlegroups.com
Perfect, thanks Sylvain. :)

Nick


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.


Andy Freeman

unread,
Feb 5, 2010, 12:54:32 AM2/5/10
to Google App Engine
Note that memcaching a protocol buffer has interesting consequences.
One is that the auto-now datetime properties are updated when the
protocol buffer is created. This update is just on the protocol
buffer - it doesn't affect the datastore copy.

http://code.google.com/p/googleappengine/issues/detail?id=2402

> > Nick- Hide quoted text -
>
> - Show quoted text -

Nickolas Daskalou

unread,
Feb 5, 2010, 1:27:42 AM2/5/10
to google-a...@googlegroups.com
Thanks Andy. Do you know what happens to a RefenceProperty that has already had the referenced entity "loaded" on the Model instance? Does the referenced entity also get saved in the ProtocolBuffer, or is only its key saved?


Andy Freeman

unread,
Feb 6, 2010, 2:21:57 AM2/6/10
to Google App Engine
I'm pretty sure that the protocol buffer saves just the key.

However, I'm virtually certain that pickle will save both the instance
and the key if an reference property has been used. And, if that
instance has used reference properties, their instances are also saved
by pickle. I mention pickle because that's what memcache uses to
store instances.

It's an interesting choice - do you pickle instances as is, which
won't change their datetime now properties, but will save instances if
you've used the a reference property that names them. Or, do you
memcache a protocol buffer, which will update datetime now properties
but won't save instances associated with reference properties.

> > google-appengi...@googlegroups.com<google-appengine%2Bunsubscrib­e...@googlegroups.com>


> > .
> > For more options, visit this group at

> >http://groups.google.com/group/google-appengine?hl=en.- Hide quoted text -

Reply all
Reply to author
Forward
0 new messages