Interface OHCache<K,V>
-
- All Superinterfaces:
AutoCloseable
,Closeable
- All Known Implementing Classes:
OHCacheChunkedImpl
,OHCacheLinkedImpl
public interface OHCache<K,V> extends Closeable
-
-
Field Summary
Fields Modifier and Type Field Description static long
NEVER_EXPIRE
static long
USE_DEFAULT_EXPIRE_AT
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
addOrReplace(K key, V old, V value)
Same asaddOrReplace(Object, Object, Object, long)
but uses the configured default TTL, if any.boolean
addOrReplace(K key, V old, V value, long expireAt)
Adds key/value if either the key is not present or the existing value matches parameterold
.long
capacity()
void
clear()
Removes all entries from the cache.boolean
containsKey(K key)
Checks whether an entry for a given key exists.int
deserializeEntries(ReadableByteChannel channel)
Deserialize entries.boolean
deserializeEntry(ReadableByteChannel channel)
Deserialize a single entry.CloseableIterator<K>
deserializeKeys(ReadableByteChannel channel)
long
freeCapacity()
V
get(K key)
Get the value for a given key.EstimatedHistogram
getBucketHistogram()
DirectValueAccess
getDirect(K key)
Returns a closeable byte buffer.DirectValueAccess
getDirect(K key, boolean updateLRU)
V
getWithLoader(K key, CacheLoader<K,V> loader)
V
getWithLoader(K key, CacheLoader<K,V> loader, long timeout, TimeUnit unit)
Future<V>
getWithLoaderAsync(K key, CacheLoader<K,V> loader)
Shortcut to callgetWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry time-to-live.Future<V>
getWithLoaderAsync(K key, CacheLoader<K,V> loader, long expireAt)
Shortcut to callgetWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry time-to-live.int[]
hashTableSizes()
CloseableIterator<ByteBuffer>
hotKeyBufferIterator(int n)
Builds an iterator over all keys returning direct byte buffers.CloseableIterator<K>
hotKeyIterator(int n)
Builds an iterator over the N most recently used keys returning deserialized objects.CloseableIterator<ByteBuffer>
keyBufferIterator()
Builds an iterator over all keys returning direct byte buffers.CloseableIterator<K>
keyIterator()
Builds an iterator over all keys returning deserialized objects.float
loadFactor()
long
memUsed()
long[]
perSegmentSizes()
boolean
put(K key, V value)
Same asput(Object, Object, long)
but uses the configured default TTL, if any.boolean
put(K key, V value, long expireAt)
Adds the key/value.void
putAll(Map<? extends K,? extends V> m)
This is effectively a shortcut to add all entries in the given mapm
.boolean
putIfAbsent(K key, V value)
Same asputIfAbsent(Object, Object, long)
but uses the configured default TTL, if any.boolean
putIfAbsent(K key, V value, long expireAt)
Adds the key/value if the key is not present.boolean
remove(K key)
Remove a single entry for the given key.void
removeAll(Iterable<K> keys)
This is effectively a shortcut to remove the entries for all keys given in the iterablekeys
.void
resetStatistics()
int
segments()
boolean
serializeEntry(K key, WritableByteChannel channel)
Serialize a cache entry.int
serializeHotNEntries(int n, WritableByteChannel channel)
Serialize a the hottest cache entries.int
serializeHotNKeys(int n, WritableByteChannel channel)
Serialize a the keys of the hottest cache entries.void
setCapacity(long capacity)
Modify the cache's capacity.long
size()
OHCacheStats
stats()
-
-
-
Field Detail
-
USE_DEFAULT_EXPIRE_AT
static final long USE_DEFAULT_EXPIRE_AT
- See Also:
- Constant Field Values
-
NEVER_EXPIRE
static final long NEVER_EXPIRE
- See Also:
- Constant Field Values
-
-
Method Detail
-
put
boolean put(K key, V value)
Same asput(Object, Object, long)
but uses the configured default TTL, if any.- Parameters:
key
- key of the entry to be added. Must not benull
.value
- value of the entry to be added. Must not benull
.- Returns:
true
, if the entry has been added,false
otherwise
-
put
boolean put(K key, V value, long expireAt)
Adds the key/value. If the entry size of key/value exceeds the configured maximum entry length, any previously existing entry for the key is removed.- Parameters:
key
- key of the entry to be added. Must not benull
.value
- value of the entry to be added. Must not benull
.expireAt
- timestamp in milliseconds since "epoch" (likeSystem.currentTimeMillis()
) when the entry shall expire. PassUSE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orNEVER_EXPIRE
to let it never expire.- Returns:
true
, if the entry has been added,false
otherwise
-
addOrReplace
boolean addOrReplace(K key, V old, V value)
Same asaddOrReplace(Object, Object, Object, long)
but uses the configured default TTL, if any.- Parameters:
key
- key of the entry to be added or replaced. Must not benull
.old
- if the entry exists, it's serialized value is compared to the serialized value ofold
and only replaced, if it matches.value
- value of the entry to be added. Must not benull
.- Returns:
true
on success orfalse
if the existing value does not matcuhold
-
addOrReplace
boolean addOrReplace(K key, V old, V value, long expireAt)
Adds key/value if either the key is not present or the existing value matches parameterold
. If the entry size of key/value exceeds the configured maximum entry length, the old value is removed.- Parameters:
key
- key of the entry to be added or replaced. Must not benull
.old
- if the entry exists, it's serialized value is compared to the serialized value ofold
and only replaced, if it matches.value
- value of the entry to be added. Must not benull
.expireAt
- timestamp in milliseconds since "epoch" (likeSystem.currentTimeMillis()
) when the entry shall expire. PassUSE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orNEVER_EXPIRE
to let it never expire.- Returns:
true
on success orfalse
if the existing value does not matcuhold
-
putIfAbsent
boolean putIfAbsent(K key, V value)
Same asputIfAbsent(Object, Object, long)
but uses the configured default TTL, if any.- Parameters:
key
- key of the entry to be added. Must not benull
.value
- value of the entry to be added. Must not benull
.- Returns:
true
on success orfalse
if the key is already present.
-
putIfAbsent
boolean putIfAbsent(K key, V value, long expireAt)
Adds the key/value if the key is not present. If the entry size of key/value exceeds the configured maximum entry length, any previously existing entry for the key is removed.- Parameters:
key
- key of the entry to be added. Must not benull
.value
- value of the entry to be added. Must not benull
.expireAt
- timestamp in milliseconds since "epoch" (likeSystem.currentTimeMillis()
) when the entry shall expire. PassUSE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orNEVER_EXPIRE
to let it never expire.- Returns:
true
on success orfalse
if the key is already present.
-
putAll
void putAll(Map<? extends K,? extends V> m)
This is effectively a shortcut to add all entries in the given mapm
.- Parameters:
m
- entries to be added
-
remove
boolean remove(K key)
Remove a single entry for the given key.- Parameters:
key
- key of the entry to be removed. Must not benull
.- Returns:
true
, if the entry has been removed,false
otherwise
-
removeAll
void removeAll(Iterable<K> keys)
This is effectively a shortcut to remove the entries for all keys given in the iterablekeys
.- Parameters:
keys
- keys to be removed
-
clear
void clear()
Removes all entries from the cache.
-
get
V get(K key)
Get the value for a given key.- Parameters:
key
- key of the entry to be retrieved. Must not benull
.- Returns:
- either the non-
null
value ornull
if no entry for the requested key exists
-
containsKey
boolean containsKey(K key)
Checks whether an entry for a given key exists. Usually, this is more efficient than testing fornull
viaget(Object)
.- Parameters:
key
- key of the entry to be retrieved. Must not benull
.- Returns:
- either
true
if an entry for the given key exists orfalse
if no entry for the requested key exists
-
getDirect
DirectValueAccess getDirect(K key)
Returns a closeable byte buffer. You must close the returnedDirectValueAccess
instance after use. After closing, you must not call any of the methods of theByteBuffer
returned byDirectValueAccess.buffer()
.- Parameters:
key
- the key of the value to retrieve- Returns:
- reference-counted byte buffer or
null
if key does not exist.
-
getDirect
DirectValueAccess getDirect(K key, boolean updateLRU)
- Parameters:
key
- the key to retrieveupdateLRU
- whether to update LRU stats- Returns:
- reference-counted byte buffer or
null
if key does not exist.
-
getWithLoaderAsync
Future<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader)
Shortcut to callgetWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry time-to-live. Note that the cache has to be configured with anExecutor
to schedule the load process.Note that the future may indicate a failure via it's
get
methods if theCacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to not enough memory for example.- Parameters:
key
- key of the value to loadloader
- loader implementation to use- Returns:
- a future to process the load request asynchronously.
-
getWithLoaderAsync
Future<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader, long expireAt)
Shortcut to callgetWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry time-to-live. Note that the cache has to be configured with anExecutor
to schedule the load process.Note that the future may indicate a failure via it's
get
methods if theCacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to not enough memory for example.- Parameters:
key
- key of the value to loadloader
- loader implementation to useexpireAt
- optional timestamp (millis since epoch) at which the entry shall expire- Returns:
- a future to process the load request asynchronously.
-
getWithLoader
V getWithLoader(K key, CacheLoader<K,V> loader) throws InterruptedException, ExecutionException
- Parameters:
key
- key of the value to loadloader
- loader implementation to use- Returns:
- the loaded value or
null
- Throws:
InterruptedException
- as from the contract ofFuture.get()
ExecutionException
- thrown if theCacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to not enough memory for example.
-
getWithLoader
V getWithLoader(K key, CacheLoader<K,V> loader, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Parameters:
key
- key of the value to loadloader
- loader implementation to usetimeout
- timeout value to be passed toFuture.get(long, TimeUnit)
unit
- timeout unit to be passed toFuture.get(long, TimeUnit)
- Returns:
- the loaded value or
null
- Throws:
InterruptedException
- as from the contract ofFuture.get()
ExecutionException
- thrown if theCacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to not enough memory for example.TimeoutException
- as from the contract ofFuture.get(long, TimeUnit)
-
hotKeyIterator
CloseableIterator<K> hotKeyIterator(int n)
Builds an iterator over the N most recently used keys returning deserialized objects. You must callclose()
on the returned iterator.Note: During a rehash, the implementation might return keys twice or not at all.
- Parameters:
n
- the N most recently used keys- Returns:
- closeable iterator over the keys
-
keyIterator
CloseableIterator<K> keyIterator()
Builds an iterator over all keys returning deserialized objects. You must callclose()
on the returned iterator.Note: During a rehash, the implementation might return keys twice or not at all.
- Returns:
- closeable iterator over the keys
-
hotKeyBufferIterator
CloseableIterator<ByteBuffer> hotKeyBufferIterator(int n)
Builds an iterator over all keys returning direct byte buffers. Do not use a returnedByteBuffer
after calling any method on the iterator. You must callclose()
on the returned iterator.Note: During a rehash, the implementation might return keys twice or not at all.
- Parameters:
n
- the N most recently used keys- Returns:
- closeable iterator over byte-buffers
-
keyBufferIterator
CloseableIterator<ByteBuffer> keyBufferIterator()
Builds an iterator over all keys returning direct byte buffers. Do not use a returnedByteBuffer
after calling any method on the iterator. You must callclose()
on the returned iterator.Note: During a rehash, the implementation might return keys twice or not at all.
- Returns:
- closeable iterator over byte-buffers
-
deserializeEntry
boolean deserializeEntry(ReadableByteChannel channel) throws IOException
Deserialize a single entry.- Parameters:
channel
- channel to read from- Returns:
- whether the deserialization was successful
- Throws:
IOException
- indicates an IO error
-
serializeEntry
boolean serializeEntry(K key, WritableByteChannel channel) throws IOException
Serialize a cache entry.- Parameters:
key
- Key of the cache entry to serializechannel
- target channel to write to- Returns:
- whether the serialization was successful
- Throws:
IOException
- indicates an IO error
-
deserializeEntries
int deserializeEntries(ReadableByteChannel channel) throws IOException
Deserialize entries.- Parameters:
channel
- channel to read from- Returns:
- number of deserialized entries
- Throws:
IOException
- indicates an IO error
-
serializeHotNEntries
int serializeHotNEntries(int n, WritableByteChannel channel) throws IOException
Serialize a the hottest cache entries.- Parameters:
n
- number of cache entries to serializechannel
- target channel to write to- Returns:
- number of serialized entries
- Throws:
IOException
- indicates an IO error
-
serializeHotNKeys
int serializeHotNKeys(int n, WritableByteChannel channel) throws IOException
Serialize a the keys of the hottest cache entries.- Parameters:
n
- number of cache entries to serializechannel
- target channel to write to- Returns:
- number of serialized entries
- Throws:
IOException
- indicates an IO error
-
deserializeKeys
CloseableIterator<K> deserializeKeys(ReadableByteChannel channel) throws IOException
- Parameters:
channel
- channel to read from- Returns:
- deserialized keys
- Throws:
IOException
- indicates an IO error
-
resetStatistics
void resetStatistics()
-
size
long size()
-
hashTableSizes
int[] hashTableSizes()
-
perSegmentSizes
long[] perSegmentSizes()
-
getBucketHistogram
EstimatedHistogram getBucketHistogram()
-
segments
int segments()
-
capacity
long capacity()
-
memUsed
long memUsed()
-
freeCapacity
long freeCapacity()
-
loadFactor
float loadFactor()
-
stats
OHCacheStats stats()
-
setCapacity
void setCapacity(long capacity)
Modify the cache's capacity. Lowering the capacity will not immediately remove any entry nor will it immediately free allocated (off heap) memory.Future operations will even allocate in flight, temporary memory - i.e. setting capacity to 0 does not disable the cache, it will continue to work but cannot add more data.
- Parameters:
capacity
- the new capacity
-
-