Skip to main content

Class: CacheImpl<V>

@ima/core.CacheImpl

Configurable generic implementation of the Cache interface.

Example

if (cache.has('model.articles')) {
return cache.get('model.articles');
} else {
let articles = getArticlesFromStorage();
// cache for an hour
cache.set('model.articles', articles, 60 * 60 * 1000);
}

Type parameters

Name
V

Hierarchy

Constructors

constructor

new CacheImpl<V>(cacheStorage, factory, Helper, config)

Initializes the cache.

Type parameters

Name
V

Parameters

NameTypeDescription
cacheStorageStorage<CacheEntry<V>>The cache entry storage to use.
factoryCacheFactory<V>Which create new instance of cache entry.
Helper__moduleThe IMA.js helper methods.
configObjectThe cache configuration.

Overrides

Cache.constructor

Defined in

packages/core/src/cache/CacheImpl.ts:36

Properties

_Helper

Protected _Helper: __module

Defined in

packages/core/src/cache/CacheImpl.ts:24


_cache

Protected _cache: Storage<CacheEntry<V>>

Defined in

packages/core/src/cache/CacheImpl.ts:22


_enabled

Protected _enabled: boolean

Defined in

packages/core/src/cache/CacheImpl.ts:26


_factory

Protected _factory: CacheFactory<V>

Defined in

packages/core/src/cache/CacheImpl.ts:23


_ttl

Protected _ttl: number

Defined in

packages/core/src/cache/CacheImpl.ts:25

Methods

_canSerializeValue

Private _canSerializeValue(value): boolean

Tests whether the provided value can be serialized into JSON.

Parameters

NameTypeDescription
valueunknownThe value to test whether or not it can be serialized.

Returns

boolean

true if the provided value can be serialized into JSON, false otherwise.

Defined in

packages/core/src/cache/CacheImpl.ts:202


_clone

Private _clone(value): V

Attempts to clone the provided value, if possible. Values that cannot be cloned (e.g. promises) will be simply returned.

Parameters

NameTypeDescription
valueVThe value to clone.

Returns

V

The created clone, or the provided value if the value cannot be cloned.

Defined in

packages/core/src/cache/CacheImpl.ts:256


clear

clear(): void

Clears the cache by deleting all entries.

Returns

void

Overrides

Cache.clear

Defined in

packages/core/src/cache/CacheImpl.ts:66


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

Overrides

Cache.delete

Defined in

packages/core/src/cache/CacheImpl.ts:122


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
serializedDataObjectAn object representing the state of the cache to load, obtained by parsing the JSON string returned by the serialize method.

Returns

void

Overrides

Cache.deserialize

Defined in

packages/core/src/cache/CacheImpl.ts:181


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

Overrides

Cache.disable

Defined in

packages/core/src/cache/CacheImpl.ts:129


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

Overrides

Cache.enable

Defined in

packages/core/src/cache/CacheImpl.ts:137


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.

Overrides

Cache.get

Defined in

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


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.

Overrides

Cache.has

Defined in

packages/core/src/cache/CacheImpl.ts:73


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.

Overrides

Cache.serialize

Defined in

packages/core/src/cache/CacheImpl.ts:144


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

NameTypeDefault valueDescription
keystringundefinedThe identifier of the cache entry.
valueVundefinedThe cache entry value.
ttlstring | number0Cache 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

Overrides

Cache.set

Defined in

packages/core/src/cache/CacheImpl.ts:106