LRUCache

com.github.cloudfiles.core.utils.LRUCache
See theLRUCache companion object
class LRUCache[K, +V]

An implementation of a LRU cache.

This class implements a cache with a given capacity. Instances are immutable and allow fast access to the keys they contain. When elements are added (either new elements or ones that already exist in the cache) they are put in the front of the cache; if the cache's capacity is reached, elements at the end are discarded.

The intended usage scenario is that a number of read operations is performed on an instance. Based on these operations it is determined which new elements are to be added or which ones need to be put in front. The corresponding updates are then triggered leading to a new instance.

Type parameters

K

the type of keys

V

the type of values

Value parameters

capacity

the capacity of the cache

entries

a map storing the data of the cache

lruList

a map storing LRU-related information

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def apply(key: K): V

Returns the value of the given key. If the key is not found in the cache, an exception is thrown.

Returns the value of the given key. If the key is not found in the cache, an exception is thrown.

Value parameters

key

the key in question

Attributes

Returns

the value of this key

Throws
NoSuchElementException

if the key cannot be found

def contains(key: K): Boolean

Checks whether the given key is contained in this cache.

Checks whether the given key is contained in this cache.

Value parameters

key

the key in question

Attributes

Returns

a flag whether this key is contained in this cache

def get(key: K): Option[V]

Returns an ''Option'' with the value of the given key. If the key is not found in the cache, result is ''None''.

Returns an ''Option'' with the value of the given key. If the key is not found in the cache, result is ''None''.

Value parameters

key

the key in question

Attributes

Returns

an ''Option'' with the value of this key

def getLRU(key: K): (Option[V], LRUCache[K, V])

Returns an ''Option'' with the value of the given key and a copy of this instance with the key put in front of the LRU list (if it exists). If the key cannot be found, this instance is returned unchanged.

Returns an ''Option'' with the value of the given key and a copy of this instance with the key put in front of the LRU list (if it exists). If the key cannot be found, this instance is returned unchanged.

Value parameters

key

the key in question

Attributes

Returns

a tuple with the value of this key and the cache instance with the updated LRU list

def getOrPut[U >: V](key: K)(create: K => U): (U, LRUCache[K, U])

Queries the given key and returns its value if it is found. Otherwise, the given creator function is invoked with the key, and a pair with the key and the return value is added to this cache. In both cases, the key is now in front of the LRU list.

Queries the given key and returns its value if it is found. Otherwise, the given creator function is invoked with the key, and a pair with the key and the return value is added to this cache. In both cases, the key is now in front of the LRU list.

Value parameters

create

the function to create a new value

key

the key in question

Attributes

Returns

a tuple with the value of the key and the cache instance with the updated LRU list

def keySet: Set[K]

Returns a set with all the keys that are currently contained in this cache.

Returns a set with all the keys that are currently contained in this cache.

Attributes

Returns

the current key set of this cache

def put[U >: V](pairs: (K, U)*): LRUCache[K, U]

Adds or updates the given elements to the cache. If keys in the list already exist, they are removed and then added with the new value (which causes them to be moved to the front). If necessary, elements at the end of the LRU list are removed.

Adds or updates the given elements to the cache. If keys in the list already exist, they are removed and then added with the new value (which causes them to be moved to the front). If necessary, elements at the end of the LRU list are removed.

Value parameters

pairs

the entries to be put in the cache

Attributes

Returns

the updated instance of this cache

def size: Int

Returns the number of elements contained in this cache.

Returns the number of elements contained in this cache.

Attributes

Returns

the number of contained elements

Concrete fields

val capacity: Int