K - V - public class LFUCache<K,V> extends Object
A simple thread-safe LFU cache.
Disclaim: the source code is adapted from https://github.com/Tsien/LFUCache/
| Constructor and Description |
|---|
LFUCache(int cap,
double evictFactor)
Create a new LFU cache.
|
| Modifier and Type | Method and Description |
|---|---|
Integer |
decr(K key,
Integer delta)
Decrements the value stored at key by delta.
|
V |
get(K key)
Get the value of key.
|
Integer |
incr(K key,
Integer delta)
Increments the value stored at key by delta.
|
Map<K,V> |
mget(List<K> keys)
Returns the values of all specified keys.
|
void |
mset(Map<K,V> data)
Sets the given keys to their respective values.
|
void |
print()
Only for testing purpose Print the content of the cache in the order of frequency
|
void |
set(K key,
V value)
Set key to hold the value.
|
public LFUCache(int cap,
double evictFactor)
Create a new LFU cache.
cap - the size of the cacheevictFactor - the percentage of elements for replacementpublic V get(K key)
Get the value of key. If the key does not exist, return null.
key - the key to querypublic void set(K key, V value)
Set key to hold the value. If key already holds a value, it is overwritten.
key - the key of the nodevalue - the value of the nodepublic Map<K,V> mget(List<K> keys)
Returns the values of all specified keys. For every key that does not exist, null is returned.
keys - a list of keys to querypublic void mset(Map<K,V> data)
Sets the given keys to their respective values. MSET replaces existing values with new values, just as regular SET.
data - a map contains the key/val pairs to be set.public Integer incr(K key, Integer delta)
Increments the value stored at key by delta. If the key does not exist, it is set to 0 before performing the operation. Only works for integer value. This function will increase frequency by 1
key - the key needed to be increaseddelta - incrementpublic Integer decr(K key, Integer delta)
Decrements the value stored at key by delta. If the key does not exist, it is set to 0 before performing the operation. Only works for integer value. This function will increase frequency by 2
key - the key needed to be decreaseddelta - decrementpublic void print()
Only for testing purpose Print the content of the cache in the order of frequency
Copyright © 2014–2021 OSGL (Open Source General Library). All rights reserved.