Skip to content

sjdonado/cache-manager-bun-sqlite3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bun SQLite Store for node-cache-manager

  • Runs on top of bun-sqlite
  • Optimized mset/mget support
  • Multiple encoders support: msgpackr, cbor, json
  • Auto purge (clean expired records every hour)

Installation

bun add cache-manager-bun-sqlite3

Usage

Single store

import cacheManager from 'cache-manager';
import bunSqliteStore from 'cache-manager-bun-sqlite3';

// SQLite :memory: cache store
cache = await cacheManager.caching(bunSqliteStore, {
  serializer: 'json', // default is 'msgpackr'
  ttl: 20, // TTL in seconds
});

// On-disk cache on employees table
const cache = await cacheManager.caching(bunSqliteStore, {
  name: 'employees',
  path: '/tmp/cache.db',
});

// TTL in seconds
await cache.set('foo', { test: 'bar' }, 30);
const value = await cache.get('foo');

// TTL in seconds
await cache.set('foo', { test: 'bar' }, 30);
const value = await cache.get('foo');

Multi-store example:

import cacheManager from 'cache-manager';
import bunSqliteStore from 'cache-manager-bun-sqlite3';
import redisStore from 'cache-manager-ioredis';

const redisCache = await cacheManager.caching({ store: redisStore, db: 0, ttl: 600 });
const sqliteCache = await cacheManager.caching({
  store: sqliteStore,
  path: '/tmp/cache.db',
  name: 'users',
  ttl: 600,
});

const multiCache = cacheManager.multiCaching([sqliteCache, redisCache]);

// Basic get/set
await multiCache.set('foo2', 'bar2', customTTL);
const v = await multiCache.get('foo2');

// Wrap call example
const userId = 'user-1';

// Optionally pass ttl
await multiCache.wrap(userId, customTTL, async () => {
  console.log('Calling expensive service');
  await getUserFromExpensiveService(userId);
});

// set and get multiple
await multiCache.mset('foo1', 'bar1', 'foo0', 'bar0'); //Uses default TTL
await multiCache.mset('foo1', 'bar1', 'foo3', 'bar3', customTTL);
await multiCache.mget('foo1', 'foo3');

Fork from node-cache-manager-sqlite