Skip to main content

Class: Cache<V>

@ima/core.Cache

The cache provides a temporary storage for expirable information. The primary use of a cache is caching information obtained via costly means (CPU-heavy computation or networking) to speed up the application's performance when the same information needs to be retrieved multiple times.

Type parameters

NameType
Vunknown

Hierarchy

Constructors

constructor

new Cache<V>()

Type parameters

NameType
Vunknown

Methods

clear

clear(): void

Clears the cache by deleting all entries.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:15


delete

delete(key): void

Deletes the specified cache entry. The method has no effect if the entry does not exist.

Parameters

NameTypeDescription
keystringThe identifier of the cache entry.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:69


deserialize

deserialize(serializedData): void

Loads the provided serialized cache data into this cache. Entries present in this cache but not specified in the provided data will remain in this cache intact.

Parameters

NameTypeDescription
serializedDataSerializedData<V>An object representing the state of the cache to load, obtained by parsing the JSON string returned by the serialize method.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:118


disable

disable(): void

Disables the cache, preventing the retrieval of any cached entries and reporting all cache entries as non-existing. Disabling the cache does not however prevent modifying the existing or creating new cache entries.

Disabling the cache also clears all of its current entries.

The method has no effect if the cache is already disabled.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:83


enable

enable(): void

Enables the cache, allowing the retrieval of cache entries.

The method has no effect if the cache is already enabled.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:92


get

get(key): null | V

Returns the value of the entry identified by the specified key.

The method returns null if the specified entry does not exist, has already expired, or the cache is currently disabled.

Parameters

NameTypeDescription
keystringThe identifier of the cache entry.

Returns

null | V

The value of the specified cache entry, or null if the entry is not available.

Defined in

packages/core/src/cache/Cache.ts:43


has

has(key): boolean

Tests whether the cache contains a fresh entry for the specified key. A cache entry is fresh if the has not expired its TTL (time to live).

The method always returns false if the cache is currently disabled.

Parameters

NameTypeDescription
keystringThe identifier of the cache entry.

Returns

boolean

true if the cache is enabled, the entry exists and has not expired yet.

Defined in

packages/core/src/cache/Cache.ts:29


serialize

serialize(): string

Exports the state of this cache to an HTML-safe JSON string. The data obtained by parsing the result of this method are compatible with the deserialize method.

Returns

string

A JSON string containing an object representing of the current state of this cache.

Defined in

packages/core/src/cache/Cache.ts:104


set

set(key, value, ttl?): void

Sets the cache entry identified by the specified key to the provided value. The entry is created if it does not exist yet.

The method has no effect if the cache is currently disabled.

Parameters

NameTypeDescription
keystringThe identifier of the cache entry.
valueVThe cache entry value.
ttl?string | numberCache entry time to live in milliseconds. The entry will expire after the specified amount of milliseconds. Use null or omit the parameter to use the default TTL of this cache.

Returns

void

Defined in

packages/core/src/cache/Cache.ts:59