Is it okay to obtain a reference to a service from factory once and use it for handling multiple requests? It can be best explained using following pseudo-code for a servlet:
SomeServlet...{
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
UserService userService = UserServiceFactory.getUserService();
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
doGet(...){
userService.doSomething(...);
}
...
}
It may be a premature optimization but i am just curious to know what is the cost of obtaining service for each request. Please share your insight.
In the google group thread http://groups.google.com/group/google-appengine-java/browse_thread/thread/d3f1536084f59c22, Ikai Lan (from the GAE team at Google) says MemcacheService is thread-safe, but that caching it is not useful, because there is just one object allocation each time you get the service from the factory.
Since all the services are obtained in a similar way, I think we could assume that they all follow the same design and are all thread-safe. But since it isn't costly to get a new instance at each query, I wouldn't cache them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With