Object - cache : Cache

Represents the Ballerina Cache object and 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.

put

(string key, any value, int maxAgeInSeconds)

returns Error?

Add the given key value pair to the cache. If the cache previously contained a value associated with key, the old value is replaced by value.

Parameters

  • key string
  • Key of the cached value

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

  • maxAgeInSeconds int (default -1)
  • The value in seconds, which the cache entry is valid. '-1' means, 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

(string key)

returns any | Error

Return the cached value associated with the given key.

Parameters

  • key string
  • Key which is used to retrieve the cached value

  • Return Type

    (any | Error)
  • The cached value associated with the given key or Error if the provided cache key is not or if any error occurred while retrieving from the cache.

invalidate

(string key)

returns Error?

Discard 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 Error if the provided cache key is not or if any error occurred while discarding from the cache.

invalidateAll

()

returns Error?

Discard all the cached values from the cache.

  • Return Type

    (Error?)
  • () if successfully discarded all or Error if any error occurred while discarding all from the cache.

hasKey

(string key)

returns boolean

Checks whether the given key has an associated cache value.

Parameters

  • key string
  • The key to be checked

  • Return Type

    (boolean)
  • Whether the an associated cache value is available in the cache or not

keys

()

returns string[]

Returns all keys from the cache.

  • Return Type

    (string[])
  • Array of all keys from the cache

size

()

returns int

Returns the size of the cache.

  • Return Type

    (int)
  • The size of the cache

capacity

()

returns int

Returns the capacity of the cache.

  • Return Type

    (int)
  • The capacity of the cache