1 Reply Latest reply on Apr 25, 2005 9:13 AM by kkalmbach

    Custom Eviction Policy Based On Object Attribute

    Bryan Castillo Newbie

      I wanted to create my own eviction policy where the time to live would be determined by an attribute of the object. I am currently using the FIFOPolicy. It looks like I could extend that and override the getEvictionAlgorithm method to return a custom Policy inheriting from the LRUAlgorithm. Then I could override the evict method and decide if I really wanted to evict the node or not.

      One of the problems I'm seeing though has to do with the prune method. I wouldn't want to overide the eviction of nodes when the eviction is occuring because of the number of items (I don't want to run out of memory). But I would want to override the pruning items based on age.

      The way I was looking to do this seems like a hack. Is there a better way to do this?

      To give some perspective:

      I have items in a cache representing the price of an item. The prices of these items may change at any time. The source of the price comes from an SAP backend. One of the attributes on the priced item is a field indicating the relevancy of the item saying how well the item has sold in the last few months. Items which are sold more frequently should expire in the cache faster than items that don't sell as frequently.

      Before, I started looking at implementing a custom eviction policy, I was wrapping access to the data in a DAO pattern, after pulling the item out of the cache I was examing the field and checking the retieval time of the item manually. If it was too old, I would get the item directly from the SAP backend.

      Thanks.

        • 1. Re: Custom Eviction Policy Based On Object Attribute
          kkalmbach Newbie

          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