Class AbstractCache

  • All Implemented Interfaces:
    Cache
    Direct Known Subclasses:
    ConcurrentCache

    public abstract class AbstractCache
    extends Object
    implements Cache
    Abstract base class for managed Caches. This class uses atomic variables to track the current and maximum size of the cache, the cache access count and a possible CacheAccessListener instance.

    A subclass should call the protected recordCacheAccess() method whenever the cache is accessed (even cache misses should be reported). The subclass should also use the recordSizeChange(long) method to record all changes in the cache size, and automatically evict excess items when the isTooBig() method returns true.

    • Constructor Detail

      • AbstractCache

        public AbstractCache()
    • Method Detail

      • isTooBig

        protected boolean isTooBig()
        Checks whether the current estimate of the amount of memory used by this cache exceeds the allocated maximum amount of memory.
        Returns:
        true if the cache size is too big, false otherwise
      • recordSizeChange

        protected void recordSizeChange​(long delta)
        Updates the current memory use estimate of this cache.
        Parameters:
        delta - number of bytes added or removed
      • recordCacheAccess

        protected void recordCacheAccess()
        Records a single cache access and calls the configured CacheAccessListener (if any) whenever the constant access interval has passed since the previous listener call.
      • recordCacheMiss

        protected void recordCacheMiss()
      • getAccessCount

        public long getAccessCount()
        Description copied from interface: Cache
        Get the number of accesses (get or set) until resetAccessCount was called.
        Specified by:
        getAccessCount in interface Cache
        Returns:
        the count
      • resetAccessCount

        public void resetAccessCount()
        Description copied from interface: Cache
        Reset the access counter.
        Specified by:
        resetAccessCount in interface Cache
      • getTotalAccessCount

        public long getTotalAccessCount()
        Description copied from interface: Cache
        Get the total number of cache accesses.
        Specified by:
        getTotalAccessCount in interface Cache
        Returns:
        the number of hits
      • getMissCount

        public long getMissCount()
        Description copied from interface: Cache
        Get the number of cache misses.
        Specified by:
        getMissCount in interface Cache
        Returns:
        the number of misses
      • resetMissCount

        public void resetMissCount()
        Description copied from interface: Cache
        Reset the cache miss counter.
        Specified by:
        resetMissCount in interface Cache
      • getMemoryUsed

        public long getMemoryUsed()
        Description copied from interface: Cache
        Get the amount of used memory.
        Specified by:
        getMemoryUsed in interface Cache
        Returns:
        the size in bytes
      • getMaxMemorySize

        public long getMaxMemorySize()
        Description copied from interface: Cache
        Get the current limit.
        Specified by:
        getMaxMemorySize in interface Cache
        Returns:
        the size in bytes
      • setMaxMemorySize

        public void setMaxMemorySize​(long size)
        Description copied from interface: Cache
        Set the new memory limit.
        Specified by:
        setMaxMemorySize in interface Cache
        Parameters:
        size - the size in bytes
      • setAccessListener

        public void setAccessListener​(CacheAccessListener listener)
        Set the cache access listener. Only one listener per cache is supported.
        Specified by:
        setAccessListener in interface Cache
        Parameters:
        listener - the new listener
      • dispose

        public void dispose()
      • getCacheInfoAsString

        public String getCacheInfoAsString()
        Gathers the stats of the cache for logging.
        Specified by:
        getCacheInfoAsString in interface Cache