Interface Cache<Q,V>

Type Parameters:
Q - The type of query used to request data from the cache.
V - The type of value stored in the cache.
All Known Implementing Classes:
AbstractCache, AbstractFileCache

public interface Cache<Q,V>
A cache that can fetch information when needed.

A cached value is retrieved using a query. The value is stored in the cache as part of the cache data.

Author:
Garret Wilson
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Class for storing a value along with its expiration information and other information.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a listener to listen for a value being fetched.
    get(Q query)
    Retrieves a value from the cache.
    get(Q query, boolean deferFetch)
    Retrieves a value from the cache.
    getData(Q query)
    Retrieves data from the cache.
    getData(Q query, boolean deferFetch)
    Retrieves data from the cache.
    long
     
    boolean
    isCached(Q query)
    Determined if a non-stale value is in the cache.
    boolean
     
    void
    Removes a listener to listen for a value being fetched.
    void
    setExpiration(long expiration)
    Sets the life of an object in the cache.
    void
    setFetchSynchronous(boolean fetchSynchronous)
    Sets whether fetching new values is synchronous.
    uncache(Q query)
    Removes a value from the cache.
  • Method Details

    • isFetchSynchronous

      boolean isFetchSynchronous()
      Returns:
      Whether fetching new values is synchronous.
    • setFetchSynchronous

      void setFetchSynchronous(boolean fetchSynchronous)
      Sets whether fetching new values is synchronous. Changing this value does not change the sychronicity of fetches that have already started but have not yet completed.
      Parameters:
      fetchSynchronous - Whether fetches for new values should occur synchronously.
    • getExpiration

      long getExpiration()
      Returns:
      The life of an object in the cache, in milliseconds.
    • setExpiration

      void setExpiration(long expiration)
      Sets the life of an object in the cache.
      Parameters:
      expiration - The length of time, in milliseconds, to keep cached information.
    • addCacheFetchListener

      void addCacheFetchListener(Q key, CacheFetchListener<Q,V> listener)
      Adds a listener to listen for a value being fetched.
      Parameters:
      key - The query for requesting a value from the cache.
      listener - The listener to be notified when the value is fetched.
    • removeCacheFetchListener

      void removeCacheFetchListener(Q key, CacheFetchListener<Q,V> listener)
      Removes a listener to listen for a value being fetched.
      Parameters:
      key - The query for requesting a value from the cache.
      listener - The listener to be notified when the value is fetched.
    • isCached

      boolean isCached(Q query) throws IOException
      Determined if a non-stale value is in the cache.
      Parameters:
      query - The query for requesting a value from the cache.
      Returns:
      Whether the value associated with the given query is in the cache and not stale.
      Throws:
      IOException - if there was an error checking the cached information for staleness.
    • get

      V get(Q query) throws IOException
      Retrieves a value from the cache. Values are fetched from the backing store if needed, and this method blocks until the data is fetched.
      Parameters:
      query - The query for requesting a value from the cache.
      Returns:
      The cached value.
      Throws:
      IOException - if there was an error fetching the value from the backing store.
    • get

      V get(Q query, boolean deferFetch) throws IOException
      Retrieves a value from the cache. Values are fetched from the backing store if needed, with fetching optionally deferred until later.
      Parameters:
      query - The query for requesting a value from the cache.
      deferFetch - Whether fetching, if needed, should be deffered and performed in an asynchronous thread.
      Returns:
      The cached value, or null if fetching was deferred.
      Throws:
      IOException - if there was an error fetching the value from the backing store.
    • getData

      Cache.Data<V> getData(Q query) throws IOException
      Retrieves data from the cache. Data is fetched from the backing store if needed, and this method blocks until the data is fetched.
      Parameters:
      query - The query for requesting data from the cache.
      Returns:
      The cached data.
      Throws:
      IOException - if there was an error fetching the data from the backing store.
    • getData

      Cache.Data<V> getData(Q query, boolean deferFetch) throws IOException
      Retrieves data from the cache. Data is fetched from the backing store if needed, with fetching optionally deferred until later.
      Parameters:
      query - The query for requesting data from the cache.
      deferFetch - Whether fetching, if needed, should be deffered and performed in an asynchronous thread.
      Returns:
      The cached data, or null if fetching was deferred.
      Throws:
      IOException - if there was an error fetching the value from the backing store.
    • uncache

      V uncache(Q query) throws IOException
      Removes a value from the cache.
      Parameters:
      query - The query for requesting a value from the cache.
      Returns:
      The previously cached value, even if stale, or null if there was no cached value.
      Throws:
      IOException - if there was an error removing the value from the cache.