Class: Cache

Isolated

The cache:Cache object, which is used for all the cache-related operations. It is not recommended to insert () as the value of the cache since it doesn't make any sense to cache a nil.

Constructor

Initializes new cache:Cache instance.

1cache:Cache cache = new(capacity = 10, evictionFactor = 0.2);
init (*CacheConfig cacheConfig)
  • cacheConfig *CacheConfig
  • Configurations for the cache:Cache object

Methods

put

Adds the given key value pair to the cache.

get

Returns the cached value associated with the provided key.

invalidate

Discards a cached value from the cache.

invalidateAll

Discards all the cached values from the cache.

hasKey

Checks whether the given key has an associated cached value.

keys

Returns a list of all the keys from the cache.

size

Returns the size of the cache.

capacity

Returns the capacity of the cache.

put

function put(string key, any value, decimal maxAge) returns Error?
Isolated Function

Adds the given key value pair to the cache. If the cache previously contained a value associated with the provided key, the old value wil be replaced by the newly-provided value.

1check cache.put("Hello", "Ballerina");

Parameters

  • key string
  • Key of the value to be cached

  • value any
  • Value to be cached. Value should not be ()

  • maxAge decimal (default -1)
  • The time in seconds for which the cache entry is valid. If the value is '-1', the entry is valid forever.

Return Type

(Error?)

() if successfully added to the cache or a cache:Error if a () value is inserted to the cache.

get

function get(string key) returns any | Error
Isolated Function

Returns the cached value associated with the provided key.

1any value = check cache.get(key);

Parameters

  • key string
  • Key of the cached value, which should be retrieved

Return Type

(any | Error)

The cached value associated with the provided key or a cache:Error if the provided cache key is not exisiting in the cache or any error occurred while retrieving the value from the cache.

invalidate

function invalidate(string key) returns Error?
Isolated Function

Discards a cached value from the cache.

1check cache.invalidate(key);

Parameters

  • key string
  • Key of the cache value, which needs to be discarded from the cache

Return Type

(Error?)

() if successfully discarded the value or a cache:Error if the provided cache key is not present in the cache

invalidateAll

function invalidateAll() returns Error?
Isolated Function

Discards all the cached values from the cache.

1check cache.invalidateAll();

Return Type

(Error?)

() if successfully discarded all the values from the cache or a cache:Error if any error occurred while discarding all the values from the cache.

hasKey

function hasKey(string key) returns boolean
Isolated Function

Checks whether the given key has an associated cached value.

1boolean result = cache.hasKey(key);

Parameters

  • key string
  • The key to be checked in the cache

Return Type

(boolean)

true if a cached value is available for the provided key or false if there is no cached value associated for the given key

keys

function keys() returns string[ ]
Isolated Function

Returns a list of all the keys from the cache.

1string[] keys = cache.keys();

Return Type

(string[ ])

Array of all the keys from the cache

size

function size() returns int
Isolated Function

Returns the size of the cache.

1int result = cache.size();

Return Type

(int)

The size of the cache

capacity

function capacity() returns int
Isolated Function

Returns the capacity of the cache.

1int result = cache.capacity();

Return Type

(int)

The capacity of the cache