@edirect/redis 11.0.49 → 11.0.50
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/dist/README.md +4 -114
- package/dist/package.json +5 -5
- package/package.json +3 -3
package/dist/README.md
CHANGED
|
@@ -1,123 +1,13 @@
|
|
|
1
1
|
# @edirect/redis
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- Global module — register once, inject `RedisService` anywhere
|
|
8
|
-
- Automatic JSON serialization/deserialization for stored values
|
|
9
|
-
- TTL support with configurable expiry types (`EX`, `PX`, `EXAT`, `PXAT`)
|
|
10
|
-
- Hash operations (`hget`, `hgetall`)
|
|
11
|
-
- Conditional set (`setnx`) for atomic operations
|
|
12
|
-
- Integrated error logging via `@edirect/logger`
|
|
13
|
-
- Health check via `asyncPing()`
|
|
3
|
+
The EDirectInsure Redis Module.
|
|
14
4
|
|
|
15
5
|
## Installation
|
|
16
6
|
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
# or
|
|
20
|
-
npm install @edirect/redis
|
|
7
|
+
```shell
|
|
8
|
+
$ npm i --save @edirect/redis
|
|
21
9
|
```
|
|
22
10
|
|
|
23
11
|
## Usage
|
|
24
12
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { Module } from '@nestjs/common';
|
|
29
|
-
import { ConfigModule } from '@edirect/config';
|
|
30
|
-
import { LoggerModule } from '@edirect/logger';
|
|
31
|
-
import { RedisModule } from '@edirect/redis';
|
|
32
|
-
|
|
33
|
-
@Module({
|
|
34
|
-
imports: [
|
|
35
|
-
ConfigModule,
|
|
36
|
-
LoggerModule.register({ output: 'console' }),
|
|
37
|
-
RedisModule,
|
|
38
|
-
],
|
|
39
|
-
})
|
|
40
|
-
export class AppModule {}
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Because `RedisModule` is decorated with `@Global()`, you only need to import it once at the root module.
|
|
44
|
-
|
|
45
|
-
### Inject and use RedisService
|
|
46
|
-
|
|
47
|
-
```ts
|
|
48
|
-
import { Injectable } from '@nestjs/common';
|
|
49
|
-
import { RedisService } from '@edirect/redis';
|
|
50
|
-
|
|
51
|
-
@Injectable()
|
|
52
|
-
export class CacheService {
|
|
53
|
-
constructor(private readonly redis: RedisService) {}
|
|
54
|
-
|
|
55
|
-
async cachePolicy(policyId: string, data: object): Promise<void> {
|
|
56
|
-
// Store with 5-minute TTL
|
|
57
|
-
await this.redis.set(`policy:${policyId}`, data, 300);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async getPolicy(policyId: string): Promise<object | null> {
|
|
61
|
-
return this.redis.get(`policy:${policyId}`) as Promise<object | null>;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async isAlive(): Promise<boolean> {
|
|
65
|
-
return this.redis.asyncPing();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## Environment Variables
|
|
71
|
-
|
|
72
|
-
| Variable | Description | Required |
|
|
73
|
-
| ----------- | ----------------------------------------------------------------------- | -------- |
|
|
74
|
-
| `REDIS_URL` | Redis connection URL (e.g., `redis://localhost:6379`) | Yes |
|
|
75
|
-
| `REDIS_TTL` | Default TTL in seconds when `ttl` param is omitted (use `0` for no TTL) | No |
|
|
76
|
-
|
|
77
|
-
## API
|
|
78
|
-
|
|
79
|
-
### `RedisService`
|
|
80
|
-
|
|
81
|
-
| Method | Signature | Description |
|
|
82
|
-
| ----------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
|
|
83
|
-
| `get` | `(key: string): Promise<string \| object \| null>` | Get and JSON-parse a value by key |
|
|
84
|
-
| `mget` | `(keys: string[]): Promise<string[] \| object[]>` | Get and JSON-parse multiple keys |
|
|
85
|
-
| `set` | `(key: string, data: object \| string, ttl?: number, ttlType?: 'EX' \| 'PX' \| 'EXAT' \| 'PXAT'): Promise<void>` | Set a value with optional TTL. Falls back to `REDIS_TTL` env. |
|
|
86
|
-
| `del` | `(key: string): Promise<void>` | Delete a key |
|
|
87
|
-
| `setnx` | `(key: string, data: object \| string): Promise<boolean>` | Set only if the key does not exist. Returns `true` if set. |
|
|
88
|
-
| `hget` | `(hash: string, field: string): Promise<string \| null>` | Get a single field from a hash |
|
|
89
|
-
| `hgetall` | `(hash: string): Promise<{ [key: string]: string }>` | Get all fields from a hash |
|
|
90
|
-
| `keys` | `(pattern: string): Promise<string \| string[]>` | Find keys matching a pattern |
|
|
91
|
-
| `asyncPing` | `(): Promise<boolean>` | Returns `true` if Redis responds to PING |
|
|
92
|
-
|
|
93
|
-
### TTL Types
|
|
94
|
-
|
|
95
|
-
| `ttlType` | Description |
|
|
96
|
-
| -------------- | ------------------------------ |
|
|
97
|
-
| `EX` (default) | Seconds from now |
|
|
98
|
-
| `PX` | Milliseconds from now |
|
|
99
|
-
| `EXAT` | Unix timestamp in seconds |
|
|
100
|
-
| `PXAT` | Unix timestamp in milliseconds |
|
|
101
|
-
|
|
102
|
-
## Examples
|
|
103
|
-
|
|
104
|
-
```ts
|
|
105
|
-
// Store a string with 1-hour TTL
|
|
106
|
-
await redis.set('session:abc', 'user-data', 3600);
|
|
107
|
-
|
|
108
|
-
// Store an object (auto-serialized to JSON)
|
|
109
|
-
await redis.set('user:123', { name: 'John', role: 'admin' }, 600);
|
|
110
|
-
|
|
111
|
-
// Read back (auto-deserialized from JSON)
|
|
112
|
-
const user = await redis.get('user:123'); // → { name: 'John', role: 'admin' }
|
|
113
|
-
|
|
114
|
-
// Atomic set (only if key doesn't exist)
|
|
115
|
-
const wasSet = await redis.setnx('lock:job-1', 'worker-1');
|
|
116
|
-
|
|
117
|
-
// Hash operations
|
|
118
|
-
await redis.hget('config:th-broker', 'maxPolicies');
|
|
119
|
-
const allConfig = await redis.hgetall('config:th-broker');
|
|
120
|
-
|
|
121
|
-
// Wildcard key lookup
|
|
122
|
-
const sessionKeys = await redis.keys('session:*');
|
|
123
|
-
```
|
|
13
|
+
Import RedisModule on AppModule (app.module.ts)
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edirect/redis",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.46",
|
|
4
4
|
"main": "./dist/src/index.js",
|
|
5
5
|
"types": "./dist/src/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@edirect/config": "^11.0.
|
|
20
|
-
"@edirect/logger": "^11.0.
|
|
21
|
-
"@nestjs/common": "^11.1.
|
|
22
|
-
"redis": "^5.
|
|
19
|
+
"@edirect/config": "^11.0.46",
|
|
20
|
+
"@edirect/logger": "^11.0.46",
|
|
21
|
+
"@nestjs/common": "^11.1.12",
|
|
22
|
+
"redis": "^5.10.0",
|
|
23
23
|
"tslib": "^2.8.1"
|
|
24
24
|
},
|
|
25
25
|
"type": "commonjs"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edirect/redis",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.50",
|
|
4
4
|
"packageScope": "@edirect",
|
|
5
5
|
"main": "./dist/src/index.js",
|
|
6
6
|
"types": "./dist/src/index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"@nestjs/common": "^11.1.16",
|
|
21
21
|
"redis": "^5.11.0",
|
|
22
22
|
"tslib": "^2.8.1",
|
|
23
|
-
"@edirect/config": "11.0.
|
|
24
|
-
"@edirect/logger": "11.0.
|
|
23
|
+
"@edirect/config": "11.0.50",
|
|
24
|
+
"@edirect/logger": "11.0.50"
|
|
25
25
|
},
|
|
26
26
|
"nx": {
|
|
27
27
|
"name": "@edirect/redis",
|