Class LruCacheLong<V>
java.lang.Object
gaiasky.util.LruCacheLong<V>
- Type Parameters:
V- The type of values stored in the cache.
A fixed-capacity cache that uses a least-recently-used (LRU) eviction policy.
This implementation uses primitive long keys to avoid boxing overhead.
-
Constructor Summary
ConstructorsConstructorDescriptionLruCacheLong(int capacity) Constructs an LRU cache with the specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all entries from the cache.booleancontainsKey(long key) Checks if the cache contains the specified key.get(long key) Retrieves the value associated with the specified key.Retrieves the least recently used entry without removing it.voidAdds a key-value pair to the cache or updates the value if the key already exists.remove(long key) Removes the entry associated with the specified key.intsize()Returns the current number of entries in the cache.
-
Constructor Details
-
LruCacheLong
public LruCacheLong(int capacity) Constructs an LRU cache with the specified capacity.- Parameters:
capacity- the maximum number of entries in the cache
-
-
Method Details
-
getLeastRecentlyUsed
Retrieves the least recently used entry without removing it.- Returns:
- the value of the least recently used entry, or null if the cache is empty
-
get
Retrieves the value associated with the specified key.- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value associated with the specified key, or null if not found
-
put
Adds a key-value pair to the cache or updates the value if the key already exists.- Parameters:
key- the keyvalue- the value
-
containsKey
public boolean containsKey(long key) Checks if the cache contains the specified key.- Parameters:
key- the key to check- Returns:
- true if the key is present, false otherwise
-
remove
Removes the entry associated with the specified key.- Parameters:
key- the key to remove- Returns:
- the removed value, or null if the key was not present
-
size
public int size()Returns the current number of entries in the cache.- Returns:
- the size of the cache
-
clear
public void clear()Clears all entries from the cache.
-