Class LruMemoryStore

  • All Implemented Interfaces:
    Store

    public class LruMemoryStore
    extends AbstractStore
    An implementation of a LruMemoryStore.

    This uses LinkedHashMap as its backing map. It uses the LinkedHashMap LRU feature. LRU for this implementation means least recently accessed.

    Version:
    $Id$
    Author:
    Greg Luck
    • Field Detail

      • cache

        protected Ehcache cache
        The cache this store is associated with.
      • map

        protected java.util.Map map
        Map where items are stored by key.
      • diskStore

        protected final Store diskStore
        The DiskStore associated with this MemoryStore.
      • status

        protected Status status
        status.
      • maximumSize

        protected long maximumSize
        The maximum size of the store (0 == no limit)
    • Constructor Detail

      • LruMemoryStore

        public LruMemoryStore​(Ehcache cache,
                              Store diskStore)
        Constructor for the LruMemoryStore object The backing LinkedHashMap is created with LRU by access order.
    • Method Detail

      • putWithWriter

        public final boolean putWithWriter​(Element element,
                                           CacheWriterManager writerManager)
                                    throws CacheException
        Puts an item into the store and the cache writer manager in an atomic operation
        Returns:
        true if this is a new put for the key or element is null. Returns false if it was an update.
        Throws:
        CacheException
      • doPut

        protected void doPut​(Element element)
                      throws CacheException
        Allow specialised actions over adding the element to the map.
        Parameters:
        element -
        Throws:
        CacheException
      • get

        public final Element get​(java.lang.Object key)
        Gets an item from the cache.

        The last access time in Element is updated.

        Parameters:
        key - the cache key
        Returns:
        the element, or null if there was no match for the key
      • getQuiet

        public final Element getQuiet​(java.lang.Object key)
        Gets an item from the cache, without updating statistics.
        Parameters:
        key - the cache key
        Returns:
        the element, or null if there was no match for the key
      • remove

        public final Element remove​(java.lang.Object key)
        Removes an Element from the store.
        Parameters:
        key - the key of the Element, usually a String
        Returns:
        the Element if one was found, else null
      • clear

        protected final void clear()
        Clears any data structures and places it back to its state when it was first created.
      • dispose

        public final void dispose()
        Prepares for shutdown.
      • flush

        public final void flush()
        Flush to disk only if the cache is diskPersistent.
      • spoolAllToDisk

        protected final void spoolAllToDisk()
        Spools all elements to disk, in preparation for shutdown.

        This revised implementation is a little slower but avoids using increased memory during the method.

      • spoolToDisk

        protected void spoolToDisk​(Element element)
        Puts the element in the DiskStore. Should only be called if isOverflowToDisk is true

        Relies on being called from a synchronized method

        Parameters:
        element - The Element
      • getStatus

        public final Status getStatus()
        Gets the status of the MemoryStore.
      • getKeys

        public final java.util.List getKeys()
        Gets an Array of the keys for all elements in the memory cache.

        Does not check for expired entries

        Returns:
        An Object[]
      • getSize

        public final int getSize()
        Returns the current cache size.
        Returns:
        The size value
      • getTerracottaClusteredSize

        public final int getTerracottaClusteredSize()
        Returns nothing since a disk store isn't clustered
        Returns:
        returns 0
      • containsKey

        public final boolean containsKey​(java.lang.Object key)
        An unsynchronized check to see if a key is in the Store. No check is made to see if the Element is expired.
        Parameters:
        key - The Element key
        Returns:
        true if found. If this method return false, it means that an Element with the given key is definitely not in the MemoryStore. If it returns true, there is an Element there. An attempt to get it may return null if the Element has expired.
      • getSizeInBytes

        public final long getSizeInBytes()
                                  throws CacheException
        Measures the size of the memory store by using the sizeof engine.

        Warning: This method can be very expensive to run. Allow approximately 1 second per 1MB of entries. Running this method could create liveness problems because the object lock is held for a long period

        Returns:
        the size, in bytes
        Throws:
        CacheException
      • evict

        protected final void evict​(Element element)
                            throws CacheException
        Evict the Element.

        Evict means that the Element is:

        • if, the store is diskPersistent, the Element is spooled to the DiskStore
        • if not, the Element is removed.
        Parameters:
        element - the Element to be evicted.
        Throws:
        CacheException
      • notifyExpiry

        protected final void notifyExpiry​(Element element)
        Before eviction elements are checked.
        Parameters:
        element -
      • isFull

        protected final boolean isFull()
        An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.
      • expireElements

        public void expireElements()
        Expire all elsments.

        This is a default implementation which does nothing. Expiry on demand is only implemented for disk stores.

      • bufferFull

        public boolean bufferFull()
        Memory stores are never backed up and always return false
        Returns:
        true if the store write buffer is backed up.
      • getMBean

        public java.lang.Object getMBean()
        Optional implementation specific MBean exposed by the store.
        Returns:
        implementation specific management bean
      • getEvictionPolicy

        public Policy getEvictionPolicy()
        Returns:
        the current eviction policy. This may not be the configured policy, if it has been dynamically set.
        See Also:
        setEvictionPolicy(Policy)
      • setEvictionPolicy

        public void setEvictionPolicy​(Policy policy)
        Sets the eviction policy strategy. The Store will use a policy at startup. The store may allow changing the eviction policy strategy dynamically. Otherwise implementations will throw an exception if this method is called.
        Parameters:
        policy - the new policy
      • getInternalContext

        public java.lang.Object getInternalContext()
        This should not be used, and will generally return null
        Returns:
        some internal context (probably null)
      • containsKeyInMemory

        public boolean containsKeyInMemory​(java.lang.Object key)
        A check to see if a key is in the Store and is currently held in memory.
        Parameters:
        key - The Element key
        Returns:
        true if found. No check is made to see if the Element is expired.
      • containsKeyOffHeap

        public boolean containsKeyOffHeap​(java.lang.Object key)
        A check to see if a key is in the Store and is currently held off-heap.
        Parameters:
        key - The Element key
        Returns:
        true if found. No check is made to see if the Element is expired.
      • containsKeyOnDisk

        public boolean containsKeyOnDisk​(java.lang.Object key)
        A check to see if a key is in the Store and is currently held on disk.
        Parameters:
        key - The Element key
        Returns:
        true if found. No check is made to see if the Element is expired.
      • getInMemoryEvictionPolicy

        public Policy getInMemoryEvictionPolicy()
        Returns:
        the current eviction policy. This may not be the configured policy, if it has been dynamically set.
        See Also:
        Store.setInMemoryEvictionPolicy(Policy)
      • getInMemorySize

        public int getInMemorySize()
        Returns the current local in-memory store size
        Returns:
        the count of the Elements in the Store and in-memory on the local machine
      • getInMemorySizeInBytes

        public long getInMemorySizeInBytes()
        Gets the size of the in-memory portion of the store, in bytes.

        This method may be expensive to run, depending on implementation. Implementers may choose to return an approximate size.

        Returns:
        the approximate in-memory size of the store in bytes
      • getOffHeapSize

        public int getOffHeapSize()
        Returns the current local off-heap store size
        Returns:
        the count of the Elements in the Store and off-heap on the local machine
      • getOffHeapSizeInBytes

        public long getOffHeapSizeInBytes()
        Gets the size of the off-heap portion of the store, in bytes.
        Returns:
        the approximate off-heap size of the store in bytes
      • getOnDiskSize

        public int getOnDiskSize()
        Returns the current local on-disk store size
        Returns:
        the count of the Elements in the Store and on-disk on the local machine
      • getOnDiskSizeInBytes

        public long getOnDiskSizeInBytes()
        Gets the size of the on-disk portion of the store, in bytes.
        Returns:
        the on-disk size of the store in bytes
      • setInMemoryEvictionPolicy

        public void setInMemoryEvictionPolicy​(Policy policy)
        Sets the eviction policy strategy. The Store will use a policy at startup. The store may allow changing the eviction policy strategy dynamically. Otherwise implementations will throw an exception if this method is called.
        Parameters:
        policy - the new policy
      • putIfAbsent

        public Element putIfAbsent​(Element element)
                            throws java.lang.NullPointerException
        Unsupported in LruMemoryStore
        Parameters:
        element - element to be added
        Returns:
        the element previously cached for this key, or null if none.
        Throws:
        java.lang.NullPointerException - if the element is null, or has a null key
      • removeElement

        public Element removeElement​(Element element,
                                     ElementValueComparator comparator)
                              throws java.lang.NullPointerException
        Unsupported in LruMemoryStore
        Parameters:
        element - Element to be removed
        comparator - ElementValueComparator to use to compare elements
        Returns:
        the Element removed or null if no Element was removed
        Throws:
        java.lang.NullPointerException - if the element is null, or has a null key
      • replace

        public boolean replace​(Element old,
                               Element element,
                               ElementValueComparator comparator)
                        throws java.lang.NullPointerException,
                               java.lang.IllegalArgumentException
        Unsupported in LruMemoryStore
        Parameters:
        old - Element to be test against
        element - Element to be cached
        comparator - ElementValueComparator to use to compare elements
        Returns:
        true is the Element was replaced
        Throws:
        java.lang.NullPointerException - if the either Element is null or has a null key
        java.lang.IllegalArgumentException - if the two Element keys are non-null but not equal
      • replace

        public Element replace​(Element element)
                        throws java.lang.NullPointerException
        Unsupported in LruMemoryStore
        Parameters:
        element - Element to be cached
        Returns:
        the Element previously cached for this key, or null if no Element was cached
        Throws:
        java.lang.NullPointerException - if the Element is null or has a null key