using memcache for caching query results

9 views
Skip to first unread message

Jonathan

unread,
Mar 3, 2009, 7:02:12 AM3/3/09
to Google App Engine
I am using a restful interface for an ajax application and want to be
able to store the results of queries in memcache, as much of this data
is read much more often than it is written, but it is occasionally
written.

I have been trying to think of strategies for how to do this, whilst
also maintaining the ability to invalidate the cache when necessary.

so for example:
the user requests page 1 of their objects (0-9) and I store them with
a key of "modelName-userName-pageNum"
the user requests page 2 of their objects (10-19) and I store them
with a key of "modelName-userName-pageNum"
the user modifies an object on page 2, (or deletes it, or creates a
new one) and I want to invalidate all "modelName-userName" cached
lists.

how do I do this???


jonathan

Andi Albrecht

unread,
Mar 3, 2009, 7:51:52 AM3/3/09
to google-appengine
2009/3/3 Jonathan <jrick...@gmail.com>:
If you want to delete *all* 'modelName-userName' items, why not store
a mapping (just one cached value) with 'modelName-userName' as key.

For example:

from google.appengine.api import memcache
key = 'model-user'
# load cached values
data = memcache.get(key)
if data is None:
data = {}
# query page1
page1 = some_query()
data[1] = page1
memcache.set(key, data)
# return response
# query page 2
data = memcache.get(key)
if data is None:
data = {}
page2 = some_query()
data[2] = page2
memcache.set(key, data)

And if something's changed, just do "memcache.delete(key)".

Andi


>
>
> jonathan
> >
>

jonathan

unread,
Mar 16, 2009, 8:48:08 AM3/16/09
to Google App Engine
That is a really good idea. I guess it is able to be applied to other
similar kinds of things too, where I am applying filters and paging on
those results too. (though that would get a bit more tricky)

thanks
Jonathan

On Mar 3, 11:51 pm, Andi Albrecht <albrecht.a...@googlemail.com>
wrote:
> 2009/3/3 Jonathan <jricket...@gmail.com>:
>
>
>
>
>
> > I am using a restful interface for an ajax application and want to be
> > able to store the results of queries inmemcache, as much of this data

Joe Bowman

unread,
Mar 16, 2009, 9:00:03 AM3/16/09
to Google App Engine
Check out the cache utility in gaeutilities. http://gaeutilities.appspot.com/cache

Looking at the demo, it appears I need to update that page. Anyhow,
cache uses both the datastore and the memcache.

When you write a cache entry, it writes to the datastore, then to
memcache.
When you attempt to read a cache entry, it first tries memcache, then
the datastore.
If there's a hit in the datastore, but not the memcache, it populates
the memcache.

Supports timeout functionality (my cache hit is only good for 5
minutes), and can be used as a standard dictionary object

c = cache.Cache()
c['cachehit'] = "test value"
if 'cachehit' in c:
do_something()

It was originally written before appengine had memcache support, and
was updated when that was provided.
Reply all
Reply to author
Forward
0 new messages