• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object caching best approach

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application (catalogue) that contains data that does not change that frequently, 1000 product object 10 t0 20 may change or be added onec a week.

Now do I need to use a weakHasMap as the GC will remove reference when not really nessasary?

I want implement a manager to expel old data objects when I commit to the database and those objects are recreated etc...

Can someone who has developed an app that implements object caching give me some advice on a best practice.

cheers

Ras
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not implement a simple LRU list? put your cached items in an ArrayList/Vector of fixed size and remove the least recently used one on demand, if u can not put all items in cache
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With caching you are betting that extra memory usage will be offset by improved performance. If you can afford the memory for all 10,000 use a normal HashMap and don't worry about removing unused entries.

To remove the "least recently used" or entries that have exceeded a "time to live" value you'll have to keep track of access times and maybe sweep through the collection periodically to find removable entries. Then you're betting that less memory usage plus housekeeping will be offset by improved performance. It could be true.

Removing things that change is an interesting challenge, too. I'd probably set up a listener or pub-sub structure, publish an event any time I updated the database, listen for that event and invalidate the cache key or remove the item. The next time somebody tries to retrieve that key from the cache it won't be there and they'll have to hit the database to get it.

You could distribute the pub-sub with JMS across multiple servers if you wind up in a cluster.

Any of that sound useful? Raise any other questions?
 
rastas biggs
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys,

I have decided on a package OSCache, it gives me support for clusters and all the API's I need, and has LRU etc...

I took me half a day to impliment into my app nice a stright forward.

Ras
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exellent - buy or reuse before build is often said, less often practiced. Let us know how it works out. Real life user reviews are helpful to us all.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic