Class Cache<T extends Cacheable>

java.lang.Object
org.deltava.util.cache.Cache<T>
Type Parameters:
T - the cacheable object type
Direct Known Subclasses:
AgingCache, ExpiringCache, JedisCache, NullCache

public abstract class Cache<T extends Cacheable> extends Object
An an abstract class to store common cache operations. These caches can store null entries to prevent repeated uncached calls for invalid keys.
Since:
1.0
Version:
11.3
Author:
Luke
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final ConcurrentMap<Object, CacheEntry<T>>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Cache(int maxSize)
    Initializes the cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    add(T entry)
    Adds an entry to the cache.
    void
    addAll(Collection<? extends T> entries)
    Adds a number of entries to the cache.
    protected abstract void
    addEntry(T entry)
    Adds an entry to the cache.
    final void
    Adds a null entry to the cache.
    protected abstract void
    Adds a null entry to the cache.
    protected void
    Automatically resizes the cache in the case of an overflow.
    void
    Clears the cache.
    boolean
    Returns if the cache contains a particular cache key.
    abstract T
    get(Object key)
    Retrieves an entry from the cache.
    getAll(Collection<?> keys)
    Returns multiple objects from the cache.
    long
    Returns the total number of cache errors.
    final long
    Returns the total number of cache hits.
    int
    Returns the maximum size of the cache.
    final long
    Returns the total number of cache requests.
    protected void
    hit()
    Log a cache hit.
    boolean
    Returns if the cache is a remote cache.
    void
    Invalidate a cache entry.
    protected void
    Log a cache request.
    final void
    setMaxSize(int size)
    Sets the maximum size of the cache.
    int
    Returns the current size of the cache.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Cache

      protected Cache(int maxSize)
      Initializes the cache.
      Parameters:
      maxSize - the maximum size of the cache
  • Method Details

    • addAll

      public void addAll(Collection<? extends T> entries)
      Adds a number of entries to the cache.
      Parameters:
      entries - a Collection of Cacheable entries
    • contains

      public boolean contains(Object key)
      Returns if the cache contains a particular cache key.
      Parameters:
      key - the cache key
      Returns:
      TRUE if the cache contains the key, otherwise FALSE
    • clear

      public void clear()
      Clears the cache.
    • remove

      public void remove(Object key)
      Invalidate a cache entry.
      Parameters:
      key - the entry key
    • size

      public int size()
      Returns the current size of the cache.
      Returns:
      the number of entries in the cache
    • getMaxSize

      public int getMaxSize()
      Returns the maximum size of the cache.
      Returns:
      the maximum number of entries in the cache
    • setMaxSize

      public final void setMaxSize(int size)
      Sets the maximum size of the cache.
      Parameters:
      size - the maximum number of entries
    • checkOverflow

      protected void checkOverflow()
      Automatically resizes the cache in the case of an overflow. This is done by sorting the cache entries using their natural order, and removing the first entry.
    • hit

      protected void hit()
      Log a cache hit. Implementations should call this method from their get(Object) method to keep statistics.
      See Also:
    • request

      protected void request()
      Log a cache request. Implementations should call this method from their get(Object) method to keep statistics.
      See Also:
    • getHits

      public final long getHits()
      Returns the total number of cache hits.
      Returns:
      the number of hits
      See Also:
    • getRequests

      public final long getRequests()
      Returns the total number of cache requests.
      Returns:
      the number of requests
      See Also:
    • getErrors

      public long getErrors()
      Returns the total number of cache errors.
      Returns:
      the number of errors
    • isRemote

      public boolean isRemote()
      Returns if the cache is a remote cache.
      Returns:
      TRUE if remote, otherwise FALSE
    • addEntry

      protected abstract void addEntry(T entry)
      Adds an entry to the cache. Subclasses need to implement this method to handle cache additions, the public method forces an overflow check.
      Parameters:
      entry - the entry to add
    • addNullEntry

      protected abstract void addNullEntry(Object key)
      Adds a null entry to the cache. Subclasses need to implement this method to handle cache additions, the public method forces an overflow check.
      Parameters:
      key - the entry key
    • add

      public final void add(T entry)
      Adds an entry to the cache.
      Parameters:
      entry - the entry to add
    • addNull

      public final void addNull(String key)
      Adds a null entry to the cache.
      Parameters:
      key - the entry key
    • get

      public abstract T get(Object key)
      Retrieves an entry from the cache.
      Parameters:
      key - the cache key
      Returns:
      the cache entry, or null if the key is not present or the entry is invalid
    • getAll

      public Map<Object,T> getAll(Collection<?> keys)
      Returns multiple objects from the cache.
      Parameters:
      keys - the cache keys
      Returns:
      a Map of results keyed by cache key