@@ -52,10 +52,26 @@ npm install @epic-web/cachified
52
52
53
53
``` ts
54
54
import { LRUCache } from ' lru-cache' ;
55
- import { cachified , CacheEntry } from ' @epic-web/cachified' ;
55
+ import { cachified , CacheEntry , Cache , totalTtl } from ' @epic-web/cachified' ;
56
56
57
57
/* lru cache is not part of this package but a simple non-persistent cache */
58
- const lru = new LRUCache <string , CacheEntry >({ max: 1000 });
58
+ const lruInstance = new LRUCache <string , CacheEntry >({ max: 1000 });
59
+
60
+ const lru: Cache = {
61
+ set(key , value ) {
62
+ const ttl = totalTtl (value ?.metadata );
63
+ return lruInstance .set (key , value , {
64
+ ttl: ttl === Infinity ? undefined : ttl ,
65
+ start: value ?.metadata ?.createdTime ,
66
+ });
67
+ },
68
+ get(key ) {
69
+ return lruInstance .get (key );
70
+ },
71
+ delete(key ) {
72
+ return lruInstance .delete (key );
73
+ },
74
+ };
59
75
60
76
function getUserById(userId : number ) {
61
77
return cachified ({
@@ -218,129 +234,11 @@ interface CachifiedOptions<Value> {
218
234
219
235
## Adapters
220
236
221
- There are some build-in adapters for common caches, using them makes sure
222
- the used caches cleanup outdated values themselves.
223
-
224
- ### Adapter for [ lru-cache] ( https://www.npmjs.com/package/lru-cache )
225
-
226
- <!-- lru-adapter -->
227
-
228
- ``` ts
229
- import { LRUCache } from ' lru-cache' ;
230
- import { cachified , lruCacheAdapter , CacheEntry } from ' @epic-web/cachified' ;
231
-
232
- const lru = new LRUCache <string , CacheEntry >({ max: 1000 });
233
- const cache = lruCacheAdapter (lru );
234
-
235
- await cachified ({
236
- cache ,
237
- key: ' user-1' ,
238
- getFreshValue() {
239
-
240
- },
241
- });
242
- ```
243
-
244
- ### Adapter for [ redis] ( https://www.npmjs.com/package/redis )
245
-
246
- <!-- redis-adapter -->
247
-
248
- ``` ts
249
- import { createClient } from ' redis' ;
250
- import { cachified , redisCacheAdapter } from ' @epic-web/cachified' ;
251
-
252
- const redis = createClient ({
253
- /* ...opts */
254
- });
255
- const cache = redisCacheAdapter (redis );
256
-
257
- await cachified ({
258
- cache ,
259
- key: ' user-1' ,
260
- getFreshValue() {
261
-
262
- },
263
- });
264
- ```
265
-
266
- ### Adapter for [ redis@3] ( https://www.npmjs.com/package/redis/v/3.1.2 )
267
-
268
- <!-- redis-3-adapter -->
269
-
270
- ``` ts
271
- import { createClient } from ' redis' ;
272
- import { cachified , redis3CacheAdapter } from ' @epic-web/cachified' ;
273
-
274
- const redis = createClient ({
275
- /* ...opts */
276
- });
277
- const cache = redis3CacheAdapter (redis );
278
-
279
- const data = await cachified ({
280
- cache ,
281
- key: ' user-1' ,
282
- getFreshValue() {
283
-
284
- },
285
- });
286
- ```
287
-
288
- ### Adapter for [ Cloudflare KV] ( https://developers.cloudflare.com/kv/ )
289
-
290
- For additional information or to report issues, please visit the [ cachified-adapter-cloudflare-kv repository] ( https://github.com/AdiRishi/cachified-adapter-cloudflare-kv ) .
291
-
292
- ``` ts
293
- import { cachified , Cache } from ' @epic-web/cachified' ;
294
- import { cloudflareKvCacheAdapter } from ' cachified-adapter-cloudflare-kv' ;
295
-
296
- export interface Env {
297
- KV: KVNamespace ;
298
- CACHIFIED_KV_CACHE: Cache ;
299
- }
300
-
301
- export async function getUserById(
302
- userId : number ,
303
- env : Env ,
304
- ): Promise <Record <string , unknown >> {
305
- return cachified ({
306
- key: ` user-${userId } ` ,
307
- cache: env .CACHIFIED_KV_CACHE ,
308
- async getFreshValue() {
309
- const response = await fetch (
310
- ` https://jsonplaceholder.typicode.com/users/${userId } ` ,
311
- );
312
- return response .json ();
313
- },
314
- ttl: 60_000 , // 1 minute
315
- staleWhileRevalidate: 300_000 , // 5 minutes
316
- });
317
- }
318
-
319
- export default {
320
- async fetch(
321
- request : Request ,
322
- env : Env ,
323
- ctx : ExecutionContext ,
324
- ): Promise <Response > {
325
- env .CACHIFIED_KV_CACHE = cloudflareKvCacheAdapter ({
326
- kv: env .KV ,
327
- keyPrefix: ' mycache' , // optional
328
- name: ' CloudflareKV' , // optional
329
- });
330
- const userId = Math .floor (Math .random () * 10 ) + 1 ;
331
- const user = await getUserById (userId , env );
332
- return new Response (JSON .stringify (user ), {
333
- headers: {
334
- ' Content-Type' : ' application/json' ,
335
- },
336
- });
337
- },
338
- };
339
- ```
340
-
341
- ### Adapter for [ redis-json] ( https://www.npmjs.com/package/@redis/json )
237
+ There are some adapters available for common caches. Using them makes sure the used caches cleanup outdated values themselves.
342
238
343
- [ cachified-redis-json-adapter] ( https://www.npmjs.com/package/cachified-redis-json-adapter )
239
+ - Adapter for [ redis] ( https://www.npmjs.com/package/redis ) : [ cachified-redis-adapter] ( https://www.npmjs.com/package/cachified-redis-adapter )
240
+ - Adapter for [ redis-json] ( https://www.npmjs.com/package/@redis/json ) : [ cachified-redis-json-adapter] ( https://www.npmjs.com/package/cachified-redis-json-adapter )
241
+ - Adapter for [ Cloudflare KV] ( https://developers.cloudflare.com/kv/ ) : [ cachified-adapter-cloudflare-kv repository] ( https://github.com/AdiRishi/cachified-adapter-cloudflare-kv )
344
242
345
243
## Advanced Usage
346
244
0 commit comments