Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Caching System in Java, how to do it?

 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi CodeRanchers,

I am back with a question that is bothering me.
I'm not sure about the answer. I'm not sure whether this is the right place to post it too.
In case it is not please excuse me.

If I were to create a Caching System with say LRU policy for replacing the entry in the cache.
How would I do it?

I will give my version of the answer, please do add to it.

I could create a cache entry with a counter (since I need to know how frequently it was used)
Each time I search for an entry, I come to the cache and see if the entry exists, if it exists then I update the counter by 1 for the enty.
If the entry is not present in the cache then I load it from say the database and assign the value of 1 for the counter. While loading the new entry from the database, if the cache is full then I would go thru the list of the values in the counter field for each entry and remove the entry with the smallest value for the counter and replace it with the new entry with counter value 1.

Now my question is, what would be the best approach to doing this (incrementing the counter and replacing the LRU entry with the new entry with counter value 1) ?
I can do it all in java i.e. a normal class and write the code accordingly.
Or should I use a listener or probably a filter for this?
Or is there some other approach which is better?

Please comment.

Thanks in advance.
Prem Vinodh
SCJP v5.0
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Prem I am a little confused with the 2nd point. Why would you include a filter here? I think by listener you meant an Observer. Well having an observer is good to notify the cache. I would prefer you doing it in Java if you prefer going the scratch implementation. I would prefer you to take a look at memcached and its tweaks for LRU.
 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitish,

Thanks for the reply.

Regarding the second point, I was just trying to give various options as a lead for your'll to think in that direction too.
Because I just wanted to know which approach is the best approach.

Thanks for your answer. I will try out the example which I was trying using Observer
and will take a look at memcached too.

Regards
P.C. Prem Vinodh
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do it using Java Caching System.

Here is the link it might help you http://commons.apache.org/jcs/
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prem Vinodh wrote:Or is there some other approach which is better?


Yes: LinkedHashMap. Have a look at its removeEldestEntry() method. The docs has instructions on how to do exactly what you want.

Winston
 
Prem Vinodh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aggarwal Arpit/Winston Gutkowski,

Thanks for all the wonderful help. Frankly speaking I never knew they were so many options.
I will surely look at the Java Caching System and also the LinkedHashMap.

Thanks once again for helping me out with my problem.

Regards
P.C. Prem Vinodh

 
Whatever you say buddy! And I believe this tiny ad too:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic