Caching
Java Caching
Any kind of web application is required to keep some data in the JVM’s memory space so as to identify the user, number of transactions, order details, department ID, Project ID or user’s IP address. A common example of storing this data in the memory is when you want the data entered by user is one page in another page (which is not the next page/response).
If the data is to be shared with the response/next page then the data is readily available in the request object. In other cases, the data can be stored in various contexts like Session or Application. The session and application scopes are available in Servlets and JSP both. It is worth mentioning that these scopes should be used to store only small amount of data.
A more important case to go for Java caching system is to improve performance of the application by storing data in local memory than to perform I/O or database operations.For storing large amount of data, some enterprise level Java caching system should be used. There are many Java distributed Java caching products available in the market which include:
Options for Java caching
Please note that one can always avoid caching by fetching the same data from the database but that may become bottleneck because of network tips involved. The idea of Java caching frameworks/products is that the frequently accessed data be stored in the cache. If for some data request, the data was not found in the cache then the data is fetched from the database and cached so that next time it can be retrieved from the database.
Here is a sample program to use the Cacheonix cache system for storing the data and then retrieving it later on.
package com.example;
import java.util.Map;
import cacheonix.Cacheonix;
public class Test {
public static void main(String[] args) {
final Cacheonix cacheManager = Cacheonix.getInstance();
final Map cache = cacheManager.getCache("invoce.cache");
// Put object to the cache
final Object key = "key";
final Object value = "value";
cache.put(key, value);
// Get object from the cache
final Object myObject = cache.get(key);
System.out.println(myObject);
}
}
It is also worth mentioning that some people confuse application caching with JPA. JPA is a specification for how to store the Java objects into relational database systems and has nothing to do with distributed caching systems.
Some Java based distributed caching frameworks have a configuration file where one can specify various parameters like max cache size, number of objects to cache and expiry period of cache etc. The java caching systems take care of multi-threading issue so that developer can write his code as he would without using the java cache systems.
JEE Tutorials
Struts Interview Questions after 5 years
Distributed Cache tools for Java Applications
Caching Data in Java Applications
Is Struts2 Obsolete




