Skip to content

CacheManager

@blobs/sdk v0.1.0


@blobs/sdk / cache / CacheManager

Defined in: cache/CacheManager.ts:25

Top-level coordinator over a single CacheProvider.

CacheManager is what services interact with — it adds an enable/disable toggle, a getOrSet helper for cache-aside patterns, and pattern-based invalidation on top of the underlying provider. When constructed without a provider it auto-detects: LocalStorageCache in browsers (with a working localStorage) and MemoryCache everywhere else.

Example

const cache = new CacheManager();
const blob = await cache.getOrSet(
CacheKeys.blob(id),
() => api.fetchBlob(id),
60_000,
);
await cache.invalidate('GET:/blobs*');

Constructors

Constructor

new CacheManager(provider?, enabled?): CacheManager

Defined in: cache/CacheManager.ts:33

Parameters

provider?

CacheProvider

Optional cache provider; auto-detected when omitted.

enabled?

boolean = true

Whether caching starts enabled (default true).

Returns

CacheManager

Methods

setEnabled()

setEnabled(enabled): void

Defined in: cache/CacheManager.ts:76

Toggles caching on or off without losing the underlying provider.

When disabled, get and has return null/false and set is a no-op; remove and clear still flush.

Parameters

enabled

boolean

The new enabled state.

Returns

void


isEnabled()

isEnabled(): boolean

Defined in: cache/CacheManager.ts:85

Reports whether caching is currently active.

Returns

boolean

true when reads/writes flow through the provider.


get()

get<T>(key): Promise<T | null>

Defined in: cache/CacheManager.ts:97

Reads a value from the cache, or null when caching is disabled or the entry is missing/expired.

Type Parameters

T

T = any

Expected value type.

Parameters

key

string

The cache key to read.

Returns

Promise<T | null>

The cached value, or null.


set()

set<T>(key, value, ttl?): Promise<void>

Defined in: cache/CacheManager.ts:110

Writes a value into the cache. No-op when caching is disabled.

Type Parameters

T

T = any

Value type being stored.

Parameters

key

string

The cache key to write.

value

T

The value to store.

ttl?

number

Optional TTL in milliseconds; falls back to the provider’s default.

Returns

Promise<void>


remove()

remove(key): Promise<void>

Defined in: cache/CacheManager.ts:120

Removes a single entry from the cache regardless of enabled state.

Parameters

key

string

The cache key to remove.

Returns

Promise<void>


clear()

clear(): Promise<void>

Defined in: cache/CacheManager.ts:127

Clears every entry owned by the underlying provider.

Returns

Promise<void>


has()

has(key): Promise<boolean>

Defined in: cache/CacheManager.ts:137

Reports whether a key has a non-expired entry. Returns false when caching is disabled.

Parameters

key

string

The cache key to probe.

Returns

Promise<boolean>


keys()

keys(pattern?): Promise<string[]>

Defined in: cache/CacheManager.ts:147

Lists keys in the cache, optionally filtered by substring match.

Parameters

pattern?

string

Optional substring; only matching keys are returned.

Returns

Promise<string[]>


invalidate()

invalidate(pattern): Promise<void>

Defined in: cache/CacheManager.ts:159

Removes every key matching a substring pattern.

Pair with builders from InvalidationPatterns after a mutation (e.g. cache.invalidate('GET:/blobs*')).

Parameters

pattern

string

Substring matched against keys.

Returns

Promise<void>


removePattern()

removePattern(pattern): Promise<void>

Defined in: cache/CacheManager.ts:171

Alias for CacheManager.invalidate.

Parameters

pattern

string

Substring matched against keys.

Returns

Promise<void>


getOrSet()

getOrSet<T>(key, factory, ttl?): Promise<T>

Defined in: cache/CacheManager.ts:185

Cache-aside helper: returns the cached value, or invokes the factory and caches the result. Bypasses the cache entirely when caching is disabled.

Type Parameters

T

T

The value type produced by the factory.

Parameters

key

string

The cache key to use.

factory

() => Promise<T>

Async function that produces the value on cache miss.

ttl?

number

Optional TTL in milliseconds for the freshly-cached value.

Returns

Promise<T>

The cached or freshly-computed value.