Package de.umass.lastfm.cache
Class Cache
java.lang.Object
de.umass.lastfm.cache.Cache
- Direct Known Subclasses:
DatabaseCache
,FileSystemCache
,MemoryCache
The
Cache
handles storing and loading to a permanent storage for last.fm api requests. This could be
a file system or a sql database.- Author:
- Janni Kovacs
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
clear()
Clears the cache by effectively removing all cached data.abstract boolean
Checks if the cache contains an entry with the given name.static String
createCacheEntryName
(String method, Map<String, String> params) Creates a unique entry name string for a request.Returns the activeExpirationPolicy
abstract boolean
Checks if the specified entry is expired.abstract InputStream
Loads the specified entry from the cache and returns an InputStream to be read from.abstract void
Removes the specified entry from the cache if available.void
setExpirationPolicy
(ExpirationPolicy expirationPolicy) Sets the activeExpirationPolicy
.static void
setHashCacheEntryNames
(boolean hashCacheEntryNames) IfhashCacheEntryNames
is set to true thecreateCacheEntryName(java.lang.String, java.util.Map<java.lang.String, java.lang.String>)
method will return a hash of the original entry name instead of the name itself.abstract void
store
(String cacheEntryName, InputStream inputStream, long expirationDate) Stores a request in the cache.
-
Constructor Details
-
Cache
protected Cache()
-
-
Method Details
-
getExpirationPolicy
Returns the activeExpirationPolicy
- Returns:
- the ExpirationPolicy
-
setExpirationPolicy
Sets the activeExpirationPolicy
.- Parameters:
expirationPolicy
- An ExpirationPolicy, notnull
-
contains
Checks if the cache contains an entry with the given name.- Parameters:
cacheEntryName
- An entry name- Returns:
true
if the cache contains this entry
-
load
Loads the specified entry from the cache and returns an InputStream to be read from. Returnsnull
if the cache does not contain the specified cacheEntryName.- Parameters:
cacheEntryName
- An entry name- Returns:
- an InputStream or
null
-
remove
Removes the specified entry from the cache if available. Does nothing if no such entry is available.- Parameters:
cacheEntryName
- An entry name
-
store
Stores a request in the cache.- Parameters:
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.1970
-
isExpired
Checks if the specified entry is expired.- Parameters:
cacheEntryName
- An entry name- Returns:
true
if the entry is expired
-
clear
public abstract void clear()Clears the cache by effectively removing all cached data. -
createCacheEntryName
Creates a unique entry name string for a request. It consists of the method name and all the parameter names and values concatenated in alphabetical order. It is used to identify cache entries in the backend storage. IfhashCacheEntryNames
is set totrue
this method will return a MD5 hash of the generated name.- Parameters:
method
- The request methodparams
- The request parameters- Returns:
- a cache entry name
-
setHashCacheEntryNames
public static void setHashCacheEntryNames(boolean hashCacheEntryNames) IfhashCacheEntryNames
is set to true thecreateCacheEntryName(java.lang.String, java.util.Map<java.lang.String, java.lang.String>)
method will return a hash of the original entry name instead of the name itself.- Parameters:
hashCacheEntryNames
-true
to generate hashes
-