Class DatabaseCache


  • public class DatabaseCache
    extends Cache
    Generic class for caching into a database. It's constructor takes a Connection instance, which must be opened and closed by the client. SQL code used in this class should work with all common databases (which support varchar, timestamp and longvarchar datatypes).
    For more specialized versions of this class for different databases one may extend this class and override methods as needed.
    Author:
    Janni Kovacs
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.sql.Connection conn  
      protected static java.lang.String TABLE_NAME  
    • Constructor Summary

      Constructors 
      Constructor Description
      DatabaseCache​(java.sql.Connection connection)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the cache by effectively removing all cached data.
      boolean contains​(java.lang.String cacheEntryName)
      Checks if the cache contains an entry with the given name.
      protected void createTable()  
      boolean isExpired​(java.lang.String cacheEntryName)
      Checks if the specified entry is expired.
      java.io.InputStream load​(java.lang.String cacheEntryName)
      Loads the specified entry from the cache and returns an InputStream to be read from.
      void remove​(java.lang.String cacheEntryName)
      Removes the specified entry from the cache if available.
      void store​(java.lang.String cacheEntryName, java.io.InputStream inputStream, long expirationDate)
      Stores a request in the cache.
      • Methods inherited from class java.lang.Object

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

      • conn

        protected java.sql.Connection conn
    • Constructor Detail

      • DatabaseCache

        public DatabaseCache​(java.sql.Connection connection)
                      throws java.sql.SQLException
        Throws:
        java.sql.SQLException
    • Method Detail

      • createTable

        protected void createTable()
                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • contains

        public boolean contains​(java.lang.String cacheEntryName)
        Description copied from class: Cache
        Checks if the cache contains an entry with the given name.
        Specified by:
        contains in class Cache
        Parameters:
        cacheEntryName - An entry name
        Returns:
        true if the cache contains this entry
      • load

        public java.io.InputStream load​(java.lang.String cacheEntryName)
        Description copied from class: Cache
        Loads the specified entry from the cache and returns an InputStream to be read from. Returns null if the cache does not contain the specified cacheEntryName.
        Specified by:
        load in class Cache
        Parameters:
        cacheEntryName - An entry name
        Returns:
        an InputStream or null
      • remove

        public void remove​(java.lang.String cacheEntryName)
        Description copied from class: Cache
        Removes the specified entry from the cache if available. Does nothing if no such entry is available.
        Specified by:
        remove in class Cache
        Parameters:
        cacheEntryName - An entry name
      • store

        public void store​(java.lang.String cacheEntryName,
                          java.io.InputStream inputStream,
                          long expirationDate)
        Description copied from class: Cache
        Stores a request in the cache.
        Specified by:
        store in class Cache
        Parameters:
        cacheEntryName - The entry name to be stored to
        inputStream - An InputStream containing the data to be cached
        expirationDate - The date of expiration represented in milliseconds since 1.1.1970
      • isExpired

        public boolean isExpired​(java.lang.String cacheEntryName)
        Description copied from class: Cache
        Checks if the specified entry is expired.
        Specified by:
        isExpired in class Cache
        Parameters:
        cacheEntryName - An entry name
        Returns:
        true if the entry is expired
      • clear

        public void clear()
        Description copied from class: Cache
        Clears the cache by effectively removing all cached data.
        Specified by:
        clear in class Cache