|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.util.StaticCache
public class StaticCache
A cache for storing data to files on the file system. The typical
use case is to store data this is expensive to get from the database
in a file for later retrieval. The cache can be used in streaming mode
with the any of the read(String, int)
or write(String, InputStream, int)
methods and their variants. It can also be used to store any Serializable
object with store(String, Object, int)
and load(String, int)
.
In all cases the cached entry is identified by a key which is more or less directly translated to directories on the file system.
This class is thread-safe and can be used by multiple threads at the same time. Write requests to the same entry in the cache are allowed to one thread at a time. Any number of threads may read from the same cache entry as long as no thread is writing to that entry.
Nested Class Summary | |
---|---|
static class |
StaticCache.CleanupTask
A timer task that clean up the cache when it is executed. |
(package private) static class |
StaticCache.LockEntry
Keeps track of a locked cached entry. |
(package private) static class |
StaticCache.LockSafeInputStream
A lock-safe input stream that releases the associated read lock when the stream is closed. |
(package private) static class |
StaticCache.LockSafeOutputStream
A lock-safe output stream that releases the associated write lock when the stream is closed. |
Field Summary | |
---|---|
private boolean |
disabled
|
private Map<String,StaticCache.LockEntry> |
locks
|
private static Logger |
log
Log static cache events. |
private File |
root
|
static Pattern |
validKey
|
Constructor Summary | |
---|---|
StaticCache(File root)
Creates a new static cache. |
Method Summary | |
---|---|
private StaticCache.LockEntry |
aquireLock(String key,
boolean writeLock,
int timeout)
Aquire a read or write lock on a given cache entry. |
void |
cleanUp(FileFilter filter)
Remove all files that matches the specified filter from the cache. |
TimerTask |
cleanUpTask(FileFilter filter)
Creates a task that cleans up this cache when it is executed. |
boolean |
delete(String key,
int timeout)
Deletes an object from the cache. |
boolean |
exists(String key)
Checks if a cache entry exists or not. |
private InputStream |
getInputStream(String key,
int timeout)
Get a lock-safe input stream. |
private OutputStream |
getOutputStream(String key,
int timeout)
Get a lock-safe output stream. |
boolean |
isDisabled()
Check if the cache has been disabled. |
static boolean |
isValidKey(String key)
Checks if the given key is a vilid cache entry key. |
Object |
load(String key,
int timeout)
Read a serializable object from the cache. |
static String |
makeValidKey(String key,
String replacement)
Convert a possible invalid cache key to a valid one by replacing invalid characters with the replacement string. |
FileFilter |
olderThan(long age)
Creates a file filter that matches all files that are older than the specified age. |
InputStream |
read(String key,
int timeout)
Get an input stream for reading binary information from the cache. |
long |
read(String key,
OutputStream out,
int timeout)
Read binary information from the cache. |
void |
setDisabled(boolean disabled)
Disable or enable the cache. |
long |
size(String key)
Checks the size (in bytes) of a cache entry. |
boolean |
store(String key,
Object object,
int timeout)
Store a serializable object in the cache. |
private void |
validateKey(String key)
|
long |
write(String key,
InputStream in,
int timeout)
Store binary information in the cache. |
OutputStream |
write(String key,
int timeout)
Get an output stream that can be used to write binary information to the cache. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Pattern validKey
private static final Logger log
private final File root
private final Map<String,StaticCache.LockEntry> locks
private boolean disabled
Constructor Detail |
---|
public StaticCache(File root)
root
- A directory were the cache stores it's filesMethod Detail |
---|
public static boolean isValidKey(String key)
key
- The key to check
public static String makeValidKey(String key, String replacement)
key
- The (possible invalid) cache keyreplacement
- The replacement string
public boolean isDisabled()
public void setDisabled(boolean disabled)
disabled
- TRUE if the cache should be disabledpublic void cleanUp(FileFilter filter)
filter
- A file filter that matches the files to
removepublic FileFilter olderThan(long age)
age
- The age in milliseconds
OlderThanFileFilter
public TimerTask cleanUpTask(FileFilter filter)
filter
- The file filter that determines
which file that should be deleted
cleanUp(FileFilter)
public boolean exists(String key)
key
- The cache key
public long size(String key)
key
- The cache key
public long write(String key, InputStream in, int timeout) throws IOException
key
- The cache keyin
- An input stream to read fromtimeout
- A timeout in milliseconds to wait for a write lock
on the requested cache entry
IOException
public OutputStream write(String key, int timeout) throws IOException
NOTE! It is very important that the caller closes the output stream as soon as all data has been written to it. Failure to do so may result in locking the cache entry from reading by other threads.
key
- The cache keytimeout
- A timeout in milliseconds to wait for a write lock
on the requested cache entry
IOException
public long read(String key, OutputStream out, int timeout) throws IOException
key
- The cache keyout
- An output stream to write the cache contents totimeout
- A timeout in milliseconds to wait for a
read lock on the requested cache entry
IOException
public InputStream read(String key, int timeout) throws IOException
key
- The cache keytimeout
- A timeout in milliseconds to wait for a
read lock on the requested cache entry
IOException
public boolean store(String key, Object object, int timeout)
key
- The cache keyobject
- The object to store in the cache, it must be a
Serializable
objecttimeout
- A timeout in milliseconds to wait for a write lock
on the requested cache entry
public Object load(String key, int timeout)
key
- The cache keytimeout
- A timeout in milliseconds to wait for a read lock
on the requested cache entry
public boolean delete(String key, int timeout)
key
- The cache keytimeout
- A timeout in milliseconds to wait for a write lock
on the requested cache entry
private void validateKey(String key)
private InputStream getInputStream(String key, int timeout) throws IOException
IOException
private OutputStream getOutputStream(String key, int timeout) throws IOException
IOException
private StaticCache.LockEntry aquireLock(String key, boolean writeLock, int timeout)
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |