Class OHCacheLinkedImpl<K,V>
- java.lang.Object
-
- org.caffinitas.ohc.linked.OHCacheLinkedImpl<K,V>
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,OHCache<K,V>
public final class OHCacheLinkedImpl<K,V> extends Object implements OHCache<K,V>
-
-
Field Summary
-
Fields inherited from interface org.caffinitas.ohc.OHCache
NEVER_EXPIRE, USE_DEFAULT_EXPIRE_AT
-
-
Constructor Summary
Constructors Constructor Description OHCacheLinkedImpl(OHCacheBuilder<K,V> builder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addOrReplace(K key, V old, V value)
Same asOHCache.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 parameterold
.long
capacity()
void
clear()
Removes all entries from the cache.void
close()
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)
LikeOHCache.getDirect(Object)
, but allows skipping the update of LRU stats whenupdateLRU
isfalse
.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 callOHCache.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 callOHCache.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()
boolean
put(K k, V v)
Same asOHCache.put(Object, Object, long)
but uses the configured default TTL, if any.boolean
put(K k, V v, 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 k, V v)
Same asOHCache.putIfAbsent(Object, Object, long)
but uses the configured default TTL, if any.boolean
putIfAbsent(K k, V v, long expireAt)
Adds the key/value if the key is not present.boolean
remove(K k)
Remove a single entry for the given key.void
removeAll(Iterable<K> iterable)
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()
String
toString()
-
-
-
Constructor Detail
-
OHCacheLinkedImpl
public OHCacheLinkedImpl(OHCacheBuilder<K,V> builder)
-
-
Method Detail
-
getDirect
public DirectValueAccess getDirect(K key)
Description copied from interface:OHCache
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()
.
-
getDirect
public DirectValueAccess getDirect(K key, boolean updateLRU)
Description copied from interface:OHCache
LikeOHCache.getDirect(Object)
, but allows skipping the update of LRU stats whenupdateLRU
isfalse
.
-
containsKey
public boolean containsKey(K key)
Description copied from interface:OHCache
Checks whether an entry for a given key exists. Usually, this is more efficient than testing fornull
viaOHCache.get(Object)
.- Specified by:
containsKey
in interfaceOHCache<K,V>
- 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
-
put
public boolean put(K k, V v)
Description copied from interface:OHCache
Same asOHCache.put(Object, Object, long)
but uses the configured default TTL, if any.
-
put
public boolean put(K k, V v, long expireAt)
Description copied from interface:OHCache
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.- Specified by:
put
in interfaceOHCache<K,V>
- Parameters:
k
- key of the entry to be added. Must not benull
.v
- value of the entry to be added. Must not benull
.expireAt
- timestamp in milliseconds since "epoch" (likeSystem.currentTimeMillis()
) when the entry shall expire. PassOHCache.USE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orOHCache.NEVER_EXPIRE
to let it never expire.- Returns:
true
, if the entry has been added,false
otherwise
-
addOrReplace
public boolean addOrReplace(K key, V old, V value)
Description copied from interface:OHCache
Same asOHCache.addOrReplace(Object, Object, Object, long)
but uses the configured default TTL, if any.- Specified by:
addOrReplace
in interfaceOHCache<K,V>
- 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
public boolean addOrReplace(K key, V old, V value, long expireAt)
Description copied from interface:OHCache
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.- Specified by:
addOrReplace
in interfaceOHCache<K,V>
- 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. PassOHCache.USE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orOHCache.NEVER_EXPIRE
to let it never expire.- Returns:
true
on success orfalse
if the existing value does not matcuhold
-
putIfAbsent
public boolean putIfAbsent(K k, V v)
Description copied from interface:OHCache
Same asOHCache.putIfAbsent(Object, Object, long)
but uses the configured default TTL, if any.- Specified by:
putIfAbsent
in interfaceOHCache<K,V>
- Parameters:
k
- key of the entry to be added. Must not benull
.v
- value of the entry to be added. Must not benull
.- Returns:
true
on success orfalse
if the key is already present.
-
putIfAbsent
public boolean putIfAbsent(K k, V v, long expireAt)
Description copied from interface:OHCache
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.- Specified by:
putIfAbsent
in interfaceOHCache<K,V>
- Parameters:
k
- key of the entry to be added. Must not benull
.v
- value of the entry to be added. Must not benull
.expireAt
- timestamp in milliseconds since "epoch" (likeSystem.currentTimeMillis()
) when the entry shall expire. PassOHCache.USE_DEFAULT_EXPIRE_AT
for the configured default time-to-live orOHCache.NEVER_EXPIRE
to let it never expire.- Returns:
true
on success orfalse
if the key is already present.
-
remove
public boolean remove(K k)
Description copied from interface:OHCache
Remove a single entry for the given key.
-
getWithLoader
public V getWithLoader(K key, CacheLoader<K,V> loader) throws InterruptedException, ExecutionException
Description copied from interface:OHCache
- Specified by:
getWithLoader
in interfaceOHCache<K,V>
- 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
public V getWithLoader(K key, CacheLoader<K,V> loader, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Description copied from interface:OHCache
- Specified by:
getWithLoader
in interfaceOHCache<K,V>
- 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)
-
getWithLoaderAsync
public Future<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader)
Description copied from interface:OHCache
Shortcut to callOHCache.getWithLoader(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.- Specified by:
getWithLoaderAsync
in interfaceOHCache<K,V>
- Parameters:
key
- key of the value to loadloader
- loader implementation to use- Returns:
- a future to process the load request asynchronously.
-
getWithLoaderAsync
public Future<V> getWithLoaderAsync(K key, CacheLoader<K,V> loader, long expireAt)
Description copied from interface:OHCache
Shortcut to callOHCache.getWithLoader(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.- Specified by:
getWithLoaderAsync
in interfaceOHCache<K,V>
- 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.
-
clear
public void clear()
Description copied from interface:OHCache
Removes all entries from the cache.
-
setCapacity
public void setCapacity(long capacity)
Description copied from interface:OHCache
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.
- Specified by:
setCapacity
in interfaceOHCache<K,V>
- Parameters:
capacity
- the new capacity
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
resetStatistics
public void resetStatistics()
- Specified by:
resetStatistics
in interfaceOHCache<K,V>
-
stats
public OHCacheStats stats()
-
freeCapacity
public long freeCapacity()
- Specified by:
freeCapacity
in interfaceOHCache<K,V>
-
loadFactor
public float loadFactor()
- Specified by:
loadFactor
in interfaceOHCache<K,V>
-
hashTableSizes
public int[] hashTableSizes()
- Specified by:
hashTableSizes
in interfaceOHCache<K,V>
-
perSegmentSizes
public long[] perSegmentSizes()
- Specified by:
perSegmentSizes
in interfaceOHCache<K,V>
-
getBucketHistogram
public EstimatedHistogram getBucketHistogram()
- Specified by:
getBucketHistogram
in interfaceOHCache<K,V>
-
deserializeKeys
public CloseableIterator<K> deserializeKeys(ReadableByteChannel channel) throws IOException
- Specified by:
deserializeKeys
in interfaceOHCache<K,V>
- Parameters:
channel
- channel to read from- Returns:
- deserialized keys
- Throws:
IOException
- indicates an IO error
-
deserializeEntry
public boolean deserializeEntry(ReadableByteChannel channel) throws IOException
Description copied from interface:OHCache
Deserialize a single entry.- Specified by:
deserializeEntry
in interfaceOHCache<K,V>
- Parameters:
channel
- channel to read from- Returns:
- whether the deserialization was successful
- Throws:
IOException
- indicates an IO error
-
serializeEntry
public boolean serializeEntry(K key, WritableByteChannel channel) throws IOException
Description copied from interface:OHCache
Serialize a cache entry.- Specified by:
serializeEntry
in interfaceOHCache<K,V>
- 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
public int deserializeEntries(ReadableByteChannel channel) throws IOException
Description copied from interface:OHCache
Deserialize entries.- Specified by:
deserializeEntries
in interfaceOHCache<K,V>
- Parameters:
channel
- channel to read from- Returns:
- number of deserialized entries
- Throws:
IOException
- indicates an IO error
-
serializeHotNEntries
public int serializeHotNEntries(int n, WritableByteChannel channel) throws IOException
Description copied from interface:OHCache
Serialize a the hottest cache entries.- Specified by:
serializeHotNEntries
in interfaceOHCache<K,V>
- 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
public int serializeHotNKeys(int n, WritableByteChannel channel) throws IOException
Description copied from interface:OHCache
Serialize a the keys of the hottest cache entries.- Specified by:
serializeHotNKeys
in interfaceOHCache<K,V>
- Parameters:
n
- number of cache entries to serializechannel
- target channel to write to- Returns:
- number of serialized entries
- Throws:
IOException
- indicates an IO error
-
putAll
public void putAll(Map<? extends K,? extends V> m)
Description copied from interface:OHCache
This is effectively a shortcut to add all entries in the given mapm
.
-
removeAll
public void removeAll(Iterable<K> iterable)
Description copied from interface:OHCache
This is effectively a shortcut to remove the entries for all keys given in the iterablekeys
.
-
hotKeyIterator
public CloseableIterator<K> hotKeyIterator(int n)
Description copied from interface:OHCache
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.
- Specified by:
hotKeyIterator
in interfaceOHCache<K,V>
- Parameters:
n
- the N most recently used keys- Returns:
- closeable iterator over the keys
-
hotKeyBufferIterator
public CloseableIterator<ByteBuffer> hotKeyBufferIterator(int n)
Description copied from interface:OHCache
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.
- Specified by:
hotKeyBufferIterator
in interfaceOHCache<K,V>
- Parameters:
n
- the N most recently used keys- Returns:
- closeable iterator over byte-buffers
-
keyIterator
public CloseableIterator<K> keyIterator()
Description copied from interface:OHCache
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.
- Specified by:
keyIterator
in interfaceOHCache<K,V>
- Returns:
- closeable iterator over the keys
-
keyBufferIterator
public CloseableIterator<ByteBuffer> keyBufferIterator()
Description copied from interface:OHCache
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.
- Specified by:
keyBufferIterator
in interfaceOHCache<K,V>
- Returns:
- closeable iterator over byte-buffers
-
-