https://github.com/dominictobias/keshi
An in-memory cache for Node and the browser designed for Promises
https://github.com/dominictobias/keshi
async cache in-memory javascript nodejs store
Last synced: 18 days ago
JSON representation
An in-memory cache for Node and the browser designed for Promises
- Host: GitHub
- URL: https://github.com/dominictobias/keshi
- Owner: dominictobias
- Created: 2019-01-09T13:50:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-16T14:04:42.000Z (3 months ago)
- Last Synced: 2025-03-28T13:06:02.851Z (25 days ago)
- Topics: async, cache, in-memory, javascript, nodejs, store
- Language: TypeScript
- Homepage:
- Size: 225 KB
- Stars: 76
- Watchers: 2
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Keshi
[](https://www.npmjs.com/package/keshi)
[](https://github.com/sekoyo/keshi/actions/workflows/ci.yml)Keshi is a tiny in-memory cache for Node and the browser that is especially suited to storing Promises (e.g. caching fetch requests).
```js
import Keshi from 'keshi'
// or
const Keshi = require('keshi')
```Usage
```js
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.
API
#### `cache.resolve(key: string, getValue: () => T | Promise, expiresIn?: number | string) => Promise`
```ts
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:
```ts
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:
```js
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.