cache

ballerina/cache Ballerina library
Classes
cache: 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
Initializes new cache:Cache
instance.
cache:Cache cache = new(capacity = 10, evictionFactor = 0.2);
init (*CacheConfig cacheConfig)
- cacheConfig *CacheConfig - Configurations for the
cache:Cache
object
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 will be replaced by the newly-provided value.
check 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 acache:Error
if a()
value is inserted to the cache.
get
Returns the cached value associated with the provided key.
any 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
Discards a cached value from the cache.
check 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 acache: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.
check cache.invalidateAll();
Return Type
- Error? -
()
if successfully discarded all the values from the cache or acache:Error
if any error occurred while discarding all the values from the cache.
hasKey
Checks whether the given key has an associated cached value.
boolean 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 orfalse
if there is no cached value associated for the given key
keys
function keys() returns string[]
Returns a list of all the keys from the cache.
string[] keys = cache.keys();
Return Type
- string[] - Array of all the keys from the cache
size
function size() returns int
Returns the size of the cache.
int result = cache.size();
Return Type
- int - The size of the cache
capacity
function capacity() returns int
Returns the capacity of the cache.
int result = cache.capacity();
Return Type
- int - The capacity of the cache
Enums
cache: EvictionPolicy
Possible types of eviction policy that can be passed into the EvictionPolicy
.
Members
Records
cache: CacheConfig
Represents configurations for the cache:Cache
object.
Fields
- capacity int(default 100) - Maximum number of entries allowed in the cache
- evictionFactor float(default 0.25) - The factor by which the entries will be evicted once the cache is full
- evictionPolicy EvictionPolicy(default LRU) - The policy which is used to evict entries once the cache is full
- defaultMaxAge decimal(default -1) - The max-age in seconds which all the cache entries are valid. '-1' means, the entries are
valid forever. This will be overwritten by the
maxAge
property set when inserting item into the cache
- cleanupInterval? decimal - Interval (in seconds) of the timer task, which will clean up the cache
Errors
cache: Error
Represents Cache related errors. This will be returned if an error occurred while doing any of the cache operations.
Object types
cache: AbstractCache
The cache:AbstractCache
object is used for custom implementations of the Ballerina cache.
Any custom cache implementation should be object-wise similar.
put
Adds the given key value pair to the cache. If the cache previously contained a value associated with the key, the old value is replaced by the new value.
Parameters
- key string - Key of the value to be cached
- value any - Value to be cached
- maxAge decimal (default -1) - The time in seconds during which the cache entry is valid. '-1' means, the entry is valid forever
Return Type
- Error? -
()
if successfully added to the cache orError
if any error occurred while inserting the entry to the cache
get
Returns the cached value associated with the provided key.
Parameters
- key string - The key used to retrieve the cached value
Return Type
- any|Error - The cached value associated with the given key or an
Error
if the provided cache key is not available or if any error occurred while retrieving from the cache
invalidate
Discards a cached value from the cache.
Parameters
- key string - Key of the cache entry which needs to be discarded
Return Type
- Error? -
()
if successfully discarded or anError
if the provided cache key is not available or if any error occurred while discarding from the cache
invalidateAll
function invalidateAll() returns Error?
Discards all the cached values from the cache.
Return Type
- Error? -
()
if successfully discarded all or anError
if any error occurred while discarding all from the cache
hasKey
Checks whether the given key has an associated cache value.
Parameters
- key string - The key to be checked
Return Type
- boolean -
true
if an associated cache value is available for the provided key orfalse
if there is not a cache value associated with the provided key
keys
function keys() returns string[]
Returns all keys from the cache.
Return Type
- string[] - Array of all the keys from the cache
size
function size() returns int
Returns the current size of the cache.
Return Type
- int - The size of the cache
capacity
function capacity() returns int
Returns the capacity of the cache.
Return Type
- int - The capacity of the cache
Import
import ballerina/cache;
Metadata
Released date: 5 months ago
Version: 3.9.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.11.0
GraalVM compatible: Yes
Pull count
Total: 90088
Current verison: 16
Weekly downloads
Keywords
cache
LRU
Contributors