3.0.2 • Published 10 months ago

keshi v3.0.2

Weekly downloads
14
License
ISC
Repository
github
Last release
10 months ago

Keshi

Keshi on NPM CI

Keshi is a tiny in-memory cache for Node and the browser that is especially suited to storing Promises (e.g. caching fetch requests).

import Keshi from 'keshi'
// or
const Keshi = require('keshi')
const cache = new Keshi()

const user = await cache.resolve(
  'user',
  () => fetch('https://myapi.com/user').then(r => r.json()),
  '5mins'
)

What this will do:

  • Fetch the user from the API as it doesn't have it in cache.
  • If called again within 5 minutes it will return the cached user.
  • If called after 5 minutes it will fetch the user again and re-cache.

Keshi automatically cleans up expired items.

cache.resolve<T>(key: string, getValue: () => T | Promise<T>, expiresIn?: number | string) => Promise<T>

function getCachedUser() {
  return cache.resolve(
    'user',
    () => fetch('https://myapi.com/user').then(r => r.json()),
    '5mins' // Anything 'ms' package accepts or milliseconds as a number. Omit for no expiry.
  )
}

const user1 = await getCachedUser() // First time caches the promise and returns it
const user2 = await getCachedUser() // Second time returns the first promise if within 5mins

You can use plain values but they must still be awaited:

const plainValue = await cache.resolve('mynumber', () => 5, '10mins')
console.log(plainValue) // prints 5

cache.delete(key: string, matchStart?: boolean)

Explicitly delete a cached object.

Note: expired objects are automatically cleanup up.

If true is passed for matchStart then any cache starting with the key will be deleted:

cache.del(`project.${projectId}.`, true) // Delete all caches under this projectId

cache.clear()

Clear the whole cache.

cache.teardown()

A stale cache cleanup interval is running in the background. If your cache doesn't last the lifetime of your application then you should call teardown.

3.0.2

10 months ago

3.0.1

10 months ago

3.0.0

10 months ago

2.0.7

5 years ago

2.0.5

5 years ago

2.0.6

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago