public class DiskCacheModule extends AbstractModule implements CacheService
Stores cached content on a file system. The file names are MD5 hashes of the provided cache key. To avoid hash collisions, the key is stored at the start of each cache file and is checked when the cached input stream is returned. The returned input and output streams are at the position where the actual cached contents are read/written, that is, this class handles internally everything about the keys. Note that hash collisions should be extremely rare.
Use parameter name cacheDir in init options to set the directory where files are cached. maxCacheTime parameter can be used to set the maximum age of cached content. Any page that is older than that will not be returned from the cache. The time is given in milliseconds. Use 0 for no cache expiry, this is the default setting.
The cached files can optionally be organised into a directory tree based on the first few characters of the MD5 hash. This avoids having directories with massive amounts of files which may in some situations be problematic. To configure this in the module parameters, use parameter names numSubdirs and subdirPrefixLength. subdirPrefixLength is the number of characters to use for each subdir, it defaults to 2. numSubdirs is the number of subdirectories to use, it defaults to 0, i.e. put everything directly under the cache directory instead of using any subdirectories.
It is essential that the streams returned by getPage and storePage be closed properly. Failing to do so will cause the cache to remain locked. Even if an IOException is thrown by one of the read methods, close should still be called.
Modifier and Type | Class and Description |
---|---|
private class |
DiskCacheModule.LockedInputStream |
private class |
DiskCacheModule.LockedOutputStream |
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
cacheDir |
protected java.util.concurrent.locks.ReadWriteLock |
cacheLock |
protected java.lang.String |
COLLISION_SUFFIX |
protected long |
maxCacheTime |
protected int |
numSubdirs |
protected int |
subdirPrefixLength |
autoStart, isInitialized, isRunning, logging, loggingModule, moduleManager
Constructor and Description |
---|
DiskCacheModule() |
Modifier and Type | Method and Description |
---|---|
java.util.Collection<Module> |
getDependencies(ModuleManager manager)
Returns all the modules this module depends on.
|
protected java.io.InputStream |
getInputStream(java.lang.String originalKey,
long modifyTime) |
protected java.io.OutputStream |
getOutputStream(java.lang.String originalKey) |
java.io.InputStream |
getPage(java.lang.String key,
long modifyTime)
Gets a cached version of a page if one exists in the cache.
|
protected java.lang.String |
getSubdirs(java.lang.String hash) |
void |
init(ModuleManager manager,
java.util.HashMap<java.lang.String,java.lang.Object> settings)
Initialises the module.
|
protected java.lang.String |
makeHash(java.lang.String key) |
protected java.lang.String |
readCacheFileKey(java.io.InputStream in) |
void |
start(ModuleManager manager)
Starts the module.
|
void |
stop(ModuleManager manager)
Stops the module.
|
java.io.OutputStream |
storePage(java.lang.String key,
long modifyTime)
Stores a cached version of a page with the given time stamp.
|
protected void |
writeCacheFileKey(java.io.OutputStream out,
java.lang.String key) |
isInitialized, isRunning, requireLogging, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
isInitialized, isRunning
protected final java.lang.String COLLISION_SUFFIX
protected int numSubdirs
protected int subdirPrefixLength
protected long maxCacheTime
protected java.util.concurrent.locks.ReadWriteLock cacheLock
protected java.lang.String cacheDir
public java.util.Collection<Module> getDependencies(ModuleManager manager) throws ModuleException
Module
getDependencies
in interface Module
getDependencies
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public void init(ModuleManager manager, java.util.HashMap<java.lang.String,java.lang.Object> settings) throws ModuleException
Module
Initialises the module. After constructor, this is the first method called in the life cycle of a module. It should not perform anything time consuming or anything with notable outside side effects. It should only read the parameters and initialise the module so that it can later be started. Note that a module being initialised doesn't mean that it necessarily will ever be started.
A ModuleException may be thrown if something vital is missing from the parameters or they are not sensible. In some cases you may not want to throw an exception even if vital initialisation information is missing. If, for example, it is possible that the module is initialised in some other way between the init and the start method calls. A ModuleException may also be thrown at the start method if the module is still not initialised.
init
in interface Module
init
in class AbstractModule
manager
- The module manager handling this module. You may keep a
reference to it if needed.ModuleException
public void start(ModuleManager manager) throws ModuleException
Module
start
in interface Module
start
in class AbstractModule
manager
- The module manager handling this module.ModuleException
public void stop(ModuleManager manager)
Module
stop
in interface Module
stop
in class AbstractModule
manager
- The module manager handling this module.protected java.lang.String makeHash(java.lang.String key)
protected java.lang.String readCacheFileKey(java.io.InputStream in) throws java.io.IOException
java.io.IOException
protected java.lang.String getSubdirs(java.lang.String hash)
protected java.io.InputStream getInputStream(java.lang.String originalKey, long modifyTime)
protected void writeCacheFileKey(java.io.OutputStream out, java.lang.String key) throws java.io.IOException
java.io.IOException
protected java.io.OutputStream getOutputStream(java.lang.String originalKey) throws java.io.IOException
java.io.IOException
public java.io.InputStream getPage(java.lang.String key, long modifyTime)
CacheService
getPage
in interface CacheService
key
- The key of the cached page.modifyTime
- The minimum caching time of the page, or 0 if no such limit is used.public java.io.OutputStream storePage(java.lang.String key, long modifyTime)
CacheService
storePage
in interface CacheService
key
- The key of the cached page.modifyTime
- A timestamp for the cached version.Copyright 2004-2015 Wandora Team