1.1.7 • Published 11 months ago
@bondsports/cache v1.1.7
Cache
Cache is a module to encapsulate generic caching functionality under simple interface
It provides two interfaces BaseCache and SimpleCache
Both were designed to be used to cache plain javascript objects containing id:number
field for identification
BaseCache is a low level one to have more control over the process.
SimpleCache is better suited if you want to have separate cache holders for different kind of objects
Usage
BaseCache
import { BaseCache, MemoryStore } from '@bondsports/cache';
const cache = new BaseCache(new MemoryStore(), console.log, true);
const program = cache.getOrFetchOne('programs', 12, async missingId => ({ id: 12, name: 'program 12' }));
const programs = cache.getOrFetch('programs', [12, 13], async missingIds => [
{ id: 12, name: 'program 12' },
{ id: 13, name: 'program 13' },
]);
cache.invalidateOne('programs', 12);
cache.invalidate('programs', [13]);
SimpleCache
import { BaseCache, MemoryStore } from '@bondsports/cache';
const cache = new SimpleCache(new MemoryStore(), 'programs', async missingIds => [
{ id: 12, name: 'program 12' },
{ id: 13, name: 'program 13' },
]);
const program = cache.getOrFetchOne(12);
const programs = cache.getOrFetch([12, 13]);
cache.invalidateOne(12);
cache.invalidateAll();
SimpleCache with Redis as a store
import { RedisClientType, createClient } from '@redis/client';
import { RedisStore } from '@bondsports/cache';
const client = createClient({ url: 'redis://localhost:6379' });
const store = new RedisStore(client, 1000);
const cache = new SimpleCache(store, 'programs', async missingIds => [
{ id: 12, name: 'program 12' },
{ id: 13, name: 'program 13' },
]);
const program = cache.getOrFetchOne(12);
const programs = cache.getOrFetch([12, 13]);
cache.invalidateAll();
SimpleCache with Redis as a store
import { RedisClientType, createClient } from '@redis/client';
import { RedisStore } from '@bondsports/cache';
const client = createClient({ url: 'redis://localhost:6379' });
const store = new RedisStore(client, 1000);
const cache = new SimpleCache(store, 'programs', async missingIds => [
{ id: 12, name: 'program 12' },
{ id: 13, name: 'program 13' },
]);
const program = cache.getOrFetchOne(12);
const programs = cache.getOrFetch([12, 13]);
1.1.7
11 months ago
1.1.6
11 months ago
1.1.4
11 months ago
1.1.3
11 months ago
1.1.2-ido-json-cache
11 months ago
1.1.4-ido-test
11 months ago
1.1.1
1 year ago
1.1.2
1 year ago
1.1.0-ido-test
1 year ago
1.1.0-ido-test-class-transformer
1 year ago
1.1.1-ido-only-fetch
1 year ago
1.1.0
1 year ago
1.0.8
2 years ago
1.0.7
2 years ago
1.0.4-ido
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago