de.umass.lastfm.cache
Class DatabaseCache

java.lang.Object
  extended byde.umass.lastfm.cache.Cache
      extended byde.umass.lastfm.cache.DatabaseCache

public class DatabaseCache
extends Cache

Generic class for caching into a database. Its 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 the varchar, timestamp and text datatypes).

For more specialized versions of this class for different databases one may extend this class and override methods as needed. In most cases overriding createTable() will be sufficient.
The following databases are supported and tested with this class: Not supported by this base class:

Author:
Janni Kovacs

Field Summary
protected  Connection connection
           
protected static String DEFAULT_TABLE_NAME
           
protected  String tableName
           
 
Constructor Summary
DatabaseCache(Connection connection)
           
DatabaseCache(Connection connection, String tableName)
          Creates a new DatabaseCache with the supplied database Connection and the specified table name.
 
Method Summary
 void clear()
          Clears the cache by effectively removing all cached data.
 boolean contains(String cacheEntryName)
          Checks if the cache contains an entry with the given name.
protected  void createTable()
          This internal method creates a new table in the database for storing XML responses.
 boolean isExpired(String cacheEntryName)
          Checks if the specified entry is expired.
 InputStream load(String cacheEntryName)
          Loads the specified entry from the cache and returns an InputStream to be read from.
 void remove(String cacheEntryName)
          Removes the specified entry from the cache if available.
 void store(String cacheEntryName, InputStream inputStream, long expirationDate)
          Stores a request in the cache.
 
Methods inherited from class de.umass.lastfm.cache.Cache
createCacheEntryName, getExpirationPolicy, setExpirationPolicy, setHashCacheEntryNames
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TABLE_NAME

protected static final String DEFAULT_TABLE_NAME
See Also:
Constant Field Values

tableName

protected String tableName

connection

protected Connection connection
Constructor Detail

DatabaseCache

public DatabaseCache(Connection connection)
              throws SQLException

DatabaseCache

public DatabaseCache(Connection connection,
                     String tableName)
              throws SQLException
Creates a new DatabaseCache with the supplied database Connection and the specified table name. A new table with tableName will be created in the constructor, if none exists. Note that tableName will not be sanitized for usage in SQL, so in the rare case you're using user input for a table name make sure to sanitize the input before passing it on to prevent SQL injections.

Parameters:
connection - The database connection
tableName - the name for the database table to use
Throws:
SQLException - When initializing/creating the table fails
See Also:
createTable()
Method Detail

createTable

protected void createTable()
                    throws SQLException
This internal method creates a new table in the database for storing XML responses. You can override this method in a subclass if this generic method does not work with the database server you are using, given that the following table columns are present: If you choose to use a different schema in your table you'll most likely have to override the other methods in this class, too.

Throws:
SQLException - When the generic SQL code in this method is not compatible with the database

contains

public boolean contains(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 InputStream load(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(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(String cacheEntryName,
                  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(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