public class MongoDbCache extends Object implements Cache
Uses a dedicated MongoDB collection to store the cache, creating it if it doesn't exist.
Supports storing entries as either binary dumps or detailed documents. Binary dumps take less space and are slightly more efficient, while detailed documents are far easier to debug. Binary mode is off by default.
Note that MongoDB's indexing facility allows for very high performance invalidation and pruning.
Constructor and Description |
---|
MongoDbCache()
Construction for localhost connection, database "prudence", collection
"cache".
|
MongoDbCache(MongoClient client)
Construction for database "prudence", collection "cache".
|
MongoDbCache(MongoClient client,
String databaseName)
Construction for collection "cache".
|
MongoDbCache(MongoClient client,
String databaseName,
String collectionName)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
CacheEntry |
fetch(String key)
Fetches an entry from the cache if it's there and has not yet expired.
|
void |
invalidate(String tag)
Makes sure that all tagged entries are un-fetchable.
|
boolean |
isBinary()
Whether to store entries by serializing them into BSON binaries.
|
void |
prune()
Removes expired cache entries.
|
void |
reset()
Removes all entries in the cache.
|
void |
setBinary(boolean isBinary) |
void |
store(String key,
CacheEntry entry)
Stores an entry in the cache.
|
public MongoDbCache() throws UnknownHostException
UnknownHostException
- In case localhost could not be foundpublic MongoDbCache(MongoClient client)
client
- The MongoDB clientpublic MongoDbCache(MongoClient client, String databaseName)
client
- The MongoDB clientdatabaseName
- The MongoDB database namepublic MongoDbCache(MongoClient client, String databaseName, String collectionName)
client
- The MongoDB clientdatabaseName
- The MongoDB database namecollectionName
- The name of the collection to use for the cachepublic boolean isBinary()
setBinary(boolean)
public void setBinary(boolean isBinary)
isBinary
- A booleanisBinary
public 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
Copyright © 2009-2016 Three Crickets LLC.