@node-ts-cache/redis-storage 1.0.0 → 1.0.1
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/README.md +36 -20
- package/dist/redis.storage.d.ts +5 -4
- package/dist/redis.storage.js +21 -12
- package/dist/redis.storage.js.map +1 -1
- package/package.json +10 -18
package/README.md
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.org/package/@node-ts-cache/redis-storage)
|
|
4
4
|
|
|
5
|
-
Redis storage adapter for [@node-ts-cache/core](https://www.npmjs.com/package/@node-ts-cache/core) using the
|
|
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.
|
|
5
|
+
Redis storage adapter for [@node-ts-cache/core](https://www.npmjs.com/package/@node-ts-cache/core) using the official `redis` package (v4.x).
|
|
8
6
|
|
|
9
7
|
## Installation
|
|
10
8
|
|
|
@@ -18,11 +16,13 @@ npm install @node-ts-cache/core @node-ts-cache/redis-storage
|
|
|
18
16
|
|
|
19
17
|
```typescript
|
|
20
18
|
import { Cache, ExpirationStrategy } from '@node-ts-cache/core';
|
|
21
|
-
import RedisStorage from '@node-ts-cache/redis-storage';
|
|
19
|
+
import { RedisStorage } from '@node-ts-cache/redis-storage';
|
|
22
20
|
|
|
23
21
|
const storage = new RedisStorage({
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
socket: {
|
|
23
|
+
host: 'localhost',
|
|
24
|
+
port: 6379
|
|
25
|
+
}
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
const strategy = new ExpirationStrategy(storage);
|
|
@@ -39,17 +39,32 @@ class UserService {
|
|
|
39
39
|
|
|
40
40
|
```typescript
|
|
41
41
|
const storage = new RedisStorage({
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
socket: {
|
|
43
|
+
host: 'redis.example.com',
|
|
44
|
+
port: 6379
|
|
45
|
+
},
|
|
44
46
|
password: 'your-password',
|
|
45
|
-
|
|
47
|
+
database: 0
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Using URL
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const storage = new RedisStorage({
|
|
55
|
+
url: 'redis://user:password@localhost:6379/0'
|
|
46
56
|
});
|
|
47
57
|
```
|
|
48
58
|
|
|
49
59
|
### Direct API Usage
|
|
50
60
|
|
|
51
61
|
```typescript
|
|
52
|
-
|
|
62
|
+
import { RedisStorage } from '@node-ts-cache/redis-storage';
|
|
63
|
+
import { ExpirationStrategy } from '@node-ts-cache/core';
|
|
64
|
+
|
|
65
|
+
const storage = new RedisStorage({
|
|
66
|
+
socket: { host: 'localhost', port: 6379 }
|
|
67
|
+
});
|
|
53
68
|
const strategy = new ExpirationStrategy(storage);
|
|
54
69
|
|
|
55
70
|
// Store a value
|
|
@@ -60,19 +75,21 @@ const user = await strategy.getItem<{ name: string }>('user:123');
|
|
|
60
75
|
|
|
61
76
|
// Clear all cached items
|
|
62
77
|
await strategy.clear();
|
|
78
|
+
|
|
79
|
+
// Disconnect when done
|
|
80
|
+
await storage.disconnect();
|
|
63
81
|
```
|
|
64
82
|
|
|
65
83
|
## Constructor Options
|
|
66
84
|
|
|
67
|
-
The constructor accepts [RedisClientOptions](https://github.com/redis/node-redis
|
|
85
|
+
The constructor accepts [RedisClientOptions](https://github.com/redis/node-redis#configuration) from the `redis` package v4.x:
|
|
68
86
|
|
|
69
|
-
| Option | Type |
|
|
70
|
-
| ---------- | -------- |
|
|
71
|
-
| `
|
|
72
|
-
| `
|
|
73
|
-
| `password` | `string` |
|
|
74
|
-
| `
|
|
75
|
-
| `url` | `string` | - | Redis URL (overrides host/port) |
|
|
87
|
+
| Option | Type | Description |
|
|
88
|
+
| ---------- | -------- | ---------------------------------------- |
|
|
89
|
+
| `socket` | `object` | Socket connection options (host, port) |
|
|
90
|
+
| `url` | `string` | Redis URL (alternative to socket config) |
|
|
91
|
+
| `password` | `string` | Redis authentication password |
|
|
92
|
+
| `database` | `number` | Redis database number (default: 0) |
|
|
76
93
|
|
|
77
94
|
## Interface
|
|
78
95
|
|
|
@@ -86,8 +103,7 @@ interface IAsynchronousCacheType {
|
|
|
86
103
|
|
|
87
104
|
## Dependencies
|
|
88
105
|
|
|
89
|
-
- `redis` ^
|
|
90
|
-
- `bluebird` 3.7.2 - Promise library for async operations
|
|
106
|
+
- `redis` ^4.7.0 - Official Redis client for Node.js
|
|
91
107
|
|
|
92
108
|
## Requirements
|
|
93
109
|
|
package/dist/redis.storage.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { IAsynchronousCacheType } from '@node-ts-cache/core';
|
|
2
|
-
import
|
|
3
|
-
import { ClientOpts } from 'redis';
|
|
2
|
+
import { RedisClientOptions } from 'redis';
|
|
4
3
|
export declare class RedisStorage implements IAsynchronousCacheType {
|
|
5
|
-
private redisOptions;
|
|
6
4
|
private client;
|
|
7
|
-
|
|
5
|
+
private connectionPromise;
|
|
6
|
+
constructor(redisOptions?: RedisClientOptions);
|
|
7
|
+
private ensureConnected;
|
|
8
8
|
getItem<T>(key: string): Promise<T | undefined>;
|
|
9
9
|
setItem<T = unknown>(key: string, content: T | undefined): Promise<void>;
|
|
10
10
|
clear(): Promise<void>;
|
|
11
|
+
disconnect(): Promise<void>;
|
|
11
12
|
}
|
package/dist/redis.storage.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as Redis from 'redis';
|
|
3
|
-
Bluebird.promisifyAll(Redis.RedisClient.prototype);
|
|
4
|
-
Bluebird.promisifyAll(Redis.Multi.prototype);
|
|
1
|
+
import { createClient } from 'redis';
|
|
5
2
|
export class RedisStorage {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
client;
|
|
4
|
+
connectionPromise;
|
|
5
|
+
constructor(redisOptions) {
|
|
6
|
+
this.client = createClient(redisOptions);
|
|
7
|
+
this.connectionPromise = this.client.connect();
|
|
8
|
+
}
|
|
9
|
+
async ensureConnected() {
|
|
10
|
+
await this.connectionPromise;
|
|
9
11
|
}
|
|
10
12
|
async getItem(key) {
|
|
11
|
-
|
|
13
|
+
await this.ensureConnected();
|
|
14
|
+
const entry = await this.client.get(key);
|
|
12
15
|
if (entry === null) {
|
|
13
16
|
return undefined;
|
|
14
17
|
}
|
|
@@ -17,21 +20,27 @@ export class RedisStorage {
|
|
|
17
20
|
try {
|
|
18
21
|
parsedItem = JSON.parse(entry);
|
|
19
22
|
}
|
|
20
|
-
catch
|
|
23
|
+
catch {
|
|
21
24
|
/** Not JSON, keep as string */
|
|
22
25
|
}
|
|
23
26
|
return parsedItem;
|
|
24
27
|
}
|
|
25
28
|
async setItem(key, content) {
|
|
29
|
+
await this.ensureConnected();
|
|
26
30
|
if (content === undefined) {
|
|
27
|
-
await this.client.
|
|
31
|
+
await this.client.del(key);
|
|
28
32
|
return;
|
|
29
33
|
}
|
|
30
34
|
const stringContent = typeof content === 'object' ? JSON.stringify(content) : String(content);
|
|
31
|
-
await this.client.
|
|
35
|
+
await this.client.set(key, stringContent);
|
|
32
36
|
}
|
|
33
37
|
async clear() {
|
|
34
|
-
await this.
|
|
38
|
+
await this.ensureConnected();
|
|
39
|
+
await this.client.flushDb();
|
|
40
|
+
}
|
|
41
|
+
async disconnect() {
|
|
42
|
+
await this.ensureConnected();
|
|
43
|
+
await this.client.quit();
|
|
35
44
|
}
|
|
36
45
|
}
|
|
37
46
|
//# sourceMappingURL=redis.storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis.storage.js","sourceRoot":"","sources":["../src/redis.storage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"redis.storage.js","sourceRoot":"","sources":["../src/redis.storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAsB,MAAM,OAAO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C;IAC3C,CAAC;IAEM,KAAK,CAAC,KAAK;QACjB,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAC7B,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;IAEM,KAAK,CAAC,UAAU;QACtB,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAC7B,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-ts-cache/redis-storage",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Simple and extensible caching module supporting decorators",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -31,23 +31,18 @@
|
|
|
31
31
|
"Himmet Avsar <avsar.himmet1@gmail.com>"
|
|
32
32
|
],
|
|
33
33
|
"type": "module",
|
|
34
|
-
"
|
|
35
|
-
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"import": "./dist/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
36
40
|
"files": [
|
|
37
41
|
"dist"
|
|
38
42
|
],
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"
|
|
41
|
-
"
|
|
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"
|
|
44
|
+
"redis": "^4.7.0",
|
|
45
|
+
"@node-ts-cache/core": "1.0.1"
|
|
51
46
|
},
|
|
52
47
|
"engines": {
|
|
53
48
|
"node": ">=18.0.0"
|
|
@@ -55,13 +50,10 @@
|
|
|
55
50
|
"publishConfig": {
|
|
56
51
|
"access": "public"
|
|
57
52
|
},
|
|
58
|
-
"gitHead": "c938eba762060f940a34bc192bec03bc76ea4017",
|
|
59
53
|
"scripts": {
|
|
60
54
|
"build": "tsc -p .",
|
|
61
55
|
"clean": "git clean -fdx src",
|
|
62
56
|
"dev": "tsc -p . -w",
|
|
63
|
-
"
|
|
64
|
-
"tdd-debug-brk": "mocha --loader=ts-node/esm --inspect-brk",
|
|
65
|
-
"test": "mocha --loader=ts-node/esm test/**/*.test.ts"
|
|
57
|
+
"test": "vitest run"
|
|
66
58
|
}
|
|
67
59
|
}
|