I am developing in Google App Engine since one year ago and I am understanding how important is the warmup time for instances.
So I ended up with an idea: is it possible to cache singleton in memcache? For example I am using the singleton pattern for JDO PersistenceManagerFactory.
Here is my actual code (as described in documentation):
private static PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory(<my-name>);
Does it have any sense something like extend the JDOHelper and write a function like this one:
public static PersistenceManagerFactory getPersistenceManagerFactoryCached(String name) {
MemcacheService cache = MemcacheServiceFactory.getMemcacheService();;
PersistenceManagerFactory staticPMF= null;
if (cache.contains("JDO_PMF")) {
staticPMF = (PersistenceManagerFactory) cache.get("JDO_PMF");
} else {
staticPMF = JDOHelper.getPersistenceManagerFactory(name);
cache.put("JDO_PMF", staticPMF);
}
return staticPMF;
}
My idea should be to cache the PersistenceManagerFactory to speed-up the first instance and then use this one as the singleton:
private static PersistenceManagerFactory pmfInstance = JDOHelperCached.getPersistenceManagerFactory(<my-name>);
Edit: Just found this link, seems different instances cannot share PersistenceManagerFactory instance according to Ikai Lan at https://groups.google.com/group/google-appengine-java/browse_thread/thread/1b90fae408b52d49
==========================================================================
I like the idea, and I have tried straight away on my code, but unfortunately I got some error like this:
Maybe it’s only my issue but I can’t tell at this stage.
I have thought about this more:
So let’s assume the way you suggest is working and you can get the right factory you need. The real performance will depend on how you use your memcache.
1) If you have a lot of write in your memcache, then that means potentially the PersistenceManagerFactory may be kicked out of the cache frequently, which means you need to create it again.
2) Every time you create a new PersistenceManagerFactory, you will put in into the cache which according to my experience dealing with GAE is pretty unstable and sometimes the put in part can take much longer than you expected.