Memcache

81 views
Skip to first unread message

Ryan Chazen

unread,
Nov 28, 2012, 2:13:11 PM11/28/12
to twig-p...@googlegroups.com
Does twig have some mechanism for storing entities in memcache? Objectify has @Cache annotation, for example.

John Patterson

unread,
Nov 28, 2012, 11:06:08 PM11/28/12
to twig-p...@googlegroups.com
Yes there is a working, but not yet simple, implementation of caching.  It can use a combination of memcache and in-memory caching (using a Guava cache)

I have added a @Cache annotation but not yet wired it up to register the cached entity.

Instead you must register it directly yourself like this:

  1. Create a Configuration class (MyConfiguration extends DefaultConfiguration) which you pass into the constructor of your ObjectDatastore.
  2. Register the cached types only once like this

private MyConfiguration()
{
if (!BaseObjectDatastore.isKindCached(typeToKind(Messages.class)))
{
BaseObjectDatastore.registerCachedKind(typeToKind(Messages.class), 300, 0, true, true);
}
}

Sorry its a bit messy to do this!  The only reason it needs to be in your configuration class is so that it has to be passed into the OD constructor and you need access to typeToKind().  You could however, just hard code the kind name and put the registration code in a static initialiser for your DAO.

The parameters mean:

  1. String kind: kind to cache
  2. int seconds: how long to store items for in memory and memcache
  3. int maximum: max number of items to store in-memory (i.e. not in memcache)
  4. boolean automatic: is the cache used automatically (the other option is to only use it when it is explicitly turned on for a particular operation)
  5. boolean global: should we store in memcache? (if false, only use local in-memory cache)


I use it in production so it is quite stable but just not pretty to configure yet.


John

Manoj Sivakumar

unread,
Dec 3, 2012, 8:37:34 PM12/3/12
to twig-p...@googlegroups.com
Hi John,
         Could you write up a detailed blog post of how to use the new Cache attribute - with an end to end example. It would be very helpful and am sure a lot of people will benefit.

Thanks,
Manoj

John Patterson

unread,
Dec 4, 2012, 9:19:08 PM12/4/12
to twig-p...@googlegroups.com

On 4/12/2012, at 8:37 AM, Manoj Sivakumar wrote:

> Hi John,
> Could you write up a detailed blog post of how to use the new Cache attribute - with an end to end example. It would be very helpful and am sure a lot of people will benefit.

This current configuration is only a temporary stop-gap until the @Cache annotation is wired up correctly.

One caveat, it does not currently work with async calls. For example, currently this does not use the cache at all ds.load().type(MyModeel.class).id(8).later().get();

It makes sense to document the annotation when it is complete and working as expected.
Reply all
Reply to author
Forward
0 new messages