@node-ts-cache/valkey-storage 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Simon Tretter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # @node-ts-cache/valkey-storage
2
+
3
+ Valkey storage adapter for [node-ts-cache](https://github.com/simllll/node-ts-cache).
4
+
5
+ [Valkey](https://valkey.io/) is an open-source, Redis-compatible in-memory data store that emerged as a community-driven fork after Redis changed its license.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @node-ts-cache/core @node-ts-cache/valkey-storage
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { Cache, ExpirationStrategy } from '@node-ts-cache/core';
17
+ import { ValkeyStorage } from '@node-ts-cache/valkey-storage';
18
+ import Valkey from 'iovalkey';
19
+
20
+ const valkeyClient = new Valkey({
21
+ host: 'localhost',
22
+ port: 6379
23
+ });
24
+
25
+ const valkeyCache = new ExpirationStrategy(new ValkeyStorage(() => valkeyClient, { maxAge: 3600 }));
26
+
27
+ class MyService {
28
+ @Cache(valkeyCache, { ttl: 60 })
29
+ async getUsers(): Promise<User[]> {
30
+ // expensive operation
31
+ }
32
+ }
33
+ ```
34
+
35
+ ## Configuration Options
36
+
37
+ ```typescript
38
+ new ValkeyStorage(
39
+ valkeyFactory: () => Valkey,
40
+ options?: {
41
+ maxAge: number; // TTL in seconds (default: 86400 = 1 day)
42
+ }
43
+ )
44
+ ```
45
+
46
+ ### Constructor Parameters
47
+
48
+ | Parameter | Type | Description |
49
+ | ---------------- | -------------- | -------------------------------------------------- |
50
+ | `valkeyFactory` | `() => Valkey` | Factory function returning a Valkey client |
51
+ | `options` | `object` | Configuration options |
52
+ | `options.maxAge` | `number` | Default TTL in seconds (default: 86400 = 24 hours) |
53
+
54
+ ### Error Handling
55
+
56
+ You can attach an error handler for non-blocking write operations:
57
+
58
+ ```typescript
59
+ const storage = new ValkeyStorage(() => valkeyClient, { maxAge: 3600 });
60
+
61
+ storage.onError(error => {
62
+ console.error('Valkey error:', error);
63
+ // Log to monitoring service, etc.
64
+ });
65
+ ```
66
+
67
+ ### Batch Operations
68
+
69
+ ValkeyStorage supports efficient batch operations:
70
+
71
+ ```typescript
72
+ // Get multiple items
73
+ const items = await storage.getItems<User>(['user:1', 'user:2', 'user:3']);
74
+
75
+ // Set multiple items
76
+ await storage.setItems([
77
+ { key: 'user:1', content: user1 },
78
+ { key: 'user:2', content: user2 }
79
+ ]);
80
+ ```
81
+
82
+ ## Running Tests Locally
83
+
84
+ Start Valkey using Docker:
85
+
86
+ ```bash
87
+ docker run -p 6379:6379 valkey/valkey:latest
88
+ ```
89
+
90
+ Then run the tests:
91
+
92
+ ```bash
93
+ npm test
94
+ ```
95
+
96
+ ## Why Valkey?
97
+
98
+ - **Open Source**: Valkey is BSD-3 licensed, ensuring it remains truly open source
99
+ - **Redis Compatible**: Drop-in replacement for Redis with full protocol compatibility
100
+ - **Community Driven**: Backed by Linux Foundation with contributions from AWS, Google, Oracle, and more
101
+ - **Active Development**: Regular releases with new features and improvements
102
+
103
+ ## License
104
+
105
+ MIT
@@ -0,0 +1 @@
1
+ export { ValkeyStorage } from './valkey.storage.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { ValkeyStorage } from './valkey.storage.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { IAsynchronousCacheType, IMultiIAsynchronousCacheType } from '@node-ts-cache/core';
2
+ import * as Valkey from 'iovalkey';
3
+ export declare class ValkeyStorage implements IAsynchronousCacheType, IMultiIAsynchronousCacheType {
4
+ private valkey;
5
+ private options;
6
+ constructor(valkey: () => Valkey.default, options?: {
7
+ maxAge: number;
8
+ });
9
+ private errorHandler;
10
+ onError(listener: (error: Error) => void): void;
11
+ getItems<T>(keys: string[]): Promise<{
12
+ [key: string]: T | undefined;
13
+ }>;
14
+ setItems<T = unknown>(values: {
15
+ key: string;
16
+ content: T | undefined;
17
+ }[], options?: {
18
+ ttl?: number;
19
+ }): Promise<void>;
20
+ getItem<T>(key: string): Promise<T | undefined>;
21
+ setItem<T = unknown>(key: string, content: T | undefined, options?: {
22
+ ttl?: number;
23
+ }): Promise<void>;
24
+ clear(): Promise<void>;
25
+ }
@@ -0,0 +1,102 @@
1
+ export class ValkeyStorage {
2
+ valkey;
3
+ options;
4
+ constructor(valkey, options = { maxAge: 86400 }) {
5
+ this.valkey = valkey;
6
+ this.options = options;
7
+ }
8
+ errorHandler;
9
+ onError(listener) {
10
+ this.errorHandler = listener;
11
+ }
12
+ async getItems(keys) {
13
+ const mget = await this.valkey().mget(...keys);
14
+ const res = {};
15
+ mget.forEach((entry, i) => {
16
+ if (entry === null) {
17
+ res[keys[i]] = undefined; // value does not exist yet
18
+ return;
19
+ }
20
+ if (entry === '') {
21
+ res[keys[i]] = null; // value does exist, but is empty
22
+ return;
23
+ }
24
+ // Try to parse as JSON
25
+ let parsedItem = entry;
26
+ try {
27
+ if (entry) {
28
+ parsedItem = JSON.parse(entry);
29
+ }
30
+ }
31
+ catch {
32
+ /** Not JSON, keep as string */
33
+ }
34
+ res[keys[i]] = parsedItem;
35
+ });
36
+ return res;
37
+ }
38
+ async setItems(values, options) {
39
+ const pipeline = this.valkey().pipeline();
40
+ values.forEach(val => {
41
+ if (val.content === undefined)
42
+ return;
43
+ const content = JSON.stringify(val.content);
44
+ const ttl = options?.ttl ?? this.options.maxAge;
45
+ if (ttl) {
46
+ pipeline.setex(val.key, ttl, content);
47
+ }
48
+ else {
49
+ pipeline.set(val.key, content);
50
+ }
51
+ });
52
+ const savePromise = pipeline.exec();
53
+ if (this.errorHandler) {
54
+ // if we have an error handler, we do not need to await the result
55
+ savePromise.catch((err) => this.errorHandler && this.errorHandler(err));
56
+ }
57
+ else {
58
+ await savePromise;
59
+ }
60
+ }
61
+ async getItem(key) {
62
+ const entry = await this.valkey().get(key);
63
+ if (entry === null) {
64
+ return undefined;
65
+ }
66
+ if (entry === '') {
67
+ return null; // value exists but is empty
68
+ }
69
+ // Try to parse as JSON
70
+ let parsedItem = entry;
71
+ try {
72
+ parsedItem = JSON.parse(entry);
73
+ }
74
+ catch {
75
+ /** Not JSON, keep as string */
76
+ }
77
+ return parsedItem;
78
+ }
79
+ async setItem(key, content, options) {
80
+ if (content === undefined) {
81
+ await this.valkey().del(key);
82
+ return;
83
+ }
84
+ // Serialize to string
85
+ const serialized = typeof content === 'object' ? JSON.stringify(content) : String(content);
86
+ const ttl = options?.ttl ?? this.options.maxAge;
87
+ const savePromise = ttl
88
+ ? this.valkey().setex(key, ttl, serialized)
89
+ : this.valkey().set(key, serialized);
90
+ if (this.errorHandler) {
91
+ // if we have an error handler, we do not need to await the result
92
+ savePromise.catch((err) => this.errorHandler && this.errorHandler(err));
93
+ }
94
+ else {
95
+ await savePromise;
96
+ }
97
+ }
98
+ async clear() {
99
+ await this.valkey().flushdb();
100
+ }
101
+ }
102
+ //# sourceMappingURL=valkey.storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valkey.storage.js","sourceRoot":"","sources":["../src/valkey.storage.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,aAAa;IAEhB;IACA;IAFT,YACS,MAA4B,EAC5B,UAEJ,EAAE,MAAM,EAAE,KAAK,EAAE;QAHb,WAAM,GAAN,MAAM,CAAsB;QAC5B,YAAO,GAAP,OAAO,CAEM;IACnB,CAAC;IAEI,YAAY,CAAuC;IAE3D,OAAO,CAAC,QAAgC;QACvC,IAAI,CAAC,YAAY,GAAG,QAAQ,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,QAAQ,CAAI,IAAc;QAC/B,MAAM,IAAI,GAAsB,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QAClE,MAAM,GAAG,GAAqC,EAAE,CAAC;QACjD,IAAI,CAAC,OAAO,CAAC,CAAC,KAAoB,EAAE,CAAS,EAAE,EAAE;YAChD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,2BAA2B;gBACrD,OAAO;YACR,CAAC;YAED,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;gBAClB,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAS,CAAC,CAAC,iCAAiC;gBAC3D,OAAO;YACR,CAAC;YAED,uBAAuB;YACvB,IAAI,UAAU,GAAe,KAAK,CAAC;YACnC,IAAI,CAAC;gBACJ,IAAI,KAAK,EAAE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;YAClE,WAAW,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,GAAY,CAAC,CAAC,CAAC;QAC3F,CAAC;aAAM,CAAC;YACP,MAAM,WAAW,CAAC;QACnB,CAAC;IACF,CAAC;IAEM,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC;IAC/B,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@node-ts-cache/valkey-storage",
3
+ "version": "1.0.0",
4
+ "description": "Valkey storage adapter for node-ts-cache",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "dependencies": {
16
+ "iovalkey": "^0.2.1",
17
+ "@node-ts-cache/core": "1.0.1"
18
+ },
19
+ "devDependencies": {
20
+ "ioredis-mock": "^8.9.0"
21
+ },
22
+ "engines": {
23
+ "node": ">=18.0.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/simllll/node-ts-cache.git",
31
+ "directory": "storages/valkey"
32
+ },
33
+ "keywords": [
34
+ "cache",
35
+ "typescript",
36
+ "valkey",
37
+ "redis-compatible"
38
+ ],
39
+ "license": "MIT",
40
+ "scripts": {
41
+ "build": "tsc -p .",
42
+ "clean": "git clean -fdx src",
43
+ "dev": "tsc -p . -w",
44
+ "test": "vitest run"
45
+ }
46
+ }