@skalfa/skalfa-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/bun.lock ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "lockfileVersion": 1,
3
+ "workspaces": {
4
+ "": {
5
+ "name": "@skalfa/skalfa-cache",
6
+ "dependencies": {
7
+ "@skalfa/skalfa-redis": "file:../skalfa-redis",
8
+ },
9
+ "devDependencies": {
10
+ "@types/node": "^26.0.0",
11
+ "typescript": "^6.0.3",
12
+ },
13
+ },
14
+ },
15
+ "packages": {
16
+ "@ioredis/commands": ["@ioredis/commands@1.10.0", "", {}, "sha512-UmeW7z4LfctwoQ5wkhVzgq8tXkreED2xZGpX+Bg+zA+WJFZCT6c062AfCK/Dfk81xZnnwdhJCUMkitihRaoC2Q=="],
17
+
18
+ "@skalfa/skalfa-redis": ["@skalfa/skalfa-redis@file:../skalfa-redis", { "dependencies": { "ioredis": "^5.4.1" }, "devDependencies": { "@types/node": "^26.0.0", "typescript": "^6.0.3" } }],
19
+
20
+ "@types/node": ["@types/node@26.0.0", "", { "dependencies": { "undici-types": "~8.3.0" } }, "sha512-vf2YFi1iY9lHGwNJMs01biZFbKJkrZR1T6/MlzjhJLPdntOHLhTrDSnSVcdtvjihi4VQNlrFRIxLsDBlQpAipA=="],
21
+
22
+ "cluster-key-slot": ["cluster-key-slot@1.1.1", "", {}, "sha512-rwHwUfXL40Chm1r08yrhU3qpUvdVlgkKNeyeGPOxnW8/SyVDvgRaed/Uz54AqWNaTCAThlj6QAs3TZcKI0xDEw=="],
23
+
24
+ "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="],
25
+
26
+ "denque": ["denque@2.1.0", "", {}, "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw=="],
27
+
28
+ "ioredis": ["ioredis@5.11.1", "", { "dependencies": { "@ioredis/commands": "1.10.0", "cluster-key-slot": "1.1.1", "debug": "4.4.3", "denque": "2.1.0", "redis-errors": "1.2.0", "redis-parser": "3.0.0", "standard-as-callback": "2.1.0" } }, "sha512-ehuGcf94bQXhfagULNXrJdfnWO38v070jxSx/qE87Kjzmu2fU7ro5EFAb+OPituLqgfyuQaym5DlrNydW2sJ9A=="],
29
+
30
+ "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
31
+
32
+ "redis-errors": ["redis-errors@1.2.0", "", {}, "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w=="],
33
+
34
+ "redis-parser": ["redis-parser@3.0.0", "", { "dependencies": { "redis-errors": "^1.0.0" } }, "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A=="],
35
+
36
+ "standard-as-callback": ["standard-as-callback@2.1.0", "", {}, "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A=="],
37
+
38
+ "typescript": ["typescript@6.0.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw=="],
39
+
40
+ "undici-types": ["undici-types@8.3.0", "", {}, "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ=="],
41
+ }
42
+ }
@@ -0,0 +1,6 @@
1
+ export declare const cache: {
2
+ makeKey(type: string, prefix: string, query: any): string;
3
+ get<T>(key: string): Promise<T | null>;
4
+ set(key: string, value: any, expired: number): Promise<void>;
5
+ clear(type: string, prefix: string): Promise<void>;
6
+ };
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import { redis } from "@skalfa/skalfa-redis";
2
+ export const cache = {
3
+ // =============================>
4
+ // ## Cache: Make key of cache database
5
+ // =============================>
6
+ makeKey(type, prefix, query) {
7
+ const keyParts = typeof query === "object" ? JSON.stringify(query) : String(query);
8
+ return `${type}:${prefix}:${Buffer.from(keyParts).toString("base64")}`;
9
+ },
10
+ // =============================>
11
+ // ## Cache: Get cache with key
12
+ // =============================>
13
+ async get(key) {
14
+ const cached = await redis.get(key);
15
+ if (!cached)
16
+ return null;
17
+ try {
18
+ return JSON.parse(cached);
19
+ }
20
+ catch {
21
+ return null;
22
+ }
23
+ },
24
+ // =============================>
25
+ // ## Cache: Set cache record
26
+ // =============================>
27
+ async set(key, value, expired) {
28
+ const ttl = expired ?? 60;
29
+ await redis.set(key, JSON.stringify(value), "EX", ttl);
30
+ },
31
+ // =============================>
32
+ // ## Cache: Set cache record
33
+ // =============================>
34
+ async clear(type, prefix) {
35
+ const keyPrefix = `${type}:${prefix}:*`;
36
+ const keys = await redis.keys(keyPrefix);
37
+ if (keys.length)
38
+ await redis.del(keys);
39
+ }
40
+ };
41
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,iCAAiC;IACjC,uCAAuC;IACvC,iCAAiC;IACjC,OAAO,CAAC,IAAY,EAAE,MAAc,EAA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AAA;IACxC,CAAC;CACF,CAAA"}
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@skalfa/skalfa-cache",
3
+ "version": "1.0.0",
4
+ "description": "Cache utility package for Skalfa API framework using Redis.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc --ignoreDeprecations 6.0"
9
+ },
10
+ "keywords": [
11
+ "aluna",
12
+ "cache",
13
+ "redis",
14
+ "skalfa"
15
+ ],
16
+ "author": "",
17
+ "license": "UNLICENSED",
18
+ "dependencies": {
19
+ "@skalfa/skalfa-redis": "file:../skalfa-redis"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^26.0.0",
23
+ "typescript": "^6.0.3"
24
+ }
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { redis } from "@skalfa/skalfa-redis";
2
+
3
+ export const cache = {
4
+ // =============================>
5
+ // ## Cache: Make key of cache database
6
+ // =============================>
7
+ makeKey(type: string, prefix: string, query: any): string {
8
+ const keyParts = typeof query === "object" ? JSON.stringify(query) : String(query);
9
+ return `${type}:${prefix}:${Buffer.from(keyParts).toString("base64")}`;
10
+ },
11
+
12
+
13
+
14
+ // =============================>
15
+ // ## Cache: Get cache with key
16
+ // =============================>
17
+ async get<T>(key: string): Promise<T | null> {
18
+ const cached = await redis.get(key);
19
+ if (!cached) return null;
20
+ try {
21
+ return JSON.parse(cached) as T;
22
+ } catch {
23
+ return null;
24
+ }
25
+ },
26
+
27
+
28
+
29
+ // =============================>
30
+ // ## Cache: Set cache record
31
+ // =============================>
32
+ async set(key: string, value: any, expired: number): Promise<void> {
33
+ const ttl = expired ?? 60;
34
+ await redis.set(key, JSON.stringify(value), "EX", ttl);
35
+ },
36
+
37
+
38
+
39
+ // =============================>
40
+ // ## Cache: Set cache record
41
+ // =============================>
42
+ async clear(type: string, prefix: string) {
43
+ const keyPrefix = `${type}:${prefix}:*`
44
+ const keys = await redis.keys(keyPrefix)
45
+ if (keys.length) await redis.del(keys)
46
+ }
47
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "ignoreDeprecations": "5.0",
4
+ "target": "ES2021",
5
+ "module": "ES2022",
6
+ "moduleResolution": "node",
7
+ "declaration": true,
8
+ "sourceMap": true,
9
+ "outDir": "./dist",
10
+ "rootDir": "./src",
11
+ "strict": true,
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "baseUrl": "."
16
+ },
17
+ "include": ["src/**/*"]
18
+ }