@node-ts-cache/redis-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) 2017 Himmet Avsar
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,99 @@
1
+ # @node-ts-cache/redis-storage
2
+
3
+ [![npm](https://img.shields.io/npm/v/@node-ts-cache/redis-storage.svg)](https://www.npmjs.org/package/@node-ts-cache/redis-storage)
4
+
5
+ Redis storage adapter for [@node-ts-cache/core](https://www.npmjs.com/package/@node-ts-cache/core) using the legacy `redis` package (v3.x).
6
+
7
+ > **Note:** For new projects, consider using [@node-ts-cache/ioredis-storage](https://www.npmjs.com/package/@node-ts-cache/ioredis-storage) which uses the modern `ioredis` client with additional features like compression and multi-operations.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @node-ts-cache/core @node-ts-cache/redis-storage
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Usage
18
+
19
+ ```typescript
20
+ import { Cache, ExpirationStrategy } from '@node-ts-cache/core';
21
+ import RedisStorage from '@node-ts-cache/redis-storage';
22
+
23
+ const storage = new RedisStorage({
24
+ host: 'localhost',
25
+ port: 6379
26
+ });
27
+
28
+ const strategy = new ExpirationStrategy(storage);
29
+
30
+ class UserService {
31
+ @Cache(strategy, { ttl: 300 })
32
+ async getUser(id: string): Promise<User> {
33
+ return await db.users.findById(id);
34
+ }
35
+ }
36
+ ```
37
+
38
+ ### With Authentication
39
+
40
+ ```typescript
41
+ const storage = new RedisStorage({
42
+ host: 'redis.example.com',
43
+ port: 6379,
44
+ password: 'your-password',
45
+ db: 0
46
+ });
47
+ ```
48
+
49
+ ### Direct API Usage
50
+
51
+ ```typescript
52
+ const storage = new RedisStorage({ host: 'localhost', port: 6379 });
53
+ const strategy = new ExpirationStrategy(storage);
54
+
55
+ // Store a value
56
+ await strategy.setItem('user:123', { name: 'John' }, { ttl: 60 });
57
+
58
+ // Retrieve a value
59
+ const user = await strategy.getItem<{ name: string }>('user:123');
60
+
61
+ // Clear all cached items
62
+ await strategy.clear();
63
+ ```
64
+
65
+ ## Constructor Options
66
+
67
+ The constructor accepts [RedisClientOptions](https://github.com/redis/node-redis/tree/v3.1.2#options-object-properties) from the `redis` package:
68
+
69
+ | Option | Type | Default | Description |
70
+ | ---------- | -------- | ------------- | ------------------------------- |
71
+ | `host` | `string` | `"127.0.0.1"` | Redis server hostname |
72
+ | `port` | `number` | `6379` | Redis server port |
73
+ | `password` | `string` | - | Redis authentication password |
74
+ | `db` | `number` | `0` | Redis database number |
75
+ | `url` | `string` | - | Redis URL (overrides host/port) |
76
+
77
+ ## Interface
78
+
79
+ ```typescript
80
+ interface IAsynchronousCacheType {
81
+ getItem<T>(key: string): Promise<T | undefined>;
82
+ setItem(key: string, content: any, options?: any): Promise<void>;
83
+ clear(): Promise<void>;
84
+ }
85
+ ```
86
+
87
+ ## Dependencies
88
+
89
+ - `redis` ^3.1.2 - Redis client for Node.js
90
+ - `bluebird` 3.7.2 - Promise library for async operations
91
+
92
+ ## Requirements
93
+
94
+ - Node.js >= 18.0.0
95
+ - Redis server
96
+
97
+ ## License
98
+
99
+ MIT
@@ -0,0 +1,2 @@
1
+ import { RedisStorage } from './redis.storage.js';
2
+ export default RedisStorage;
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { RedisStorage } from './redis.storage.js';
2
+ export default RedisStorage;
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,eAAe,YAAY,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { IAsynchronousCacheType } from '@node-ts-cache/core';
2
+ import * as Redis from 'redis';
3
+ import { ClientOpts } from 'redis';
4
+ export declare class RedisStorage implements IAsynchronousCacheType {
5
+ private redisOptions;
6
+ private client;
7
+ constructor(redisOptions: ClientOpts, redis?: typeof Redis);
8
+ getItem<T>(key: string): Promise<T | undefined>;
9
+ setItem<T = unknown>(key: string, content: T | undefined): Promise<void>;
10
+ clear(): Promise<void>;
11
+ }
@@ -0,0 +1,37 @@
1
+ import Bluebird from 'bluebird';
2
+ import * as Redis from 'redis';
3
+ Bluebird.promisifyAll(Redis.RedisClient.prototype);
4
+ Bluebird.promisifyAll(Redis.Multi.prototype);
5
+ export class RedisStorage {
6
+ constructor(redisOptions, redis = Redis) {
7
+ this.redisOptions = redisOptions;
8
+ this.client = redis.createClient(this.redisOptions);
9
+ }
10
+ async getItem(key) {
11
+ const entry = await this.client.getAsync(key);
12
+ if (entry === null) {
13
+ return undefined;
14
+ }
15
+ // Try to parse as JSON, fallback to raw string
16
+ let parsedItem = entry;
17
+ try {
18
+ parsedItem = JSON.parse(entry);
19
+ }
20
+ catch (error) {
21
+ /** Not JSON, keep as string */
22
+ }
23
+ return parsedItem;
24
+ }
25
+ async setItem(key, content) {
26
+ if (content === undefined) {
27
+ await this.client.delAsync(key);
28
+ return;
29
+ }
30
+ const stringContent = typeof content === 'object' ? JSON.stringify(content) : String(content);
31
+ await this.client.setAsync(key, stringContent);
32
+ }
33
+ async clear() {
34
+ await this.client.flushdbAsync();
35
+ }
36
+ }
37
+ //# sourceMappingURL=redis.storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.storage.js","sourceRoot":"","sources":["../src/redis.storage.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAI/B,QAAQ,CAAC,YAAY,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;AACnD,QAAQ,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAE7C,MAAM,OAAO,YAAY;IAGxB,YAAoB,YAAwB,EAAE,KAAK,GAAG,KAAK;QAAvC,iBAAY,GAAZ,YAAY,CAAY;QAC3C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAiB,CAAC;IACrE,CAAC;IAEM,KAAK,CAAC,OAAO,CAAI,GAAW;QAClC,MAAM,KAAK,GAAkB,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC7D,IAAI,KAAK,KAAK,IAAI,EAAE;YACnB,OAAO,SAAS,CAAC;SACjB;QACD,+CAA+C;QAC/C,IAAI,UAAU,GAAe,KAAK,CAAC;QACnC,IAAI;YACH,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAM,CAAC;SACpC;QAAC,OAAO,KAAK,EAAE;YACf,+BAA+B;SAC/B;QACD,OAAO,UAA2B,CAAC;IACpC,CAAC;IAEM,KAAK,CAAC,OAAO,CAAc,GAAW,EAAE,OAAsB;QACpE,IAAI,OAAO,KAAK,SAAS,EAAE;YAC1B,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAChC,OAAO;SACP;QACD,MAAM,aAAa,GAClB,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACzE,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;IAChD,CAAC;IAEM,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC;IAClC,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@node-ts-cache/redis-storage",
3
+ "version": "1.0.0",
4
+ "description": "Simple and extensible caching module supporting decorators",
5
+ "keywords": [
6
+ "node",
7
+ "nodejs",
8
+ "cache",
9
+ "typescript",
10
+ "ts",
11
+ "caching",
12
+ "memcache",
13
+ "memory-cache",
14
+ "redis-cache",
15
+ "redis",
16
+ "file-cache",
17
+ "node-cache",
18
+ "ts-cache"
19
+ ],
20
+ "homepage": "https://github.com/simllll/node-ts-cache/tree/master/storages/redis#readme",
21
+ "bugs": {
22
+ "url": "https://github.com/simllll/node-ts-cache/issues"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/simllll/node-ts-cache.git"
27
+ },
28
+ "license": "MIT",
29
+ "author": "Simon Tretter <s.tretter@gmail.com>",
30
+ "contributors": [
31
+ "Himmet Avsar <avsar.himmet1@gmail.com>"
32
+ ],
33
+ "type": "module",
34
+ "main": "dist/index.js",
35
+ "types": "dist/index.d.ts",
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "dependencies": {
40
+ "bluebird": "3.7.2",
41
+ "redis": "^3.1.2",
42
+ "@node-ts-cache/core": "1.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@types/bluebird": "^3.5.36",
46
+ "@types/proxyquire": "1.3.28",
47
+ "@types/redis": "^2.8.32",
48
+ "@types/sinon": "10.0.6",
49
+ "redis-mock": "^0.56.0",
50
+ "sinon": "12.0.1"
51
+ },
52
+ "engines": {
53
+ "node": ">=18.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "gitHead": "c938eba762060f940a34bc192bec03bc76ea4017",
59
+ "scripts": {
60
+ "build": "tsc -p .",
61
+ "clean": "git clean -fdx src",
62
+ "dev": "tsc -p . -w",
63
+ "tdd": "mocha --loader=ts-node/esm -w",
64
+ "tdd-debug-brk": "mocha --loader=ts-node/esm --inspect-brk",
65
+ "test": "mocha --loader=ts-node/esm test/**/*.test.ts"
66
+ }
67
+ }