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
    • Method Detail

      • 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 java.io.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:
        java.io.IOException - if there was an error checking the cached information for staleness.
      • get

        V get​(Q query)
        throws java.io.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:
        java.io.IOException - if there was an error fetching the value from the backing store.
      • get

        V get​(Q query,
              boolean deferFetch)
        throws java.io.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:
        java.io.IOException - if there was an error fetching the value from the backing store.
      • getData

        Cache.Data<V> getData​(Q query)
                       throws java.io.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:
        java.io.IOException - if there was an error fetching the data from the backing store.
      • getData

        Cache.Data<V> getData​(Q query,
                              boolean deferFetch)
                       throws java.io.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:
        java.io.IOException - if there was an error fetching the value from the backing store.
      • uncache

        V uncache​(Q query)
           throws java.io.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:
        java.io.IOException - if there was an error removing the value from the cache.