Class Cache

java.lang.Object
overit.geocall.cache.Cache
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
CaffeineCache, RedisCache

public abstract class Cache extends Object implements AutoCloseable
Define an abstract class far a generic cache system capable of caching Entry objects.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Utility class that holds the information about a specific cache entry.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cache(String keyPrefix, Cache parent)
    Construct a generic cache, working with a som-prefix keys and possibly referring a parent cache
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    checkPrefix(String key, boolean exception)
     
    protected abstract boolean
    Clears all the cache
    void
    Close the cache and disconnects from the parent
    abstract <T, S> Item<T,S>
    fetch(Entry<T,S> entry)
    Extracts data from cache, or fetches and caches data from the parent cache.
    protected void
     
    abstract <T, S> Item<T,S>
    get(Entry<T,S> entry)
    Locally retrieve the data, without search in the parent cache if the entry has not been found.
    abstract List<Cache.Info>
    Return the information about the cache's entries related to the passed key.
     
    Return the parent cache from where fetch the entries that are not present into the current cache.
    protected final Cache
     
    protected final <T, S> void
    hold(Entry<T,S> entry)
    Insert the entry in the current cache and all depending caches
    final void
    Tells the cache that an entry is changed and should be invalidate in all the cache tree
    final void
    Tells the cache that all the entries matching the predicate are changed and should be invalidated in all the cache tree
    void
    Tells the cache that all the cache tree is invalid and must be cleared
    abstract Set<String>
    Returns the keys stored inside the cache
    protected final void
    Remove the entry from all the depending caches
    protected final void
    Remove all the entries from all the depending caches
    protected final void
    lose(String key)
    Remove the entry from the current cache and all depending caches
    protected final void
    Remove all the entries from the current cache and all depending caches
    void
    onInvalidation(String key, Runnable listener)
    Register the passed listener to the invalidation of the specific key.
    protected abstract <T, S> void
    put(Entry<T,S> entry)
    Locally stores an item inside the cache
    protected abstract boolean
    Locally removes a key from the cache
    final void
    setParent(Cache parent)
    Set the passed cache as parent of the current one.
    <T, S> void
    store(Entry<T,S> entry)
    tells the cache that an entry has a new value and must be propagated in all the cache tree

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Cache

      public Cache(String keyPrefix, Cache parent)
      Construct a generic cache, working with a som-prefix keys and possibly referring a parent cache
      Parameters:
      keyPrefix - the prefix af all the key cached
      parent - the optional parent cache
  • Method Details

    • getParent

      public Cache getParent()
      Return the parent cache from where fetch the entries that are not present into the current cache.
      Returns:
      the parent cache or null if there's no one.
    • setParent

      public final void setParent(Cache parent)
      Set the passed cache as parent of the current one. All the entries that are not loaded in the current cache, will be fetched from the parent one. If the current cache has no parent, the entries will be directly loaded.
      Parameters:
      parent - the cache's instance that will be used as parent of the current one.
    • close

      public void close()
      Close the cache and disconnects from the parent
      Specified by:
      close in interface AutoCloseable
    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • checkPrefix

      protected boolean checkPrefix(String key, boolean exception)
    • fetch

      public abstract <T, S> Item<T,S> fetch(Entry<T,S> entry)
      Extracts data from cache, or fetches and caches data from the parent cache. If the parent cache is missing it can use the Entry.load() to load the real data.
      Type Parameters:
      T - the type of the raw data contained in the cache item
      S - the type of the computed data contained in the cache item
      Parameters:
      entry - the cache's entry to look for
      Returns:
      the data contained in cache, on the parent cache, or potentially loaded and cached
    • remove

      protected abstract boolean remove(String key)
      Locally removes a key from the cache
      Parameters:
      key - the key of the entry to be removed
      Returns:
      true if the key has been removed, false if the key was absent
    • clear

      protected abstract boolean clear()
      Clears all the cache
      Returns:
      true if the cache has been cleaned, false if the cache was empty
    • put

      protected abstract <T, S> void put(Entry<T,S> entry)
      Locally stores an item inside the cache
      Type Parameters:
      T - the type of the raw data contained in the cache item
      S - the type of the computed data contained in the cache item
      Parameters:
      entry - the item to be stored
    • get

      public abstract <T, S> Item<T,S> get(Entry<T,S> entry)
      Locally retrieve the data, without search in the parent cache if the entry has not been found.
      Type Parameters:
      T - the type of the raw data contained in the cache item
      S - the type of the computed data contained in the cache item
      Parameters:
      entry - the cache's entry to look for
      Returns:
      the data contained in cache or null if not present
    • keys

      public abstract Set<String> keys()
      Returns the keys stored inside the cache
      Returns:
      the keys stored inside the cache or an empty set if there's none.
    • getInfo

      public abstract List<Cache.Info> getInfo(String key)
      Return the information about the cache's entries related to the passed key. Each element contains the information about a specific view for that entry.
      Parameters:
      key - the key of the entry
      Returns:
      the list containing the information, or an empty list the there's no information related to the passed key
    • invalidate

      public final void invalidate(String key)
      Tells the cache that an entry is changed and should be invalidate in all the cache tree
      Parameters:
      key - the name of the entry to be invalidate
    • invalidate

      public final void invalidate(Predicate<String> predicate)
      Tells the cache that all the entries matching the predicate are changed and should be invalidated in all the cache tree
      Parameters:
      predicate - the predicate to match the keys to be invalidated
    • invalidateAll

      public void invalidateAll()
      Tells the cache that all the cache tree is invalid and must be cleared
    • onInvalidation

      public void onInvalidation(String key, Runnable listener)
      Register the passed listener to the invalidation of the specific key. When the cache's entry, with the specific key, will be invalidated, the listener function will be called.
      Parameters:
      key - the cache key to listen to
      listener - the runnable task that will be called when the key will be invalidated
    • getKeyPrefix

      public String getKeyPrefix()
      Returns:
      the prefix for all the keys admitted inside the cache
    • getRoot

      protected final Cache getRoot()
      Returns:
      the instance root cache. If the current cache doesn't belong to any caches tree, it returns the current instance.
    • store

      public <T, S> void store(Entry<T,S> entry)
      tells the cache that an entry has a new value and must be propagated in all the cache tree
      Type Parameters:
      T - the type of the raw data contained in the cache item
      S - the type of the computed data contained in the cache item
      Parameters:
      entry - the entry to be stored
    • lose

      protected final void lose(String key)
      Remove the entry from the current cache and all depending caches
      Parameters:
      key - the name of the entry to be removed
    • loosed

      protected final void loosed(String key)
      Remove the entry from all the depending caches
      Parameters:
      key - the name of the entry to be removed
    • loseAll

      protected final void loseAll()
      Remove all the entries from the current cache and all depending caches
    • loosedAll

      protected final void loosedAll()
      Remove all the entries from all the depending caches
    • hold

      protected final <T, S> void hold(Entry<T,S> entry)
      Insert the entry in the current cache and all depending caches
      Type Parameters:
      T - the type of the raw data contained in the cache item
      S - the type of the computed data contained in the cache item
      Parameters:
      entry - the entry to be inserted