Class: Cache
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
Called when a new cache:Cache
object is created.
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. |
Fields
- Fields Included from *AbstractCache
put
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.
1cache:Error? result = 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 Error
if a ()
value is inserted to the cache.
get
Returns the cached value associated with the provided key.
1any|cache:Error value = 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 an Error
if the provided cache key is not
exisiting in the cache or any error occurred while retrieving the value from the cache.
invalidate
Discards a cached value from the cache.
1cache:Error? result = 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 an Error
if the provided cache key is not present in the
cache
invalidateAll
function invalidateAll() returns Error?
Discards all the cached values from the cache.
1cache:Error? result = cache.invalidateAll();
Return Type
(Error?)()
if successfully discarded all the values from the cache or an Error
if any error occurred while
discarding all the values from the cache.
hasKey
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