https://github.com/ken107/memoize
Extensible memoize that allows plugin arbitrary cache implementation, supporting multiple layers of caches
https://github.com/ken107/memoize
cache memcached memoize nodejs
Last synced: 4 months ago
JSON representation
Extensible memoize that allows plugin arbitrary cache implementation, supporting multiple layers of caches
- Host: GitHub
- URL: https://github.com/ken107/memoize
- Owner: ken107
- License: mit
- Created: 2019-12-19T08:46:56.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-28T03:39:42.000Z (about 2 years ago)
- Last Synced: 2025-01-30T10:31:09.127Z (over 1 year ago)
- Topics: cache, memcached, memoize, nodejs
- Language: TypeScript
- Size: 663 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Usage
```typescript
import memoize from "extensible-memoize";
const getItem = memoize(fetchItem);
//define your fetch function:
function fetchItem(key: K): Promise {
//fetch the item identified by key
}
```
*Note*: `K` must be one of `void`, `string`, or `extends {hashKey: string}` (an object with a hashKey property)
### Caching
By default, memoize uses a mem cache with no expiration. To specify your own caching implementation, pass an array of Caches as the second parameter.
```typescript
interface Cache {
get: (key: K) => Promise
set: (key: K, value: V) => Promise
}
const getItem = memoize(fetchItem, [cache1, cache2, ...]);
```
Memoize will first look for the item in `cache1`, then in `cache2`, and so on. If the item is not found in the caches, `fetchItem` will be called. Note that your cache implementations do not have to worry about dealing with concurrency, memoize will take care of that.