-
1. Re: Custom Eviction Policy Based On Object Attribute
kkalmbach Apr 25, 2005 9:13 AM (in response to bryan_castillo)I was tring to do similar things and submitted some changes that were put into CVS last week.
So first, get the latest code. I think it's in the latest 1.2.2 release, but I'm not sure.
Then make a new EvictionPolicy that extends LRUPolicy. the only thing you need to do here is override the method getEvictionAlgorithm() to return a custom alogrithm that you will create.
Then make a new EvictionAlgorithm that entends LRU or FIFO Algorithm and overrides the method
protected boolean shouldEvictNode(NodeEntry entry, Region region, long currentTime). You probably want to call super() on this and check the result, but that's up to you really.
Given the NodeEntry, you can get the data from the cache and just about anything else. Just have your method return true or false based upon whatever criteria you have for the node, the LRUAlgorithm does the rest.
One more thing, in my algorithm, I needed to look at some data in the node, but if I got the data, the access time would update, and nothing would ever age-out.
In my Policy (not algorithm) I also put in the following method to get the data without updating the access time.public Object peekCacheData(Fqn fqn, Object key) { try { return cache_.peek(fqn, key); } catch (CacheException e) { log.error("exception in peekCacheData "+fqn+":"+ key, e); } return null; }
The algorithm can then get the Policy (from the Region) and the Policy can peek at data without updating the access time.
Hope that helps, if not let me know.
-Kevin