public interface Cache<K extends Serializable,V extends Serializable> extends Map<K,V>
If the cache does grow too large, objects will be removed such that those that are accessed least frequently are removed first. Because expiration happens automatically, the cache makes no guarantee as to how long an object will remain in cache after it is put in.
Optionally, a maximum lifetime for all objects can be specified. In that case, objects will be deleted from cache after that amount of time, even if they are frequently accessed. This feature is useful if objects put in cache represent data that should be periodically refreshed; for example, information from a database.
All cache operations are thread safe.
Note that neither keys or values can be null; A NullPointerException
will be thrown attempting to place or retrieve null values in to the cache.
Cacheable
Modifier and Type | Method and Description |
---|---|
Set<Map.Entry<K,V>> |
entrySet()
IMPORTANT: Unlike the standard
Map.entrySet() implementation, the set returned from
this method cannot be modified. |
long |
getCacheHits()
Returns the number of cache hits.
|
long |
getCacheMisses()
Returns the number of cache misses.
|
int |
getCacheSize()
Returns the size of the cache contents in bytes.
|
default Lock |
getLock(K key)
Returns an existing
Lock on the specified key or creates a new one
if none was found. |
long |
getMaxCacheSize()
Returns the maximum size of the cache in bytes.
|
long |
getMaxLifetime()
Returns the maximum number of milliseconds that any object can live
in cache.
|
String |
getName()
Returns the name of the cache.
|
Set<K> |
keySet()
IMPORTANT: Unlike the standard
Map.keySet() implementation, the set returned from
this method cannot be modified. |
void |
setMaxCacheSize(int maxSize)
Sets the maximum size of the cache in bytes.
|
void |
setMaxLifetime(long maxLifetime)
Sets the maximum number of milliseconds that any object can live
in cache.
|
void |
setName(String name)
Sets the name of the cache
|
Collection<V> |
values()
IMPORTANT: Unlike the standard
Map.values() implementation, the collection returned from
this method cannot be modified. |
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size
String getName()
void setName(String name)
name
- the name of the cachelong getMaxCacheSize()
void setMaxCacheSize(int maxSize)
Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
maxSize
- the maximum size of the cache in bytes.long getMaxLifetime()
void setMaxLifetime(long maxLifetime)
Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
maxLifetime
- the maximum number of milliseconds before objects are expired.int getCacheSize()
long getCacheHits()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
long getCacheMisses()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
Collection<V> values()
Map.values()
implementation, the collection returned from
this method cannot be modified.values
in interface Map<K extends Serializable,V extends Serializable>
Set<Map.Entry<K,V>> entrySet()
Map.entrySet()
implementation, the set returned from
this method cannot be modified.entrySet
in interface Map<K extends Serializable,V extends Serializable>
Set<K> keySet()
Map.keySet()
implementation, the set returned from
this method cannot be modified.keySet
in interface Map<K extends Serializable,V extends Serializable>
default Lock getLock(K key)
Lock
on the specified key or creates a new one
if none was found. This operation is thread safe. Successive calls with the same key may or may not
return the same Lock
. However, different threads asking for the
same Lock at the same time will get the same Lock object.
The supplied cache may or may not be used depending whether the server is running on cluster mode or not. When not running as part of a cluster then the lock will be unrelated to the cache and will only be visible in this JVM.
key
- the object that defines the visibility or scope of the lock.Copyright © 2003–2019 Ignite Realtime. All rights reserved.