Skip to content

Commit 86a999c

Browse files
committed
docs: add multicache and synccache to docs
1 parent 6105151 commit 86a999c

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

ts-cache/README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Simple and extensible caching module supporting decorators
99

1010
<!-- TOC depthTo:2 -->
1111

12-
- [node-ts-cache](#node-ts-cache)
12+
- [node-ts-cache](#with-decorator)
1313
- [Install](#install)
1414
- [Usage](#usage)
1515
- [With decorator](#with-decorator)
@@ -35,11 +35,15 @@ Caches function response using the given options. Works with different strategie
3535

3636
`@Cache(strategy, options)`
3737

38-
- `strategy`: A supported caching [Strategy](#strategies)
38+
Standard method to cache an async method.
39+
40+
- `strategy`: A supported caching [Strategy](#strategies) (Async)
3941
- `options`: Options passed to the strategy for this particular method
4042

4143
_Note: @Cache always converts the method response to a promise because caching might be async._
4244

45+
E.g.
46+
4347
```ts
4448
import { Cache, ExpirationStrategy, MemoryStorage } from "@hokify/node-ts-cache";
4549

@@ -53,6 +57,64 @@ class MyService {
5357
}
5458
```
5559

60+
`@SyncCache(strategy, options)`
61+
Method to use only sync caches. This allows you to use caching without a promise function.
62+
63+
- `strategy`: A supported caching [Strategy](#strategies) (Sync)
64+
- `options`: Options passed to the strategy for this particular method
65+
66+
E.g.
67+
68+
```ts
69+
import { SyncCache, ExpirationStrategy, MemoryStorage } from "@hokify/node-ts-cache";
70+
71+
const myStrategy = new ExpirationStrategy(new MemoryStorage());
72+
73+
class MyService {
74+
@SyncCache(myStrategy, { ttl: 60 })
75+
public getUsers(): string[] {
76+
return ["Max", "User"];
77+
}
78+
}
79+
```
80+
81+
`@MultiCache(strategy, parameterIndex, cacheKey, options)`
82+
This method uses multi get and multi set of the cache providers if supported and therefore can use
83+
different input paramters and still cache each variation.
84+
85+
- `strategies`: A list of caching [Strategy](#strategies), which is handled by provided order
86+
- `parameterIndex`: The parameter index of the array
87+
- `cacheKey`: a custom cache key function for each element of the cache
88+
- `options`: Options passed to the strategy for this particular method
89+
90+
E.g.
91+
```ts
92+
import { MultiCache, ExpirationStrategy, MemoryStorage } from "@hokify/node-ts-cache";
93+
import RedisIOStorage from '@hokify/node-ts-cache-redisio-storage';
94+
95+
const localStrategy = new MemoryStorage();
96+
const centralStrategy = new RedisIOStorage({/*..*/});
97+
98+
class MyService {
99+
@MultiCache([localStrategy, centralStrategy], 0)
100+
public getUserNames(userIds: string[]): Promise<string>[] {
101+
return getUserNamesFromDatabase(userIds); // beware: return same order and number of userIds -> name
102+
// e.g. 1,2,3 .. shoudl return [userName1, userName2, userName3] (or null for entries that do not exist)
103+
// if you return undefined (instead of null) for one entry, it is queried the next time again.
104+
}
105+
}
106+
107+
const a = new MyService();
108+
/**
109+
* this call checks local cache if it has user id1, 2 or 3..
110+
* all cache misses are then checked by the central cache
111+
* if there are still some missing entries, they are retrieved with the original getUsers method.
112+
*/
113+
const result = await a.getUsers([1,2,3]);
114+
115+
```
116+
117+
56118
Cache decorator generates cache key according to class name, class method and args (with JSON.stringify).
57119
If you want another key creation logic you can bypass key creation strategy to the Cache decorator.
58120

0 commit comments

Comments
 (0)