An open API service indexing awesome lists of open source software.

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

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.