public interface OHCache<K,V> extends Closeable
Modifier and Type | Field and Description |
---|---|
static long |
NEVER_EXPIRE |
static long |
USE_DEFAULT_EXPIRE_AT |
Modifier and Type | Method and Description |
---|---|
boolean |
addOrReplace(K key,
V old,
V value)
Same as
addOrReplace(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 parameter
old . |
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) |
boolean |
deserializeEntry(ReadableByteChannel channel) |
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 call
getWithLoader(Object, CacheLoader, long, TimeUnit) using the default entry
time-to-live. |
Future<V> |
getWithLoaderAsync(K key,
CacheLoader<K,V> loader,
long expireAt)
Shortcut to call
getWithLoader(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() |
void |
put(K key,
V value)
Same as
put(Object, Object, long) but uses the configured default TTL, if any. |
void |
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 map
m . |
boolean |
putIfAbsent(K key,
V value)
Same as
putIfAbsent(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.
|
void |
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 iterable
keys . |
void |
resetStatistics() |
int |
segments() |
boolean |
serializeEntry(K key,
WritableByteChannel channel) |
int |
serializeHotNEntries(int n,
WritableByteChannel channel) |
int |
serializeHotNKeys(int n,
WritableByteChannel channel) |
void |
setCapacity(long capacity)
Modify the cache's capacity.
|
long |
size() |
OHCacheStats |
stats() |
static final long USE_DEFAULT_EXPIRE_AT
static final long NEVER_EXPIRE
void put(K key, V value)
put(Object, Object, long)
but uses the configured default TTL, if any.key
- key of the entry to be added. Must not be null
.value
- value of the entry to be added. Must not be null
.void put(K key, V value, long expireAt)
key
- key of the entry to be added. Must not be null
.value
- value of the entry to be added. Must not be null
.expireAt
- timestamp in milliseconds since "epoch" (like System.currentTimeMillis()
)
when the entry shall expire. Pass USE_DEFAULT_EXPIRE_AT
for the configured default
time-to-live or NEVER_EXPIRE
to let it never expire.boolean addOrReplace(K key, V old, V value)
addOrReplace(Object, Object, Object, long)
but uses the configured default TTL, if any.key
- key of the entry to be added or replaced. Must not be null
.old
- if the entry exists, it's serialized value is compared to the serialized value of old
and only replaced, if it matches.value
- value of the entry to be added. Must not be null
.true
on success or false
if the existing value does not matcuh old
boolean addOrReplace(K key, V old, V value, long expireAt)
old
.
If the entry size of key/value exceeds the configured maximum entry length, the old value is removed.key
- key of the entry to be added or replaced. Must not be null
.old
- if the entry exists, it's serialized value is compared to the serialized value of old
and only replaced, if it matches.value
- value of the entry to be added. Must not be null
.expireAt
- timestamp in milliseconds since "epoch" (like System.currentTimeMillis()
)
when the entry shall expire. Pass USE_DEFAULT_EXPIRE_AT
for the configured default
time-to-live or NEVER_EXPIRE
to let it never expire.true
on success or false
if the existing value does not matcuh old
boolean putIfAbsent(K key, V value)
putIfAbsent(Object, Object, long)
but uses the configured default TTL, if any.key
- key of the entry to be added. Must not be null
.value
- value of the entry to be added. Must not be null
.true
on success or false
if the key is already present.boolean putIfAbsent(K key, V value, long expireAt)
key
- key of the entry to be added. Must not be null
.value
- value of the entry to be added. Must not be null
.expireAt
- timestamp in milliseconds since "epoch" (like System.currentTimeMillis()
)
when the entry shall expire. Pass USE_DEFAULT_EXPIRE_AT
for the configured default
time-to-live or NEVER_EXPIRE
to let it never expire.true
on success or false
if the key is already present.void putAll(Map<? extends K,? extends V> m)
m
.m
- entries to be addedvoid remove(K key)
key
- key of the entry to be removed. Must not be null
.void removeAll(Iterable<K> keys)
keys
.keys
- keys to be removedvoid clear()
V get(K key)
key
- key of the entry to be retrieved. Must not be null
.null
value or null
if no entry for the requested key existsboolean containsKey(K key)
null
via get(Object)
.key
- key of the entry to be retrieved. Must not be null
.true
if an entry for the given key exists or false
if no entry for the requested key existsDirectValueAccess getDirect(K key)
DirectValueAccess
instance after use.
After closing, you must not call any of the methods of the ByteBuffer
returned by DirectValueAccess.buffer()
.null
if key does not exist.DirectValueAccess getDirect(K key, boolean updateLRU)
null
if key does not exist.Future<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader)
getWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry
time-to-live. Note that the cache has to be configured with an Executor
to
schedule the load process.
Note that the future may indicate a failure via it's get
methods if the
CacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to
not enough memory for example.
key
- key of the value to loadloader
- loader implementation to useFuture<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader, long expireAt)
getWithLoader(Object, CacheLoader, long, TimeUnit)
using the default entry
time-to-live. Note that the cache has to be configured with an Executor
to
schedule the load process.
Note that the future may indicate a failure via it's get
methods if the
CacheLoader.load(Object)
implementation threw an error or a runtime exception occured due to
not enough memory for example.
key
- key of the value to loadloader
- loader implementation to useV getWithLoader(K key, CacheLoader<K,V> loader) throws InterruptedException, ExecutionException
key
- key of the value to loadloader
- loader implementation to usenull
InterruptedException
- as from the contract of Future.get()
ExecutionException
- thrown if the CacheLoader.load(Object)
implementation threw an error or a
runtime exception occured due to not enough memory for example.V getWithLoader(K key, CacheLoader<K,V> loader, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
key
- key of the value to loadloader
- loader implementation to usetimeout
- timeout value to be passed to Future.get(long, TimeUnit)
unit
- timeout unit to be passed to Future.get(long, TimeUnit)
null
InterruptedException
- as from the contract of Future.get()
ExecutionException
- thrown if the CacheLoader.load(Object)
implementation threw an error or a
runtime exception occured due to not enough memory for example.TimeoutException
- as from the contract of Future.get(long, TimeUnit)
CloseableIterator<K> hotKeyIterator(int n)
close()
on the returned iterator.
Note: During a rehash, the implementation might return keys twice or not at all.
CloseableIterator<K> keyIterator()
close()
on the returned iterator.
Note: During a rehash, the implementation might return keys twice or not at all.
CloseableIterator<ByteBuffer> hotKeyBufferIterator(int n)
ByteBuffer
after calling any method on the iterator.
You must call close()
on the returned iterator.
Note: During a rehash, the implementation might return keys twice or not at all.
CloseableIterator<ByteBuffer> keyBufferIterator()
ByteBuffer
after calling any method on the iterator.
You must call close()
on the returned iterator.
Note: During a rehash, the implementation might return keys twice or not at all.
boolean deserializeEntry(ReadableByteChannel channel) throws IOException
IOException
boolean serializeEntry(K key, WritableByteChannel channel) throws IOException
IOException
int deserializeEntries(ReadableByteChannel channel) throws IOException
IOException
int serializeHotNEntries(int n, WritableByteChannel channel) throws IOException
IOException
int serializeHotNKeys(int n, WritableByteChannel channel) throws IOException
IOException
CloseableIterator<K> deserializeKeys(ReadableByteChannel channel) throws IOException
IOException
void resetStatistics()
long size()
int[] hashTableSizes()
long[] perSegmentSizes()
EstimatedHistogram getBucketHistogram()
int segments()
long capacity()
long memUsed()
long freeCapacity()
float loadFactor()
OHCacheStats stats()
void setCapacity(long capacity)
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.
Copyright © 2014–2017 Robert Stupp, Koeln, Germany, robert-stupp.de. All rights reserved.