K - Indicates the key type of the stored value.V - Indicates the type of the stored value.public class LruBuffer<K,V> extends Object
You can specify the capacity of a buffer. When the number of key-value pairs in the buffer exceeds its preset
capacity, the LRU algorithm takes effect. Specifically, the least recently accessed key-value pairs will be removed
to make room for new key-value pairs. You can also reset the buffer capacity. If the newly set buffer capacity is
less than the number of already stored key-value pairs, the LRU algorithm is triggered to remove the least recently
accessed key-value pairs.
The following methods have an access token: put(java.lang.Object,java.lang.Object), get(java.lang.Object),
contains(java.lang.Object).If one of them has been successfully called and has executed an actual operation for a
value, this value will be marked as a recently accessed value and therefore its automatic clearance upon a full
buffer will be postponed.
Most methods of this class support the synchronized mechanism and therefore can be used in multi-thread or
concurrency scenarios.
This class also provides the getMatchCount(), getMissCount(), and getPutCount() methods
for you to obtain access statistics on stored key-value pairs, including the total numbers of matched queried
key-value pairs, unmatched queried key-value pairs, and added key-value pairs.
In addition, you can override the afterRemoval(boolean,java.lang.Object,java.lang.Object,java.lang.Object) method in this class to
perform subsequent operations after the stored key-value pairs are deleted or automatically cleared.
Note: The buffer capacity cannot be set to 0, and the key or value cannot be null.
| 构造器 | 说明 |
|---|---|
LruBuffer() |
Default constructor used to create a new
LruBuffer instance with the default capacity of 64. |
LruBuffer(int capacity) |
A constructor used to create a new
LruBuffer instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
protected void |
afterRemoval(boolean isEvict,
K key,
V value,
V newValue) |
Executes subsequent operations after a value is deleted.
|
int |
capacity() |
Obtains the capacity of the current buffer.
|
void |
clear() |
Clears key-value pairs from the current buffer.
|
boolean |
contains(K key) |
Checks whether the current buffer contains a specified key.
|
protected V |
createDefault(K key) |
Executes subsequent operations if miss to compute a value for the specific key.
|
V |
get(K key) |
Obtains the value associated with a specified key.
|
int |
getCreateCount() |
Obtains the number of times
createDefault(java.lang.Object) returned a value. |
int |
getMatchCount() |
Obtains the number of times that the queried values are successfully matched.
|
int |
getMissCount() |
Obtains the number of times that the queried values are not matched.
|
int |
getPutCount() |
Obtains the number of times that values are added to the buffer.
|
int |
getRemovalCount() |
Obtains the number of times that values are evicted from the buffer.
|
boolean |
isEmpty() |
Checks whether the current buffer is empty.
|
List<K> |
keys() |
Obtains a list of keys for the values in the current buffer.
|
V |
put(K key,
V value) |
Adds a key-value pair to the buffer.
|
Optional<V> |
remove(K key) |
Deletes a specified
key and its associated value from the current buffer. |
int |
size() |
Obtains the total number of values in the current buffer.
|
String |
toString() |
Returns a string representation of the object.
|
void |
updateCapacity(int newCapacity) |
Updates the buffer capacity to a specified capacity.
|
List<V> |
values() |
Obtains a list of all values in the current buffer.
|
public LruBuffer()
LruBuffer instance with the default capacity of 64.public LruBuffer(int capacity)
LruBuffer instance.capacity - Indicates the capacity to customize for the buffer.IllegalArgumentException - Throws this exception if capacity is less than or equal to 0.protected void afterRemoval(boolean isEvict,
K key,
V value,
V newValue)
This method is automatically called when the remove(java.lang.Object), clear(),
put(java.lang.Object,java.lang.Object), or updateCapacity(int) method is used. You can determine whether to
override this method based on the features of the stored value.
isEvict - The parameter value is true if this method is called due to insufficient capacity, and the
parameter value is false in other cases.key - Indicates the deleted key.value - Indicates the deleted value.newValue - The parameter value is the new value associated if the put(java.lang.Object,java.lang.Object) method is
called and the key to add already exists. The parameter value is null in other cases.protected V createDefault(K key)
This method is automatically called if after get(java.lang.Object) miss to compute a value for the key.
You can determine whether to override this method based on the features of the stored value.
key - Indicates the missed key.public final int capacity()
public final void clear()
afterRemoval(boolean,java.lang.Object,java.lang.Object,java.lang.Object) method is called to execute subsequent operations for them
in sequence.public final int getMatchCount()
public final int getMissCount()
public final int getPutCount()
public final boolean contains(K key)
key - Indicates the key to check.true if the buffer contains the specified key.public final V get(K key)
key - Indicates the key to query.null otherwise.public final int getCreateCount()
createDefault(java.lang.Object) returned a value.createDefault(java.lang.Object) returned a value.public final boolean isEmpty()
true if the current buffer contains no value.public final List<K> keys()
public final V put(K key, V value)
If the current buffer is full, the least recently accessed key-value pair will be automatically cleared to make
room for adding the new key-value pair. If the key to be added already exists in the buffer, its
associated value will be replaced by value to be added.
key - Indicates the key to add.value - Indicates the value associated with the key to add.key; returns the original value if the key to
add already exists.NullPointerException - Throws this exception if key or value is null.public final int getRemovalCount()
public final Optional<V> remove(K key)
key and its associated value from the current buffer.
After the associated value is deleted, the afterRemoval(boolean,java.lang.Object,java.lang.Object,java.lang.Object) method
is automatically called to perform subsequent operations for the deleted value.
key - Indicates the key to delete.Optional object containing the deleted key-value pair; returns an empty
Optional object if the key does not exist.NullPointerException - Throws this exception if key is null.public final int size()
public String toString()
ObjecttoString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public final void updateCapacity(int newCapacity)
If the specified capacity is less than the total number of values stored in the buffer, values are cleared based on their access frequency from least recently accessed to most recently accessed, until the number equals to the specified capacity.
newCapacity - Indicates the new capacity to set.IllegalArgumentException - Throws this exception if newCapacity is less than or equal to 0.