MStodd
MStodd

Reputation: 4746

How to cache things for my servlet?

I am working on a web service servlet running on Google App Engine using Spring 3.1, and since I'm pretty new to this game, I simply don't know how I should be caching things. It seems like there are a number of places I can put things to be maintained during the lifetime of my servlet:

  1. Application context. Defining beans here would allow me to easily inject them into my code. This seems like the easiest solution, just scope my beans to be singletons.
  2. Servlet context. For this, when my servlet starts up, I can put things in the ServletContext by grabbing the instance of it, and doing a setAttribute().
  3. Memcache. I haven't looked into this much yet, but I imagine I would just get an instance of the cache, and shove an object into it like the ServletContext.

The specific case I'm curious about is for creating user accounts. I want to keep a HashSet of all user IDs so that I can check if the requested user ID is already in use.

Beyond the specific case above, including cases for using each of the different technologies would be appreciated.

Upvotes: 0

Views: 554

Answers (1)

Sebastian Kreft
Sebastian Kreft

Reputation: 8209

In your specific case I recommend you to use a persistent storage, like the datastore or cloud sql (you can see all alternatives here).

Memcache is not an option, since you can't control how many elements are stored. And any application context is not a solution either, since the instances are in practice being loaded and reloaded many times in different datacenters.

Note also that AppEngine provides support for using Google Accounts or OpenId via the Users Service.

Upvotes: 2

Related Questions