Skip to content

Commit

Permalink
docs: memoExternal
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Mar 7, 2024
1 parent 533b033 commit c24ad4d
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ console.log(add(1, 2))

### memoAsync

It also supports memorize async function call.
It also supports memorize async function call. After invoking once for every specified arguments, the result is located in the local memory.

```ts
import { memoAsync } from 'memofunc'
Expand Down Expand Up @@ -84,7 +84,46 @@ await Promise.all([task1, task2, task3])

### memoExternal

> WIP.
The caching mechanism relies on an external asynchronous service and does not store results in the local memory.

Upon invoking the function, it will:

1. It retrieves results from the cache. For concurrent function calls, the cache is queried only once like `memoAsync`;
2. It either returns the cached results or calls the underlying function if the cache is empty.

This approach facilitates the development of caching solutions within distributed systems, such as Cloudflare Workers.

Consider a scenario where the goal of your proxied function is to query database. Once some distributed nodes update the database, you should invalidate the related cache. This ensures that other nodes, relying entirely on the external cache service, will access the latest data available.

```ts
let cnt = 0;
const func = memoExternal(async () => 0, {
external: {
async get() {
await sleep(100);
return ++cnt;
},
async set() {},
async clear() {
cnt = 0;
},
async remove() {
cnt = 0;
}
}
});

// It will call the external cache get function with cnt = 0
const tasks = await Promise.all([func(), func(), func(), func(), func()]);
expect(tasks).toStrictEqual([1, 1, 1, 1, 1]);

// Clear the cache, cnt = 0
func.clear();

// It will call the external cache get function with cnt = 0
const tasks2 = await Promise.all([func(), func(), func(), func(), func()]);
expect(tasks2).toStrictEqual([1, 1, 1, 1, 1]);
```

## License

Expand Down

0 comments on commit c24ad4d

Please sign in to comment.