public class PersistentUserManagedEhcache<K,V> extends java.lang.Object implements PersistentUserManagedCache<K,V>
PersistentUserManagedCache
which is a cache with a persistent resource outside of a
CacheManager
.
Ehcache
users should not have to depend on this type but rely exclusively on the api types in package
org.ehcache
.
Cache.Entry<K,V>
Constructor and Description |
---|
PersistentUserManagedEhcache(CacheConfiguration<K,V> configuration,
Store<K,V> store,
ResilienceStrategy<K,V> resilienceStrategy,
DiskResourceService diskPersistenceService,
CacheLoaderWriter<? super K,V> cacheLoaderWriter,
CacheEventDispatcher<K,V> eventDispatcher,
java.lang.String id)
Creates a new
PersistentUserManagedCache based on the provided parameters. |
Modifier and Type | Method and Description |
---|---|
void |
addHook(LifeCycled lifeCycled)
Adds a hook to lifecycle transitions.
|
void |
clear()
Removes all mappings currently present in the
Cache . |
void |
close()
Transitions this
UserManagedCache to UNINITIALIZED . |
boolean |
containsKey(K key)
Checks whether a mapping for the given key is present, without retrieving the associated value.
|
void |
destroy()
Destroys all persistent data structures for this
PersistentUserManagedCache . |
V |
get(K key)
Retrieves the value currently mapped to the provided key.
|
java.util.Map<K,V> |
getAll(java.util.Set<? extends K> keys)
Retrieves all values associated with the given key set.
|
CacheRuntimeConfiguration<K,V> |
getRuntimeConfiguration()
Exposes the
CacheRuntimeConfiguration associated with this Cache instance. |
Status |
getStatus()
Returns the current
Status of this UserManagedCache . |
void |
init()
Transitions this
UserManagedCache to AVAILABLE . |
java.util.Iterator<Cache.Entry<K,V>> |
iterator()
Returns an iterator over the cache entries.
|
void |
put(K key,
V value)
Associates the given value to the given key in this
Cache . |
void |
putAll(java.util.Map<? extends K,? extends V> entries)
Associates all the provided key:value pairs.
|
V |
putIfAbsent(K key,
V value)
Maps the specified key to the specified value in this cache, unless a non-expired mapping
already exists.
|
void |
remove(K key)
Removes the value, if any, associated with the provided key.
|
boolean |
remove(K key,
V value)
Removes the entry for a key only if currently mapped to the given value
and the entry is not expired.
|
void |
removeAll(java.util.Set<? extends K> keys)
Removes any associated value for the given key set.
|
V |
replace(K key,
V value)
Replaces the entry for a key only if currently mapped to some value and the entry is not expired.
|
boolean |
replace(K key,
V oldValue,
V newValue)
Replaces the entry for a key only if currently mapped to the given value
and the entry is not expired.
|
public PersistentUserManagedEhcache(CacheConfiguration<K,V> configuration, Store<K,V> store, ResilienceStrategy<K,V> resilienceStrategy, DiskResourceService diskPersistenceService, CacheLoaderWriter<? super K,V> cacheLoaderWriter, CacheEventDispatcher<K,V> eventDispatcher, java.lang.String id)
PersistentUserManagedCache
based on the provided parameters.configuration
- the cache configurationstore
- the underlying storediskPersistenceService
- the persistence servicecacheLoaderWriter
- the optional loader writereventDispatcher
- the event dispatcherid
- an id for this cachepublic void destroy() throws CachePersistenceException
PersistentUserManagedCache
.destroy
in interface PersistentUserManagedCache<K,V>
CachePersistenceException
- if the persistent data cannot be destroyedpublic void init()
UserManagedCache
to AVAILABLE
.
If an error occurs before the UserManagedCache
is AVAILABLE
, it will revert to
UNINITIALIZED
and attempt to properly release all resources.
init
in interface UserManagedCache<K,V>
public void close()
UserManagedCache
to UNINITIALIZED
.
This will release all resources held by this cache.
Failure to release a resource will not prevent other resources from being released.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface UserManagedCache<K,V>
public Status getStatus()
Status
of this UserManagedCache
.getStatus
in interface UserManagedCache<K,V>
Status
public V get(K key) throws CacheLoadingException
get
in interface Cache<K,V>
key
- the key, may not be null
null
if noneCacheLoadingException
- if the CacheLoaderWriter
associated with this cache was
invoked and threw an Exception
public void put(K key, V value) throws CacheWritingException
Cache
.put
in interface Cache<K,V>
key
- the key, may not be null
value
- the value, may not be null
CacheWritingException
- if the CacheLoaderWriter
associated with this cache threw an
Exception
while writing the value for the given key to the underlying system of record.public boolean containsKey(K key)
containsKey
in interface Cache<K,V>
key
- the key, may not be null
true
if a mapping is present, false
otherwisepublic void remove(K key) throws CacheWritingException
remove
in interface Cache<K,V>
key
- the key to remove the value for, may not be null
CacheWritingException
- if the CacheLoaderWriter
associated with this cache threw an
Exception
while removing the value for the given key from the underlying system of record.public java.util.Map<K,V> getAll(java.util.Set<? extends K> keys) throws BulkCacheLoadingException
getAll
in interface Cache<K,V>
keys
- keys to query for, may not contain null
null
if the key was not mappedBulkCacheLoadingException
- if loading some or all values failedpublic void putAll(java.util.Map<? extends K,? extends V> entries) throws BulkCacheWritingException
putAll
in interface Cache<K,V>
entries
- key:value pairs to associate, keys or values may not be null
BulkCacheWritingException
- if the CacheLoaderWriter
associated with this cache threw an
Exception
while writing given key:value pairs to the underlying system of record.public void removeAll(java.util.Set<? extends K> keys) throws BulkCacheWritingException
removeAll
in interface Cache<K,V>
keys
- keys to remove values for, may not be null
BulkCacheWritingException
- if the CacheLoaderWriter
associated with this cache threw an
Exception
while removing mappings for given keys from the underlying system of record.public void clear()
Cache
.
It does so without invoking the CacheLoaderWriter
or any registered
CacheEventListener
instances.
This is not an atomic operation and can potentially be very expensive.
public V putIfAbsent(K key, V value) throws CacheLoadingException, CacheWritingException
This is equivalent to
if (!cache.containsKey(key)) cache.put(key, value); return null; else return cache.get(key);except that the action is performed atomically.
The value can be retrieved by calling the get
method
with a key that is equal to the original key.
Neither the key nor the value can be null
.
putIfAbsent
in interface Cache<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keynull
if no such mapping existed or the mapping was expiredCacheLoadingException
- if the CacheLoaderWriter
associated with this cache was invoked and threw an Exception
CacheWritingException
- if the CacheLoaderWriter
associated
with this cache threw an Exception
while writing the value for the
given key to the underlying system of record.public boolean remove(K key, V value) throws CacheWritingException
This is equivalent to
if (cache.containsKey(key) && cache.get(key).equals(value)) { cache.remove(key); return true; } else return false;except that the action is performed atomically.
The key cannot be null
.
remove
in interface Cache<K,V>
key
- key with which the specified value is associatedvalue
- value expected to be removedCacheWritingException
- if the CacheLoaderWriter
associated
with this cache threw an Exception
while removing the value for the
given key from the underlying system of record.public V replace(K key, V value) throws CacheLoadingException, CacheWritingException
This is equivalent to
V oldValue = cache.get(key); if (oldValue != null) { cache.put(key, value); } return oldValue;except that the action is performed atomically.
Neither the key nor the value can be null
.
replace
in interface Cache<K,V>
key
- of the value to be replacedvalue
- the new valuenull
if
no such mapping existed or the mapping was expiredCacheLoadingException
- if the CacheLoaderWriter
associated with this cache was invoked and threw an Exception
CacheWritingException
- if the CacheLoaderWriter
associated
with this cache threw an Exception
while writing the value for the
given key to the underlying system of record.public boolean replace(K key, V oldValue, V newValue) throws CacheLoadingException, CacheWritingException
This is equivalent to
if (cache.containsKey(key) && cache.get(key).equals(oldValue)) { cache.put(key, newValue); return true; } else return false;except that the action is performed atomically.
Neither the key nor the value can be null
.
replace
in interface Cache<K,V>
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified keyCacheLoadingException
- if the CacheLoaderWriter
associated with this cache was invoked and threw an Exception
CacheWritingException
- if the CacheLoaderWriter
associated
with this cache threw an Exception
while writing the value for the
given key to the underlying system of record.public CacheRuntimeConfiguration<K,V> getRuntimeConfiguration()
CacheRuntimeConfiguration
associated with this Cache instance.getRuntimeConfiguration
in interface Cache<K,V>
public java.util.Iterator<Cache.Entry<K,V>> iterator()
Due to the interactions of the cache and iterator contracts it is possible for iteration to return expired entries.
public void addHook(LifeCycled lifeCycled)