|
1 | | -import { type CacheHandler, type KeysData } from "./lib/types"; |
2 | | -import { streamToRaw } from "./lib/stream"; |
| 1 | +import { type CacheHandler } from "@nimpl/cache"; |
3 | 2 |
|
4 | | -export const getKeys = async ( |
5 | | - cacheHandler: CacheHandler, |
6 | | - type: "main" | "persistent" | "ephemeral" = "main", |
7 | | -): Promise<KeysData> => { |
8 | | - const layers = { |
9 | | - main: cacheHandler, |
10 | | - persistent: cacheHandler.persistentLayer, |
11 | | - ephemeral: cacheHandler.ephemeralLayer, |
12 | | - }; |
13 | | - const handler = layers[type]; |
14 | | - return handler.keys(); |
15 | | -}; |
16 | | - |
17 | | -export const getKeyDetails = async ( |
18 | | - cacheHandler: CacheHandler, |
19 | | - type: "main" | "persistent" | "ephemeral", |
20 | | - key: string, |
21 | | -) => { |
22 | | - const layers = { |
23 | | - main: cacheHandler, |
24 | | - persistent: cacheHandler.persistentLayer, |
25 | | - ephemeral: cacheHandler.ephemeralLayer, |
26 | | - }; |
27 | | - const handler = layers[type]; |
28 | | - try { |
29 | | - const cacheEntry = await handler.getEntry(key); |
30 | | - |
31 | | - if (!cacheEntry) { |
32 | | - return { |
33 | | - key, |
34 | | - metadata: null, |
35 | | - value: null, |
36 | | - size: 0, |
37 | | - status: null, |
38 | | - }; |
39 | | - } |
40 | | - |
41 | | - const { entry, size, status } = cacheEntry; |
42 | | - const [cacheStream, responseStream] = entry.value.tee(); |
43 | | - entry.value = cacheStream; |
44 | | - const value = await streamToRaw(responseStream); |
45 | | - |
46 | | - return { |
47 | | - key, |
48 | | - metadata: { |
49 | | - tags: entry.tags, |
50 | | - timestamp: entry.timestamp, |
51 | | - stale: entry.stale, |
52 | | - revalidate: entry.revalidate, |
53 | | - expire: entry.expire, |
54 | | - }, |
55 | | - value, |
56 | | - size, |
57 | | - status, |
58 | | - }; |
59 | | - } catch (error) { |
60 | | - return { |
61 | | - key, |
62 | | - metadata: null, |
63 | | - value: null, |
64 | | - size: 0, |
65 | | - error: error instanceof Error ? error.message : "Unknown error", |
66 | | - status: null, |
67 | | - }; |
68 | | - } |
69 | | -}; |
70 | | - |
71 | | -export const getCacheData = (cacheHandler: CacheHandler, segments?: string[]) => { |
72 | | - if (!segments?.length || segments.length > 2) { |
73 | | - return null; |
74 | | - } |
75 | | - const type = segments[0] as "main" | "persistent" | "ephemeral"; |
76 | | - if (!["main", "persistent", "ephemeral"].includes(type)) { |
77 | | - return null; |
78 | | - } |
79 | | - if (segments.length === 1) { |
80 | | - return getKeys(cacheHandler, type); |
81 | | - } |
82 | | - |
83 | | - return getKeyDetails(cacheHandler, type, segments[1]); |
84 | | -}; |
85 | | - |
86 | | -export const updateTags = async (cacheHandler: CacheHandler, tags: string[], duration: number) => { |
87 | | - return cacheHandler.updateTags(tags, { expire: duration }); |
88 | | -}; |
89 | | - |
90 | | -export const updateKey = async (cacheHandler: CacheHandler, key: string, duration: number) => { |
91 | | - return cacheHandler.updateKey(key, { expire: duration }); |
92 | | -}; |
| 3 | +import { type LayerType } from "./lib/types"; |
| 4 | +import { getKeys, getKeyDetails } from "./lib/get-helpers"; |
| 5 | +import { updateKey, updateTags } from "./lib/put-helpers"; |
| 6 | +import { deleteKey } from "./lib/delete-helpers"; |
93 | 7 |
|
94 | 8 | export const createHelpers = (cacheHandler: CacheHandler) => { |
95 | 9 | return { |
96 | | - getKeys: (type: "main" | "persistent" | "ephemeral") => getKeys(cacheHandler, type), |
97 | | - getKeyDetails: (type: "main" | "persistent" | "ephemeral", key: string) => |
98 | | - getKeyDetails(cacheHandler, type, key), |
99 | | - getCacheData: (segments?: string[]) => getCacheData(cacheHandler, segments), |
100 | | - updateTags: (tags: string[], duration: number) => updateTags(cacheHandler, tags, duration), |
101 | | - updateKey: (key: string, duration: number) => updateKey(cacheHandler, key, duration), |
| 10 | + getKeys: (type: LayerType) => getKeys(cacheHandler, type), |
| 11 | + getKeyDetails: (type: LayerType, key: string) => getKeyDetails(cacheHandler, type, key), |
| 12 | + updateTags: (type: LayerType, tags: string[], duration: number) => |
| 13 | + updateTags(cacheHandler, type, tags, duration), |
| 14 | + updateKey: (type: LayerType, key: string, duration: number) => updateKey(cacheHandler, type, key, duration), |
| 15 | + deleteKey: (type: LayerType, key: string) => deleteKey(cacheHandler, type, key), |
102 | 16 | }; |
103 | 17 | }; |
0 commit comments