|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.umass.lastfm.cache.Cache
de.umass.lastfm.cache.DatabaseCache
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).
createTable()
will be sufficient.
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 |
protected static final String DEFAULT_TABLE_NAME
protected String tableName
protected Connection connection
Constructor Detail |
public DatabaseCache(Connection connection) throws SQLException
public DatabaseCache(Connection connection, String tableName) throws SQLException
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.
connection
- The database connectiontableName
- the name for the database table to use
SQLException
- When initializing/creating the table failscreateTable()
Method Detail |
protected void createTable() throws SQLException
id
- The primary key, which is used to identify cache entries (see Cache.createCacheEntryName(java.lang.String, java.util.Map)
expiration_date
- A timestamp field for this cache entry's expiration dateresponse
- The actual response XML
SQLException
- When the generic SQL code in this method is not compatible with the databasepublic boolean contains(String cacheEntryName)
Cache
contains
in class Cache
cacheEntryName
- An entry name
true
if the cache contains this entrypublic InputStream load(String cacheEntryName)
Cache
null
if the cache does not contain the specified cacheEntryName.
load
in class Cache
cacheEntryName
- An entry name
null
public void remove(String cacheEntryName)
Cache
remove
in class Cache
cacheEntryName
- An entry namepublic void store(String cacheEntryName, InputStream inputStream, long expirationDate)
Cache
store
in class Cache
cacheEntryName
- The entry name to be stored toinputStream
- An InputStream containing the data to be cachedexpirationDate
- The date of expiration represented in milliseconds since 1.1.1970public boolean isExpired(String cacheEntryName)
Cache
isExpired
in class Cache
cacheEntryName
- An entry name
true
if the entry is expiredpublic void clear()
Cache
clear
in class Cache
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |