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?
@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.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api