Wednesday, March 01, 2006

Java Object Caching:

Object caching is an important aspect in the design and development of Web Applications. Object caching is for objects which are neither static (unchanging) nor fully dynamic. The best definition of object caching can be found in the functional specification document for Object Caching Service for JavaOCS4J, which says ...

A server must manage information and executable objects that fall into three basic categories: objects that never change, objects that are different with every request, and everything in between. Java is well equipped to handle the first two cases but offers little help for the third. If the object never changes, we create a static object when the server is initialized. If the object is unique to every request, we create a new object each time. For everything in between, objects or information that can change and are shared across requests, between users or between processes, there is the "Object Caching Service."

Object Caching appears to be similar to object pooling in many ways but, the difference is that pooled objects don't have any identity. While in case of pooling, we are looking for any object, possibly within certain characteristic constraints, in case of caching we are looking for a specific object with an identity and probably a state. The only purpose of object pooling is to prevent memory allocation. While object caching help improve application performance and scalability, it has some cost associated with it.

Consistency: Depending on the type of data that is being cached, the caching mechanism has to ensure consistency between the original and cached objects.

Durability: Changes made to the cache are likely to be lost (System cache).

Size: The memory used by cached objects needs to be maintained at a reasonable amount as is the case with HTTPSession too.

So how would you decide what objects to cache and what should not be cached. The following simple rules may be used as a guide.Which objects may be Cached: Any data that does not change frequently and takes longer times to retrieve from the data source is a good candidate for caching.What not to cache:

Secure information that can be accessed by other users on the web site. Like User profile information and other personal information, or credit card details.

Business information that changes frequently and causes problems if not up to date and accurate.