Class LruCache

  • All Implemented Interfaces:
    Cache
    Direct Known Subclasses:
    LruEJBCache

    public class LruCache
    extends BaseCache
    LRUCache in-memory bounded cache with an LRU list
    • Field Detail

      • trimCount

        protected int trimCount
      • listSize

        protected int listSize
      • timeout

        protected long timeout
      • defaultMaxEntries

        protected int defaultMaxEntries
      • isUnbounded

        protected boolean isUnbounded
    • Constructor Detail

      • LruCache

        public LruCache()
        default constructor
      • LruCache

        public LruCache​(int defaultMaxEntries)
        constructor with specified max entries.
        Parameters:
        defaultMaxEntries - specifies the default max entries to use when the maxEntries is <= 0.
    • Method Detail

      • init

        public void init​(int maxEntries,
                         long timeout,
                         float loadFactor,
                         Properties props)
        initialize the cache
        Parameters:
        maxEntries - maximum number of entries expected in the cache
        loadFactor - the load factor
        timeout - to be used to trim the expired entries
        props - opaque list of properties for a given cache implementation
        Throws:
        a - generic Exception if the initialization failed
      • setTimeout

        public void setTimeout​(long timeout)
        sets the timeout value
        Parameters:
        timeout - to be used to trim the expired entries
      • createItem

        protected BaseCache.CacheItem createItem​(int hashCode,
                                                 Object key,
                                                 Object value,
                                                 int size)
        create new item
        Overrides:
        createItem in class BaseCache
        Parameters:
        hashCode - for the entry
        key - Object key
        value - Object value
        size - size in bytes of the item subclasses may override to provide their own CacheItem extensions e.g. one that permits persistence.
      • trimLru

        protected BaseCache.CacheItem trimLru​(long currentTime)
        trim one item from the LRU list
        Parameters:
        currentTime - of this operation
        Returns:
        the item trimmed from cache list synchronization is handled by the caller
      • itemAdded

        protected BaseCache.CacheItem itemAdded​(BaseCache.CacheItem item)
        /** this item is just added to the cache
        Overrides:
        itemAdded in class BaseCache
        Parameters:
        item - CacheItem that was created
        Returns:
        a overflow item; may be null this function checks if adding the new item results in a overflow; if so, it returns the item to be removed. Cache bucket is already synchronized by the caller
      • itemAccessed

        protected void itemAccessed​(BaseCache.CacheItem item)
        this item is accessed
        Overrides:
        itemAccessed in class BaseCache
        Parameters:
        item - CacheItem accessed Cache bucket is already synchronized by the caller
      • itemRefreshed

        protected void itemRefreshed​(BaseCache.CacheItem item,
                                     int oldSize)
        item value has been refreshed
        Overrides:
        itemRefreshed in class BaseCache
        Parameters:
        item - CacheItem that was refreshed
        oldSize - size of the previous value that was refreshed Cache bucket is already synchronized by the caller
      • itemRemoved

        protected void itemRemoved​(BaseCache.CacheItem item)
        item value has been removed from the cache
        Overrides:
        itemRemoved in class BaseCache
        Parameters:
        item - CacheItem that was just removed Cache bucket is already synchronized by the caller
      • trimExpiredEntries

        public void trimExpiredEntries​(int maxCount)
        trim the expired entries from the cache.
        Specified by:
        trimExpiredEntries in interface Cache
        Overrides:
        trimExpiredEntries in class BaseCache
        Parameters:
        maxCount - maximum number of invalid entries to trim specify Integer.MAX_VALUE to trim all invalid entries This call is to be scheduled by a thread managed by the container. NOTE: this algorithm assumes that all the entries in the cache have identical timeout (otherwise traversing from tail won't be right).
      • getStatByName

        public Object getStatByName​(String key)
        get the desired statistic counter
        Specified by:
        getStatByName in interface Cache
        Overrides:
        getStatByName in class BaseCache
        Parameters:
        key - to corresponding stat
        Returns:
        an Object corresponding to the stat See also: Constant.java for the key
      • getStats

        public Map getStats()
        Description copied from class: BaseCache
        get the stats snapshot
        Specified by:
        getStats in interface Cache
        Overrides:
        getStats in class BaseCache
        Returns:
        a Map of stats See also: Constant.java for the keys