Enabling JDO caching on Google App Engine
[Update: There is a bug in the version of datanucleus that app engine uses, which prevents this from working with embedded entities. The bug has been fixed in datanucleus 2.x but as long as Google does not update app engine, embedded entities will not work with cache enabled (becaue app engine still requires datanucleus 1.x). Try annotating your embedded entities with @PersistenceCapable(cacheable="false").]
GAE/J supports caching with JDO via javax.cache (thanks to DataNucleus for pointing this out to me on stackoverflow!). Here is how you enable it:
- Download the datanucleus cache plugin (only version 1.x is supported by GAE/J) datanucleus-cache-1.1.1.jar.
- Copy the jar to your war/WEB-INF/lib
- Add the jar to your build path.
- Edit the JDO configuration file in src/META-INF/jdoconfig.xml (assuming the default project layout, src is your source folder) by adding the following lines inside the persistence-manage-factory tag:
<property name="datanucleus.cache.level2.type" value="javax.cache" />
<property name="datanucleus.cache.level2.cacheName" value="jdocache" /> <!-- You may replace "jdocache" by a name of your choice -->
Verify that it works
In pseudo code:
- Create 100 new entities and persist them.
- Get the memcache statistics like so:Stats before = MemcacheServiceFactory.getMemcacheService().getStatistics();
Fetch the 100 entites from the datastore.
Get the memcache again Stats after1 = MemcacheServiceFactory.getMemcacheService().getStatistics();
Fetch the same 100 entities again.
Get the memcache Stats after2 = MemcacheServiceFactory.getMemcacheService().getStatistics;
You can simply System.out.println those status (i.e. before, after1 and after2) and should note that last batch of stats shows (among others) "Hits: 100". This indicates that fetching the entities for the second time (in step 5) was successfully done using the cache instead of from the datastore. If all stats are 0 all the time then caching was not enabled. Re-trace your steps and try again!
- If you get a JDOException that javax.cache is not registered: double check that you have added the datanucleus-cache-1.1.1.jar to your build path and your server path.