@sebspark/promise-cache
TypeScript icon, indicating that this package has built-in type declarations

0.2.9 • Public • Published

@sebspark/promise-cache

A simple caching wrapper for promises.

/*
 * Pseudocode example.
 */
import { PromiseCache } from '@sebspark/promise-cache'

// Instantiate the cache with a TTL.
const cache = new PromiseCache<number>(60, true, true) // 1 minute cache TTL / is case sensitive / use local storage

const redisCache = new PromiseCache<number>(60, false, false) // 1 minute cache TTL / is not case sensitive / use redis storage

// Use the cache wrapper for a database query to relieve the database.
const query = 'SELECT username FROM users ORDER BY created DESC LIMIT 1'
const newestUser = await cache.wrap('newestUser', () => database.query(query))

// Use the cache wrapper for a database query to relieve the database.
const query = 'SELECT username FROM users ORDER BY created DESC LIMIT 1'
const newestUser = await redisCache.wrap('newestUserRedis', () => database.query(query))

Readme

Keywords

none

Package Sidebar

Install

npm i @sebspark/promise-cache

Weekly Downloads

523

Version

0.2.9

License

Apache-2.0

Unpacked Size

30.4 kB

Total Files

7

Last publish

Collaborators

  • believer
  • raduachim
  • alexanderczigler
  • johanobrink