• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Data Caching Ideas

Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently using the Spring Framework, version 1.2.6, for a new application that has a web interface. There are several business/database queries that are very time intensive so I decided to cache this data. The way I implemented this was to create a bean call CacheCleanerJob which contains a List of all the Cache objects. When a new cache is created it registers itself with the CacheCleanerJob.

Because I don't want to keep the cache around indefinitely, the CacheCleanerJob extends org.quartz.Job and runs on a schedule to check each Cache for how long it's been around and if necessary removes the data in the Cache.

I also have a simple JSP page that lists all the Cache's and allows the user to "clean" one or all of the Cache objects on-demand. In addition, there is an interface to the system that allows users to add/delete/modify data that is maintained in the cache. This interface does not directly modify the cache but rather updates the backing database from which the cache is created.

One issue I have with this implementation is that when the Cache is cleared (either on-demand or on a schedule) I'm worried about what will happen if a client is trying the access the Cache at the same time that it is cleared. In fact, in a test cast I've proven in my implementation it's possible (though rare) that if the Cache is cleared at a certain point in time the client will get no data back instead of the "known" result. Also, in some cases I'm getting java.util.ConcurrentModificationException thrown trying to access the Cache objects.

I feel like I'm kind of re-inventing the wheel with this cache implementation and that there may be a better, proven way to implement this functionality to cache data.

I'd really like some feedback on other ideas on how to implement a caching mechanism, either Spring specific or J2EE generic (EJBs?).

Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there are things like ehcache (I haven't used it myself, but heard the name here and there).

What are you using to access the database - plain JDBC or something like Hibernate? Hibernate has support for ehcache.
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are a lot of java cache implentations, ranging from simple to complex. there is definetly no need to implement a cache by yourself.
if you use hibernate to access the DB, then you can transparently plug the cache in (it is called second level cache in hibernate) and enable your objects for caching by modifying the hbm file. if you use some other methods, you might have to do the putting/retrieving from cache yourself:

public Object getObject() {
Object obj = cache.getObject("foo");
if (obj == null) {
obj = database.getObject("foo");
cache.putObject("foo", obj);
return obj;

have a nice evening
The knights of nee want a shrubbery. And a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic