PCache

org.eso.ias.utils.pcache.PCache
class PCache(val maxSize: Integer, val maxMemSize: Integer)

PCache is a cache that optionally persists objects in a DB or a file or other means (depending on the implementation).

Objects implementing this class store in memory up to n objects or up to the size of the objects in memory passes a given threshold; when the in-memory cache is filled, new objects are persisted.

Giving a max number of objects (or memory used by them) equals to 0 force all objects to be persisted. The first threshold that is passed )(num. of items or memory consumption) triggers the storage of new item in the non-volatile cache.

The way objects are persisted depends on the implementation, it can be on file or a RDBMS or other means) and it is transparent to the users.

Each object persisted in PCache is identified by a key (pretty much like a Map). In the scope of the IAS, it makes sense to store in the cache JSON strings as almost everything can be translated to/from JSON strings and has an ID (IASIOs, CONVERTERS...).

The memory used by the objects in the map consists on the sum of the sizes of the keys plus the sizes of the JSON strings i.e. PCache does not take into account the memory used to store the objects (for example if the objects are stored in a Map, the memory does not consider the memory used by the Map itself). There are more accurate solutions but we do not want to instrument the code or add complexity when not strictly necessary.

Besides the key and the JSON string, what PCache ultimately stores depends on the memory and persistence implementations.

There are many caches available on the open source world but actually in the IAS the need for a cache is to store objects in memory avoiding the risk of OoM. The objects will be put in cache and retrieved by their IDs (pretty much a single table of a RDBMS). As such, PCache is basically a Map protected against OoM.

PCache delegates the in-memory cache to implementers of InMemoryCache that can either use scala/java code or delegate to third party open source tools. For persisting objects in non-volatile memory, (implementers of NonVolatileCache), PCache delegates to open source tools. Implementers of InMemoryCache can rely on external services (elasticsearch, redis...) especially if such services are already available in the system.

Value parameters

maxMemSize

The max size (MBytes) of memory that can be used by the object in memory

maxSize

The max number of items to keep in memory

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def clear(): Unit

Empty the cahce

Empty the cahce

Attributes

def del(key: String): Boolean

Remove an object from the cache

Remove an object from the cache

Value parameters

key

The key of the object to remove

Attributes

Returns

True if the object has been removed, False otherwise (for example the object was not in the cache)

def get(key: String): Option[String]

Get an object from the cache

Get an object from the cache

Value parameters

key

The key of the object to get

Attributes

Returns

The Object in the cache if it exists, empty otherwise

def inMemoryKeySet: Set[String]

Collects all keys of the volatile cache in a set

Collects all keys of the volatile cache in a set

Attributes

def inMemorySize: Int

Attributes

Returns

the number of objects in memory

def isEmpty: Boolean

Attributes

Returns

true if the cache is empty; false otherwise

def jget(key: String): Optional[String]

Get an object from the cache (support method to avoid that java code deals with the scala Option)

Get an object from the cache (support method to avoid that java code deals with the scala Option)

Value parameters

key

The key of the object

Attributes

Returns

The Object in the cache if it exists, empty otherwise

def keySet: Set[String]

Collects all keys of this map in a set

Collects all keys of this map in a set

Attributes

def nonEmpty: Boolean

Attributes

Returns

true if the cache is not empty; false otherwise

def nonVolatileKeySet: Set[String]

Collects all keys of the volatile cache in a set

Collects all keys of the volatile cache in a set

Attributes

def nonVolatileSize: Int

Attributes

Returns

The number of objects persisted

def put(key: String, value: String): Boolean

Puts/update a value in the cache

Puts/update a value in the cache

Value parameters

key

The unique key to identify the object

value

: The string to store in the cache

Attributes

Returns

true if the object has been stored in the cache, false otherwise

def putAll(items: List[(String, String)]): Boolean

Put all the items in the cache

Put all the items in the cache

Value parameters

items

The list of (key, value) object to add to the cache

Attributes

Returns

true if all the items have been successfully added to the list; false otherwise

def size: Int

Attributes

Returns

the number of objects in the cache (both in memory and non-volatile)

Concrete fields

val maxMemSize: Integer
val maxSize: Integer