Skip to main content

Class: CacheImpl<V>

Defined in: packages/core/src/cache/CacheImpl.ts:21

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);
}

Extends

Type Parameters

V

Constructors

new CacheImpl()

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

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

Initializes the cache.

Parameters

cacheStorage

Storage<CacheEntry<V>>

The cache entry storage to use.

factory

CacheFactory<V>

Which create new instance of cache entry.

Helper

__module

The IMA.js helper methods.

config

The cache configuration.

enabled

boolean = false

ttl

number = 30000

Returns

CacheImpl<V>

Overrides

Cache.constructor

Properties

_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


_Helper

protected _Helper: __module

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


_ttl

protected _ttl: number

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

Methods

clear()

clear(): void

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

Clears the cache by deleting all entries.

Returns

void

Overrides

Cache.clear


delete()

delete(key): void

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

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

Parameters

key

string

The identifier of the cache entry.

Returns

void

Overrides

Cache.delete


deserialize()

deserialize(serializedData): void

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

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

serializedData

An object representing the state of the cache to load, obtained by parsing the JSON string returned by the Cache#serialize method.

Returns

void

Overrides

Cache.deserialize


disable()

disable(): void

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

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


enable()

enable(): void

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

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


get()

get(key): null | V

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

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

key

string

The 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


has()

has(key): boolean

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

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

key

string

The identifier of the cache entry.

Returns

boolean

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

Overrides

Cache.has


serialize()

serialize(): string

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

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 Cache#deserialize method.

Returns

string

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

Overrides

Cache.serialize


set()

set(key, value, ttl): void

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

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

key

string

The identifier of the cache entry.

value

V

The cache entry value.

ttl

Cache 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.

string | number

Returns

void

Overrides

Cache.set