Home: www.vipan.com Vipan Singla e-mail: vipan@vipan.com
Caching Java Objects

Using In-Memory Cache for Java Objects

  • Sometimes, you feel the need to cache some Java objects, mainly data which takes too long to recreate when it doesn't even change that often. At the cost of ever-increasing memory footprint of your application, you could store these objects in a hashtable. What if you could "expire" older objects in hashtable from time to time, knowing you can always recreate them if needed again?
  • Download expiringcache.jar (about 7KB). This code uses Apache Jakarta Commons Collections and Log4j software. You will need to download these packages, extract the appropriate .jar files out of the downloads and place those .jar files in your application's (or Java server's) classpath.
  • To run the JUnit test file - ExpiringCacheTests.class, you will need the jUnit software. For testing, expiringcache.zip (887KB) contains all of the files. Some of the required .jar files are in the "WEB-INF/lib" directory of the .war file included within the .zip file. You will find a sample "log4j.properties" file in "WEB-INF/classes" directory.
  • Caching is useful in long-running applications such as servlets, JSPs and GUI applications.

Details of In-Memory Cache for Java Objects

JUnit Tests

Ant Build File