generated from templates/typescript-library
add 3 caching implementations for various use cases
This commit is contained in:
64
readme.md
64
readme.md
@@ -2,3 +2,67 @@
|
||||
In-memory caching/memoization utilities
|
||||
|
||||
---
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install --save @js/memo
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { memo } from '@js/memo';
|
||||
|
||||
function get_data_from_slow_source(name: string) : string {
|
||||
// ....
|
||||
}
|
||||
|
||||
const get_data_cached = memo({
|
||||
func: get_data_from_slow_source,
|
||||
ttl: 30_000,
|
||||
});
|
||||
```
|
||||
|
||||
### Custom cache key generation
|
||||
|
||||
```ts
|
||||
import { memo } from '@js/memo';
|
||||
|
||||
interface SomeObject {
|
||||
id: string;
|
||||
foo: string;
|
||||
bar: number;
|
||||
baz: {
|
||||
qux: string;
|
||||
};
|
||||
}
|
||||
|
||||
function get_data_from_slow_source(obj: SomeObject) : string {
|
||||
// ....
|
||||
}
|
||||
|
||||
const get_data_cached = memo({
|
||||
func: get_data_from_slow_source,
|
||||
ttl: 30_000,
|
||||
|
||||
// Pull just the `id` field from the first parameter to use
|
||||
// as the cache key
|
||||
key([ obj ]) {
|
||||
return obj.id;
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Simple functions with no params
|
||||
|
||||
```ts
|
||||
|
||||
import { memo_simple } from '@js/memo';
|
||||
|
||||
function get_data_from_slow_source() : string {
|
||||
// ....
|
||||
}
|
||||
|
||||
const get_data_cached = memo_simple(30_000, get_data_from_slow_source);
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user