@warlock.js/cache 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +527 -0
- package/cjs/cache-manager.d.ts +90 -0
- package/cjs/cache-manager.d.ts.map +1 -0
- package/cjs/cache-manager.js +148 -0
- package/cjs/cache-manager.js.map +1 -0
- package/cjs/drivers/base-cache-driver.d.ts +81 -0
- package/cjs/drivers/base-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/base-cache-driver.js +123 -0
- package/cjs/drivers/base-cache-driver.js.map +1 -0
- package/cjs/drivers/file-cache-driver.d.ts +42 -0
- package/cjs/drivers/file-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/file-cache-driver.js +106 -0
- package/cjs/drivers/file-cache-driver.js.map +1 -0
- package/cjs/drivers/index.d.ts +8 -0
- package/cjs/drivers/index.d.ts.map +1 -0
- package/cjs/drivers/lru-memory-cache-driver.d.ts +84 -0
- package/cjs/drivers/lru-memory-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/lru-memory-cache-driver.js +151 -0
- package/cjs/drivers/lru-memory-cache-driver.js.map +1 -0
- package/cjs/drivers/memory-cache-driver.d.ts +53 -0
- package/cjs/drivers/memory-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/memory-cache-driver.js +107 -0
- package/cjs/drivers/memory-cache-driver.js.map +1 -0
- package/cjs/drivers/memory-extended-cache-driver.d.ts +14 -0
- package/cjs/drivers/memory-extended-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/memory-extended-cache-driver.js +25 -0
- package/cjs/drivers/memory-extended-cache-driver.js.map +1 -0
- package/cjs/drivers/null-cache-driver.d.ts +58 -0
- package/cjs/drivers/null-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/null-cache-driver.js +84 -0
- package/cjs/drivers/null-cache-driver.js.map +1 -0
- package/cjs/drivers/redis-cache-driver.d.ts +39 -0
- package/cjs/drivers/redis-cache-driver.d.ts.map +1 -0
- package/cjs/drivers/redis-cache-driver.js +113 -0
- package/cjs/drivers/redis-cache-driver.js.map +1 -0
- package/cjs/index.d.ts +5 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/types.d.ts +193 -0
- package/cjs/types.d.ts.map +1 -0
- package/cjs/utils.d.ts +20 -0
- package/cjs/utils.d.ts.map +1 -0
- package/cjs/utils.js +27 -0
- package/cjs/utils.js.map +1 -0
- package/esm/cache-manager.d.ts +90 -0
- package/esm/cache-manager.d.ts.map +1 -0
- package/esm/cache-manager.js +148 -0
- package/esm/cache-manager.js.map +1 -0
- package/esm/drivers/base-cache-driver.d.ts +81 -0
- package/esm/drivers/base-cache-driver.d.ts.map +1 -0
- package/esm/drivers/base-cache-driver.js +123 -0
- package/esm/drivers/base-cache-driver.js.map +1 -0
- package/esm/drivers/file-cache-driver.d.ts +42 -0
- package/esm/drivers/file-cache-driver.d.ts.map +1 -0
- package/esm/drivers/file-cache-driver.js +106 -0
- package/esm/drivers/file-cache-driver.js.map +1 -0
- package/esm/drivers/index.d.ts +8 -0
- package/esm/drivers/index.d.ts.map +1 -0
- package/esm/drivers/lru-memory-cache-driver.d.ts +84 -0
- package/esm/drivers/lru-memory-cache-driver.d.ts.map +1 -0
- package/esm/drivers/lru-memory-cache-driver.js +151 -0
- package/esm/drivers/lru-memory-cache-driver.js.map +1 -0
- package/esm/drivers/memory-cache-driver.d.ts +53 -0
- package/esm/drivers/memory-cache-driver.d.ts.map +1 -0
- package/esm/drivers/memory-cache-driver.js +107 -0
- package/esm/drivers/memory-cache-driver.js.map +1 -0
- package/esm/drivers/memory-extended-cache-driver.d.ts +14 -0
- package/esm/drivers/memory-extended-cache-driver.d.ts.map +1 -0
- package/esm/drivers/memory-extended-cache-driver.js +25 -0
- package/esm/drivers/memory-extended-cache-driver.js.map +1 -0
- package/esm/drivers/null-cache-driver.d.ts +58 -0
- package/esm/drivers/null-cache-driver.d.ts.map +1 -0
- package/esm/drivers/null-cache-driver.js +84 -0
- package/esm/drivers/null-cache-driver.js.map +1 -0
- package/esm/drivers/redis-cache-driver.d.ts +39 -0
- package/esm/drivers/redis-cache-driver.d.ts.map +1 -0
- package/esm/drivers/redis-cache-driver.js +113 -0
- package/esm/drivers/redis-cache-driver.js.map +1 -0
- package/esm/index.d.ts +5 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -0
- package/esm/types.d.ts +193 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/utils.d.ts +20 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +27 -0
- package/esm/utils.js.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
# Warlock Cache
|
|
2
|
+
|
|
3
|
+
Cache manager for nodejs applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Simple**: The cache manager is simple to use, you can cache any data you want with just a few lines of code.
|
|
8
|
+
- **Multiple Drivers**: The cache manager supports `+6` drivers, you can use the memory driver, redis driver, or create your own custom driver.
|
|
9
|
+
- **Custom Drivers**: You can create your own custom driver, the cache manager is designed to be extensible.
|
|
10
|
+
- **Unified Configurations**: The cache manager allows you to define the cache configurations in one place, and use them across the application.
|
|
11
|
+
- **Multiple Drivers Usage**: the default cache driver is used by the cache manager, however, you can use any other driver for certain tasks.
|
|
12
|
+
- **Global Prefix**: Every driver has a global prefix function, this function is called each time an operation is performed, and it returns a string, this string is used as a prefix for the key.
|
|
13
|
+
- **Namespace**: The cache manager allows you to clear the cache for a specific namespace, this is useful when working with multi-tenant applications.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
`yarn create @mongez/warlock.cache`
|
|
18
|
+
|
|
19
|
+
Or
|
|
20
|
+
|
|
21
|
+
`npx create @mongez/warlock.cache`
|
|
22
|
+
|
|
23
|
+
Or
|
|
24
|
+
|
|
25
|
+
`pnpm create @mongez/warlock.cache`
|
|
26
|
+
|
|
27
|
+
## Configurations
|
|
28
|
+
|
|
29
|
+
First step, we need to define the cache configurations, in your configuration directory create a file to define the cache configurations.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import {
|
|
33
|
+
cache,
|
|
34
|
+
type CacheConfigurations,
|
|
35
|
+
MemoryCacheDriver,
|
|
36
|
+
} from "@mongez/warlock.cache";
|
|
37
|
+
|
|
38
|
+
const configurations: CacheConfigurations = {
|
|
39
|
+
drivers: {
|
|
40
|
+
memory: MemoryCacheDriver,
|
|
41
|
+
},
|
|
42
|
+
default: "memory",
|
|
43
|
+
memory: {
|
|
44
|
+
// time of expiration in seconds
|
|
45
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
cache.setCacheConfigurations(configurations);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This will allow using memory cache driver, which will cache the data in the memory, and it will be removed when the application is restarted.
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
It is that just simple, you can use the cache manager to cache any data you want.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { cache } from "@mongez/warlock.cache";
|
|
60
|
+
|
|
61
|
+
async function main() {
|
|
62
|
+
// get the name from the cache
|
|
63
|
+
const name = await cache.get("name");
|
|
64
|
+
|
|
65
|
+
// if the name is not cached, we will get null, we can set a default value
|
|
66
|
+
const name = await cache.get("name", "default name");
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Setting Values
|
|
71
|
+
|
|
72
|
+
To set a value, we can use the `set` method.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { cache } from "@mongez/warlock.cache";
|
|
76
|
+
|
|
77
|
+
async function main() {
|
|
78
|
+
// set the name in the cache
|
|
79
|
+
await cache.set("name", "John Doe");
|
|
80
|
+
|
|
81
|
+
// set the name in the cache for 10 minutes
|
|
82
|
+
await cache.set("name", "John Doe", 60 * 10);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Storing objects and arrays
|
|
87
|
+
|
|
88
|
+
By default, the cache manager out of the box handles primitive values along with objects and arrays, they are serialized into string if the driver allows only primitive values then deserialized when getting the value, if the driver allows setting objects and arrays, then they will be stored as is like in the memory driver.
|
|
89
|
+
|
|
90
|
+
## Removing Values
|
|
91
|
+
|
|
92
|
+
To remove a value from the cache, we can use the `remove` method.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { cache } from "@mongez/warlock.cache";
|
|
96
|
+
|
|
97
|
+
async function main() {
|
|
98
|
+
// remove the name from the cache
|
|
99
|
+
await cache.remove("name");
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Clearing the Cache
|
|
104
|
+
|
|
105
|
+
To clear the cache, we can use the `flush` method.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { cache } from "@mongez/warlock.cache";
|
|
109
|
+
|
|
110
|
+
async function main() {
|
|
111
|
+
// clear the cache
|
|
112
|
+
await cache.flush();
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Clearing By Namespace
|
|
117
|
+
|
|
118
|
+
In some situations, we need to clear the cache for a specific namespace, for example, we have a cache for the user profile, and we need to clear it when the user updates his profile, we can use the `removeByNamespace` method.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { cache } from "@mongez/warlock.cache";
|
|
122
|
+
|
|
123
|
+
async function main() {
|
|
124
|
+
// set user profile
|
|
125
|
+
await cache.set("user.profile", { name: "John Doe" });
|
|
126
|
+
|
|
127
|
+
// set user totals
|
|
128
|
+
|
|
129
|
+
await cache.set("user.totals", { posts: 10, comments: 20 });
|
|
130
|
+
|
|
131
|
+
// clear the cache
|
|
132
|
+
await cache.removeByNamespace("user");
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
This will remove both `user.profile` and `user.totals` from the cache.
|
|
137
|
+
|
|
138
|
+
It could be useful when working with multi tenant applications, we can use the tenant ID as a namespace, and when we need to clear the cache for a specific tenant, we can use the `removeByNamespace` method.
|
|
139
|
+
|
|
140
|
+
## Global Prefix
|
|
141
|
+
|
|
142
|
+
Every driver's options have by default `globalPrefix` async function, this function is called each time an operation is performed, and it returns a string, this string is used as a prefix for the key, let's add it to our memory driver.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import {
|
|
146
|
+
cache,
|
|
147
|
+
type CacheConfigurations,
|
|
148
|
+
MemoryCacheDriver,
|
|
149
|
+
} from "@mongez/warlock.cache";
|
|
150
|
+
|
|
151
|
+
const configurations: CacheConfigurations = {
|
|
152
|
+
drivers: {
|
|
153
|
+
memory: MemoryCacheDriver,
|
|
154
|
+
},
|
|
155
|
+
default: "memory",
|
|
156
|
+
memory: {
|
|
157
|
+
// time of expiration in seconds
|
|
158
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
159
|
+
globalPrefix: async () => {
|
|
160
|
+
// get the current client ID
|
|
161
|
+
const clientId = await getCurrentClientId();
|
|
162
|
+
|
|
163
|
+
// return the prefix
|
|
164
|
+
return `client-${clientId}`;
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
cache.setCacheConfigurations(configurations);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { cache } from "@mongez/warlock.cache";
|
|
174
|
+
|
|
175
|
+
async function main() {
|
|
176
|
+
// set the name in the cache
|
|
177
|
+
await cache.set("name", "John Doe"); // will be stored as client-1:name
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Available Drivers
|
|
182
|
+
|
|
183
|
+
The cache manager is shipped with 4 drivers:
|
|
184
|
+
|
|
185
|
+
- [Null Cache Driver](#null-cache-driver): This is the default used driver, it doesn't cache anything, it is useful for mocking the cache implementation.
|
|
186
|
+
- [Memory Cache Driver](#memory-cache-driver): This driver caches the data in the memory, it means that the data will be removed when the application is restarted.
|
|
187
|
+
- [Memory Extended Cache Driver](#memory-extended-cache-driver): Memory Cache Driver with extended **TTL**.
|
|
188
|
+
- [LRU Cache Driver](#lru-cache-driver): This driver caches the data in the memory with LRU algorithm.
|
|
189
|
+
- [Redis Cache Driver](#redis-cache-driver): This driver caches the data in the redis server.
|
|
190
|
+
- [File Cache Driver](#file-cache-driver): This driver caches the data in the file system.
|
|
191
|
+
|
|
192
|
+
### Redis Cache Driver
|
|
193
|
+
|
|
194
|
+
[Redis](https://redis.io/) is a popular cache driver, it is fast and reliable, to use it, we just need to define the driver and its configurations in the settings, the package is installed by default, so we don't need to install it.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
import {
|
|
198
|
+
cache,
|
|
199
|
+
type CacheConfigurations,
|
|
200
|
+
MemoryCacheDriver,
|
|
201
|
+
RedisCacheDriver,
|
|
202
|
+
} from "@mongez/warlock.cache";
|
|
203
|
+
|
|
204
|
+
const configurations: CacheConfigurations = {
|
|
205
|
+
drivers: {
|
|
206
|
+
memory: MemoryCacheDriver,
|
|
207
|
+
redis: RedisCacheDriver,
|
|
208
|
+
},
|
|
209
|
+
default: "redis",
|
|
210
|
+
options: {
|
|
211
|
+
redis: {
|
|
212
|
+
// time of expiration in seconds
|
|
213
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
214
|
+
// the redis connection options
|
|
215
|
+
host: "127.0.0.1",
|
|
216
|
+
port: 6379,
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
cache.setCacheConfigurations(configurations);
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
You can also pass `url` option directly if you're using a remote redis server.
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
const configurations: CacheConfigurations = {
|
|
228
|
+
drivers: {
|
|
229
|
+
memory: MemoryCacheDriver,
|
|
230
|
+
redis: RedisCacheDriver,
|
|
231
|
+
},
|
|
232
|
+
default: "redis",
|
|
233
|
+
options: {
|
|
234
|
+
redis: {
|
|
235
|
+
// time of expiration in seconds
|
|
236
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
237
|
+
// the redis connection options
|
|
238
|
+
url: "redis://host:port",
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
cache.setCacheConfigurations(configurations);
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Any other options that are not defined in the driver options will be passed to the redis client, so you can pass any other options that are supported by the redis client.
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
import { cache, RedisCacheDriver } from "@mongez/warlock.cache";
|
|
250
|
+
|
|
251
|
+
cache.setCacheConfigurations({
|
|
252
|
+
drivers: {
|
|
253
|
+
redis: RedisCacheDriver,
|
|
254
|
+
},
|
|
255
|
+
default: "redis",
|
|
256
|
+
options: {
|
|
257
|
+
redis: {
|
|
258
|
+
// time of expiration in seconds
|
|
259
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
260
|
+
// the redis connection options
|
|
261
|
+
...redisConnectionOptions,
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
> Redis is a dependency to this package so you don't need to install it manually.
|
|
268
|
+
|
|
269
|
+
### Memory Cache Driver
|
|
270
|
+
|
|
271
|
+
This driver caches the data in the memory, it means that the data will be removed when the application is restarted.
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
import { cache, MemoryCacheDriver } from "@mongez/warlock.cache";
|
|
275
|
+
|
|
276
|
+
cache.setCacheConfigurations({
|
|
277
|
+
drivers: {
|
|
278
|
+
memory: MemoryCacheDriver,
|
|
279
|
+
},
|
|
280
|
+
default: "memory",
|
|
281
|
+
});
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Memory Extended Cache Driver
|
|
285
|
+
|
|
286
|
+
Works exactly like the Memory Cache Driver, the main difference is that whenever the cache is hit, the TTL is extended by the same amount of time.
|
|
287
|
+
|
|
288
|
+
For example, if a cache value is set to expire in 10 minutes, and it is hit after 5 minutes, the TTL will be reset again to be 10 minutes.
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
import { cache, MemoryExtendedCacheDriver } from "@mongez/warlock.cache";
|
|
292
|
+
|
|
293
|
+
cache.setCacheConfigurations({
|
|
294
|
+
drivers: {
|
|
295
|
+
memoryExtended: MemoryExtendedCacheDriver,
|
|
296
|
+
},
|
|
297
|
+
default: "memoryExtended",
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
You can also define it as a replacement to `MemoryCacheDriver` as it already extends it:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { cache, MemoryExtendedCacheDriver } from "@mongez/warlock.cache";
|
|
305
|
+
|
|
306
|
+
cache.setCacheConfigurations({
|
|
307
|
+
drivers: {
|
|
308
|
+
memory: MemoryExtendedCacheDriver,
|
|
309
|
+
},
|
|
310
|
+
default: "memory",
|
|
311
|
+
});
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
Example of usage:
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
import { cache } from "@mongez/warlock.cache";
|
|
318
|
+
|
|
319
|
+
async function main() {
|
|
320
|
+
// set the name in the cache
|
|
321
|
+
await cache.set("name", "John Doe", 60 * 10); // will be stored for 10 minutes
|
|
322
|
+
|
|
323
|
+
// later after 3 minutes...
|
|
324
|
+
|
|
325
|
+
// get the name from the cache
|
|
326
|
+
const name = await cache.get("name"); // will be stored for 10 minutes from now
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
### LRU Cache Driver
|
|
332
|
+
|
|
333
|
+
[LRU Cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)) is a cache driver that caches the data in the memory with LRU algorithm.
|
|
334
|
+
|
|
335
|
+
So how this works? The LRU cache driver will store the data in the memory, and when the memory is full, it will remove the least recently used item.
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
import { cache, LRUCacheDriver } from "@mongez/warlock.cache";
|
|
339
|
+
|
|
340
|
+
cache.setCacheConfigurations({
|
|
341
|
+
drivers: {
|
|
342
|
+
lru: LRUCacheDriver,
|
|
343
|
+
},
|
|
344
|
+
default: "lru",
|
|
345
|
+
options: {
|
|
346
|
+
lru: {
|
|
347
|
+
// the maximum number of items to be stored in the cache, default to 1000
|
|
348
|
+
max: 1000,
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
> At the time being, there is no TTL for this driver
|
|
355
|
+
|
|
356
|
+
### File Cache Driver
|
|
357
|
+
|
|
358
|
+
This driver caches the data in the file system.
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
import { cache, FileCacheDriver } from "@mongez/warlock.cache";
|
|
362
|
+
|
|
363
|
+
cache.setCacheConfigurations({
|
|
364
|
+
drivers: {
|
|
365
|
+
file: FileCacheDriver,
|
|
366
|
+
},
|
|
367
|
+
default: "file",
|
|
368
|
+
options: {
|
|
369
|
+
file: {
|
|
370
|
+
// time of expiration in seconds
|
|
371
|
+
ttl: 60 * 60 * 24 * 7, // 7 days
|
|
372
|
+
// the directory to store the cache files
|
|
373
|
+
directory: "path/to/cache/directory",
|
|
374
|
+
},
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Null Cache Driver
|
|
380
|
+
|
|
381
|
+
This driver doesn't cache anything, it is useful for mocking the cache implementation.
|
|
382
|
+
|
|
383
|
+
```ts
|
|
384
|
+
import { cache, NullCacheDriver } from "@mongez/warlock.cache";
|
|
385
|
+
|
|
386
|
+
cache.setCacheConfigurations({
|
|
387
|
+
drivers: {
|
|
388
|
+
null: NullCacheDriver,
|
|
389
|
+
},
|
|
390
|
+
default: "null",
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## Custom Drivers
|
|
395
|
+
|
|
396
|
+
To create a custom driver, we need to create a class that implements the `CacheDriver` interface
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
export interface CacheDriver<ClientType, Options> {
|
|
400
|
+
/**
|
|
401
|
+
* The cache driver options
|
|
402
|
+
*/
|
|
403
|
+
options: Options;
|
|
404
|
+
/**
|
|
405
|
+
* Cache driver name
|
|
406
|
+
*/
|
|
407
|
+
name: string;
|
|
408
|
+
/**
|
|
409
|
+
* Remove all cached items by namespace
|
|
410
|
+
*/
|
|
411
|
+
removeNamespace(namespace: string): Promise<any>;
|
|
412
|
+
/**
|
|
413
|
+
* Set the cache driver options
|
|
414
|
+
*/
|
|
415
|
+
setOptions(options: Options): any;
|
|
416
|
+
/**
|
|
417
|
+
* Parse the key to be used in the cache
|
|
418
|
+
*/
|
|
419
|
+
parseKey(key: string | GenericObject): Promise<string>;
|
|
420
|
+
/**
|
|
421
|
+
* Set a value in the cache
|
|
422
|
+
*
|
|
423
|
+
* @param key The cache key, could be an object or string
|
|
424
|
+
* @param value The value to be stored in the cache
|
|
425
|
+
* @param ttl The time to live in seconds
|
|
426
|
+
*/
|
|
427
|
+
set(key: string | GenericObject, value: any, ttl?: number): Promise<any>;
|
|
428
|
+
/**
|
|
429
|
+
* Get a value from the cache
|
|
430
|
+
*/
|
|
431
|
+
get(key: string | GenericObject): Promise<any | null>;
|
|
432
|
+
/**
|
|
433
|
+
* Remove a value from the cache
|
|
434
|
+
*/
|
|
435
|
+
remove(key: string | GenericObject): Promise<void>;
|
|
436
|
+
/**
|
|
437
|
+
* Flush the entire cache
|
|
438
|
+
*/
|
|
439
|
+
flush(): Promise<void>;
|
|
440
|
+
/**
|
|
441
|
+
* Connect to the cache driver
|
|
442
|
+
*/
|
|
443
|
+
connect(): Promise<any>;
|
|
444
|
+
/**
|
|
445
|
+
* The cache client
|
|
446
|
+
*/
|
|
447
|
+
client?: ClientType;
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
## Base Cache Driver
|
|
452
|
+
|
|
453
|
+
This is an abstract class that implements the `CacheDriver` interface, it handles multiple things for you, like **logging**, **parsing TTL** and **parsing key** so we can easily create a new driver by extending this class.
|
|
454
|
+
|
|
455
|
+
```ts
|
|
456
|
+
import { BaseCacheDriver, type CacheDriver } from "@mongez/warlock.cache";
|
|
457
|
+
|
|
458
|
+
export class MyCacheDriver
|
|
459
|
+
extends BaseCacheDriver<>
|
|
460
|
+
implements CacheDriver<any, any>
|
|
461
|
+
{
|
|
462
|
+
/**
|
|
463
|
+
* The cache driver name
|
|
464
|
+
*/
|
|
465
|
+
public name = "database";
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Database connection
|
|
469
|
+
*/
|
|
470
|
+
public connection: any;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Connect to the cache driver
|
|
474
|
+
*/
|
|
475
|
+
public async connect() {
|
|
476
|
+
// connect to database
|
|
477
|
+
// options are handled by the base class
|
|
478
|
+
this.connection = await connectToDatabase(this.options);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Set a value in the cache
|
|
483
|
+
*
|
|
484
|
+
* @param key The cache key, could be an object or string
|
|
485
|
+
* @param value The value to be stored in the cache
|
|
486
|
+
* @param ttl The time to live in seconds
|
|
487
|
+
*/
|
|
488
|
+
public async set(key: string | GenericObject, value: any, ttl?: number) {
|
|
489
|
+
// set the value in the database
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Get a value from the database
|
|
494
|
+
*/
|
|
495
|
+
public async get(key: string | GenericObject) {
|
|
496
|
+
// get the value from the database
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Remove a value from the database
|
|
501
|
+
*/
|
|
502
|
+
public async remove(key: string | GenericObject) {
|
|
503
|
+
// remove the value from the database
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Flush the entire cache
|
|
508
|
+
*/
|
|
509
|
+
public async flush() {
|
|
510
|
+
// flush the database
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
## Using certain drivers
|
|
516
|
+
|
|
517
|
+
The `cache` manager will always use the default cache you set in the cache options, however, you could use any another driver for certain tasks by calling `use` method, this method receives the driver name and return the driver instance, if the driver is not loaded yet it will be loaded (and connected) before returning the driver instance.
|
|
518
|
+
|
|
519
|
+
```ts
|
|
520
|
+
import { cache } from "@mongez/warlock.cache";
|
|
521
|
+
|
|
522
|
+
async function main() {
|
|
523
|
+
// set the name in the cache
|
|
524
|
+
const redis = cache.use("redis");
|
|
525
|
+
const memory = cache.use("memory");
|
|
526
|
+
}
|
|
527
|
+
```
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { GenericObject } from "@mongez/reinforcements";
|
|
2
|
+
import { CacheConfigurations, CacheDriver, DriverClass } from "./types";
|
|
3
|
+
export declare class CacheManager implements CacheDriver<any, any> {
|
|
4
|
+
/**
|
|
5
|
+
* Cache Driver
|
|
6
|
+
*/
|
|
7
|
+
currentDriver?: CacheDriver<any, any>;
|
|
8
|
+
/**
|
|
9
|
+
* Loaded drivers
|
|
10
|
+
*/
|
|
11
|
+
loadedDrivers: Record<string, CacheDriver<any, any>>;
|
|
12
|
+
/**
|
|
13
|
+
* Configurations list
|
|
14
|
+
*/
|
|
15
|
+
protected configurations: CacheConfigurations;
|
|
16
|
+
/**
|
|
17
|
+
* {@inheritdoc}
|
|
18
|
+
*/
|
|
19
|
+
name: string;
|
|
20
|
+
/**
|
|
21
|
+
* {@inheritdoc}
|
|
22
|
+
*/
|
|
23
|
+
get client(): any;
|
|
24
|
+
/**
|
|
25
|
+
* Set the cache configurations
|
|
26
|
+
*/
|
|
27
|
+
setCacheConfigurations(configurations: CacheConfigurations): void;
|
|
28
|
+
/**
|
|
29
|
+
* Use the given driver
|
|
30
|
+
*/
|
|
31
|
+
use(driver: string | CacheDriver<any, any>): Promise<this>;
|
|
32
|
+
/**
|
|
33
|
+
* {@inheritdoc}
|
|
34
|
+
*/
|
|
35
|
+
get(key: string): Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* {@inheritdoc}
|
|
38
|
+
*/
|
|
39
|
+
set(key: string, value: any, ttl?: number): Promise<any>;
|
|
40
|
+
/**
|
|
41
|
+
* {@inheritdoc}
|
|
42
|
+
*/
|
|
43
|
+
remove(key: string): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* {@inheritdoc}
|
|
46
|
+
*/
|
|
47
|
+
removeNamespace(namespace: string): Promise<any>;
|
|
48
|
+
/**
|
|
49
|
+
* {@inheritdoc}
|
|
50
|
+
*/
|
|
51
|
+
flush(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* {@inheritdoc}
|
|
54
|
+
*/
|
|
55
|
+
connect(): Promise<any>;
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritdoc}
|
|
58
|
+
*/
|
|
59
|
+
parseKey(key: string | GenericObject): "" | Promise<string>;
|
|
60
|
+
/**
|
|
61
|
+
* {@inheritdoc}
|
|
62
|
+
*/
|
|
63
|
+
get options(): any;
|
|
64
|
+
/**
|
|
65
|
+
* {@inheritdoc}
|
|
66
|
+
*/
|
|
67
|
+
setOptions(options: GenericObject): any;
|
|
68
|
+
/**
|
|
69
|
+
* Get an instance of the cache driver
|
|
70
|
+
*/
|
|
71
|
+
driver(driverName: string): Promise<CacheDriver<any, any>>;
|
|
72
|
+
/**
|
|
73
|
+
* Initialize the cache manager and pick the default driver
|
|
74
|
+
*/
|
|
75
|
+
init(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Load the given cache driver name
|
|
78
|
+
*/
|
|
79
|
+
load(driver: string): Promise<CacheDriver<any, any>>;
|
|
80
|
+
/**
|
|
81
|
+
* Register and bind a driver
|
|
82
|
+
*/
|
|
83
|
+
registerDriver(driverName: string, driverClass: DriverClass): void;
|
|
84
|
+
/**
|
|
85
|
+
* Disconnect the cache manager
|
|
86
|
+
*/
|
|
87
|
+
disconnect(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
export declare const cache: CacheManager;
|
|
90
|
+
//# sourceMappingURL=cache-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-manager.d.ts","sourceRoot":"","sources":["../src/cache-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAExE,qBAAa,YAAa,YAAW,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC;IACxD;;OAEG;IACI,aAAa,CAAC,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAE7C;;OAEG;IACI,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAM;IAEjE;;OAEG;IACH,SAAS,CAAC,cAAc,EAAE,mBAAmB,CAEpC;IAET;;OAEG;IACI,IAAI,SAAkB;IAE7B;;OAEG;IACH,IAAW,MAAM,QAEhB;IAED;;OAEG;IACI,sBAAsB,CAAC,cAAc,EAAE,mBAAmB;IAMjE;;OAEG;IACU,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC;IAiBvD;;OAEG;IACU,GAAG,CAAC,GAAG,EAAE,MAAM;IAI5B;;OAEG;IACU,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM;IAItD;;OAEG;IACU,MAAM,CAAC,GAAG,EAAE,MAAM;IAI/B;;OAEG;IACU,eAAe,CAAC,SAAS,EAAE,MAAM;IAI9C;;OAEG;IACU,KAAK;IAIlB;;OAEG;IACU,OAAO;IAIpB;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa;IAI3C;;OAEG;IACH,IAAW,OAAO,QAEjB;IAED;;OAEG;IACI,UAAU,CAAC,OAAO,EAAE,aAAa;IAIxC;;OAEG;IACU,MAAM,CAAC,UAAU,EAAE,MAAM;IAItC;;OAEG;IACU,IAAI;IAYjB;;OAEG;IACU,IAAI,CAAC,MAAM,EAAE,MAAM;IAsBhC;;OAEG;IACI,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW;IAIlE;;OAEG;IACU,UAAU;CAGxB;AAED,eAAO,MAAM,KAAK,cAAqB,CAAC"}
|