Using com.google.appengine.api.memcache.MemcacheService for caching in App Engine, I’m creating the memcacheservice instance using a factory, and injecting it into my classes with spring as a singleton.
I’m asking if this is correct and optimal and if anyone has input on the positive or negative effect of using the lazy-init option or synchronized(). I’d like to have the answer to this question result in the best possible way to use memcacheservice in this manner.
Thanks!
Factory:
public class CacheFactory {
private static MemcacheService INSTANCE;
private CacheFactory() {
}
public static MemcacheService getInstance() {
if (INSTANCE == null) {
synchronized(CacheFactory.class){
INSTANCE = MemcacheServiceFactory.getMemcacheService();
}
}
return INSTANCE;
}
}
Spring Bean:
<bean id="cacheFactory" class="com.nimbits.server.transactions.memcache.CacheFactory"
factory-method="getInstance" lazy-init="false" scope="singleton" >
</bean>
sample usage:
<bean id="someService" class="SomeServiceImpl">
<property name="cacheFactory" ref="cacheFactory" />
</bean>
This solution will work but it is problematic:
You should clearly understand that it is not a complete singleton because every instance will have its own instance of your factory.
It will affect dynamic instances start time. There is a known problem that every spring bean will add some time to instance start time. SO if your HTTP request will cause instance to start there will be a delay between request start and actual response process. A lot of people faced this problem during last several; months. Instance start can be even more than 60 second and you will got hard deadline Exception. So there is a recommendation not to use Spring at all to speed up start time. Check http://code.google.com/p/googleappengine/issues/detail?id=7706 for details.