@@ -9,7 +9,7 @@ Simple and extensible caching module supporting decorators
9
9
10
10
<!-- TOC depthTo:2 -->
11
11
12
- - [ node-ts-cache] ( #node-ts-cache )
12
+ - [ node-ts-cache] ( #with-decorator )
13
13
- [ Install] ( #install )
14
14
- [ Usage] ( #usage )
15
15
- [ With decorator] ( #with-decorator )
@@ -35,11 +35,15 @@ Caches function response using the given options. Works with different strategie
35
35
36
36
` @Cache(strategy, options) `
37
37
38
- - ` strategy ` : A supported caching [ Strategy] ( #strategies )
38
+ Standard method to cache an async method.
39
+
40
+ - ` strategy ` : A supported caching [ Strategy] ( #strategies ) (Async)
39
41
- ` options ` : Options passed to the strategy for this particular method
40
42
41
43
_ Note: @Cache always converts the method response to a promise because caching might be async._
42
44
45
+ E.g.
46
+
43
47
``` ts
44
48
import { Cache , ExpirationStrategy , MemoryStorage } from " @hokify/node-ts-cache" ;
45
49
@@ -53,6 +57,64 @@ class MyService {
53
57
}
54
58
```
55
59
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
+
56
118
Cache decorator generates cache key according to class name, class method and args (with JSON.stringify).
57
119
If you want another key creation logic you can bypass key creation strategy to the Cache decorator.
58
120
0 commit comments