Class ExpiringCache<T extends Cacheable>

java.lang.Object
org.deltava.util.cache.Cache<T>
org.deltava.util.cache.ExpiringCache<T>
Type Parameters:
T - the Cacheable object type
Direct Known Subclasses:
ExpiringGeoCache

public class ExpiringCache<T extends Cacheable> extends Cache<T>
An object cache that supports expiration dates.
Since:
1.0
Version:
11.0
Author:
Luke
  • Field Details

    • _lastCreationTime

      protected long _lastCreationTime
    • _expiry

      protected int _expiry
  • Constructor Details

    • ExpiringCache

      public ExpiringCache(int maxSize, int expiryTime)
      Creates a new Cache.
      Parameters:
      maxSize - the maximum number of entries
      expiryTime - the expiration time in seconds
      Throws:
      IllegalArgumentException - if maxSize or expiryTime are zero or negative
      See Also:
  • Method Details

    • setExpiration

      public void setExpiration(int expiry)
      Sets the cache's expiration interval.
      Parameters:
      expiry - the expiration interval in seconds
    • get

      public T get(Object key)
      Returns an unexpired entry from the cache.
      Specified by:
      get in class Cache<T extends Cacheable>
      Parameters:
      key - the cache key
      Returns:
      the cache entry, or null if not present or expired
      See Also:
    • contains

      public boolean contains(Object key)
      Returns if the cache contains a particular cache key and the key has not expired.
      Overrides:
      contains in class Cache<T extends Cacheable>
      Parameters:
      key - the cache key
      Returns:
      TRUE if the cache contains the key, otherwise FALSE
    • get

      public T get(Object key, boolean ifExpired)
      Returns an entry from the cache.
      Parameters:
      key - the cache key
      ifExpired - TRUE if expired entries can be returned, otherwise FALSE
      Returns:
      the cache entry, or null if not present
      See Also:
    • isExpired

      public boolean isExpired(Object key)
      Queries the cache to determine if an object has expired
      Parameters:
      key - the cache key
      Returns:
      TRUE if the object is present and expired, otherwise FALSE
    • addEntry

      protected void addEntry(T obj)
      Adds an entry to the cache. If this operation would cause the cache to exceed its maximum size, then the entry with the earliest expiration date will be removed.
      Specified by:
      addEntry in class Cache<T extends Cacheable>
      Parameters:
      obj - the entry to add to the cache
    • addNullEntry

      protected void addNullEntry(Object key)
      Adds a null entry to the cache.
      Specified by:
      addNullEntry in class Cache<T extends Cacheable>
      Parameters:
      key - the entry key
    • checkOverflow

      protected void checkOverflow()
      Purges expired entries from the cache.
      Overrides:
      checkOverflow in class Cache<T extends Cacheable>