public class SqlCache extends Object implements Cache
This instance maintains a pool of read/write locks to guarantee atomicity of storing, fetching and invalidating. It does not use SQL transactions. This allows you disable transaction features in your database for better performance. However, it also means that you should not have more than one instance of this class working on the same set of keys, because they will not be sharing the locks.
Also note that prune()
does not clean up unused locks. Since most
applications reuse cache keys anyway, this seems like an insignificant
"memory leak" cost in order to vastly improve pruning performance.
Constructor and Description |
---|
SqlCache(DataSource dataSource)
Construction with a max entry count of 1000 entries and 10 connections in
the pool, and an
InProcessMemoryLockSource . |
SqlCache(DataSource dataSource,
int maxSize,
int poolSize,
LockSource lockSource)
Constructor.
|
SqlCache(DataSource dataSource,
LockSource lockSource)
Construction with a max entry count of 1000 entries and 10 connections in
the pool.
|
Modifier and Type | Method and Description |
---|---|
protected void |
down()
Call when server is down.
|
CacheEntry |
fetch(String key)
Fetches an entry from the cache if it's there and has not yet expired.
|
DataSource |
getDataSource()
The data source.
|
int |
getMaxSize()
The current max entry count.
|
void |
invalidate(String tag)
Makes sure that all tagged entries are un-fetchable.
|
void |
prune()
Removes expired cache entries.
|
void |
reset()
Removes all entries in the cache.
|
void |
setMaxSize(int maxSize) |
void |
store(String key,
CacheEntry entry)
Stores an entry in the cache.
|
protected void |
up()
Call when server is up.
|
void |
validateTables(boolean fresh)
Makes sure that the required tables exist.
|
public SqlCache(DataSource dataSource)
InProcessMemoryLockSource
.dataSource
- The data sourcepublic SqlCache(DataSource dataSource, LockSource lockSource)
dataSource
- The data sourcelockSource
- The lock sourcepublic SqlCache(DataSource dataSource, int maxSize, int poolSize, LockSource lockSource)
dataSource
- The data sourcemaxSize
- The max entry countpoolSize
- The number of connections in the poollockSource
- The lock sourcepublic DataSource getDataSource()
public int getMaxSize()
setMaxSize(int)
public void setMaxSize(int maxSize)
maxSize
- Max entry countgetMaxSize()
public void validateTables(boolean fresh)
fresh
- Whether to drop the table firstpublic void store(String key, CacheEntry entry)
Cache
CacheEntry.getExpirationDate()
, although it may very well
be un-fetchable sooner.
Keys are unique to the cache. Storing for a key that already exists will replace the entry if it exists.
public CacheEntry fetch(String key)
Cache
public void invalidate(String tag)
Cache
invalidate
in interface Cache
tag
- The cache tagpublic void prune()
Cache
public void reset()
Cache
protected void up()
protected void down()
Copyright © 2009-2016 Three Crickets LLC.