@quanticjs/redis 2.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/dist/CacheBehavior.d.ts +11 -0
- package/dist/CacheBehavior.js +68 -0
- package/dist/DistributedLockBehavior.d.ts +14 -0
- package/dist/DistributedLockBehavior.js +89 -0
- package/dist/InvalidateCacheBehavior.d.ts +11 -0
- package/dist/InvalidateCacheBehavior.js +62 -0
- package/dist/QuanticRedisModule.d.ts +5 -0
- package/dist/QuanticRedisModule.js +43 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +13 -0
- package/dist/redis.module.d.ts +8 -0
- package/dist/redis.module.js +49 -0
- package/package.json +29 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Result, PipelineBehavior, PipelineScope } from '@quanticjs/core';
|
|
2
|
+
import type { Redis } from 'ioredis';
|
|
3
|
+
export declare class CacheBehavior implements PipelineBehavior {
|
|
4
|
+
private readonly redis?;
|
|
5
|
+
readonly order: 60;
|
|
6
|
+
readonly scope: PipelineScope;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(redis?: Redis | undefined);
|
|
9
|
+
execute<T>(command: object, next: () => Promise<Result<T>>): Promise<Result<T>>;
|
|
10
|
+
private interpolateKey;
|
|
11
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var CacheBehavior_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.CacheBehavior = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const core_1 = require("@quanticjs/core");
|
|
19
|
+
let CacheBehavior = CacheBehavior_1 = class CacheBehavior {
|
|
20
|
+
redis;
|
|
21
|
+
order = core_1.BehaviorOrder.CACHE;
|
|
22
|
+
scope = 'both';
|
|
23
|
+
logger = new common_1.Logger(CacheBehavior_1.name);
|
|
24
|
+
constructor(redis) {
|
|
25
|
+
this.redis = redis;
|
|
26
|
+
}
|
|
27
|
+
async execute(command, next) {
|
|
28
|
+
const metadata = (0, core_1.getCacheMetadata)(command.constructor);
|
|
29
|
+
if (!metadata || !this.redis) {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
const cacheKey = this.interpolateKey(metadata.key, command);
|
|
33
|
+
try {
|
|
34
|
+
const cached = await this.redis.get(cacheKey);
|
|
35
|
+
if (cached) {
|
|
36
|
+
this.logger.debug(`Cache hit: ${cacheKey}`);
|
|
37
|
+
return core_1.Result.success(JSON.parse(cached));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
this.logger.warn(`Cache read failed for key: ${cacheKey}`);
|
|
42
|
+
}
|
|
43
|
+
const result = await next();
|
|
44
|
+
if (result.isSuccess && result.value !== undefined) {
|
|
45
|
+
try {
|
|
46
|
+
await this.redis.set(cacheKey, JSON.stringify(result.value), 'EX', metadata.ttlSeconds);
|
|
47
|
+
this.logger.debug(`Cache set: ${cacheKey} (TTL: ${metadata.ttlSeconds}s)`);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
this.logger.warn(`Cache write failed for key: ${cacheKey}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
interpolateKey(template, command) {
|
|
56
|
+
return template.replace(/\{(\w+)\}/g, (_, prop) => {
|
|
57
|
+
const value = command[prop];
|
|
58
|
+
return value != null ? String(value) : '';
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.CacheBehavior = CacheBehavior;
|
|
63
|
+
exports.CacheBehavior = CacheBehavior = CacheBehavior_1 = __decorate([
|
|
64
|
+
(0, common_1.Injectable)(),
|
|
65
|
+
__param(0, (0, common_1.Optional)()),
|
|
66
|
+
__param(0, (0, common_1.Inject)(core_1.REDIS_CLIENT)),
|
|
67
|
+
__metadata("design:paramtypes", [Function])
|
|
68
|
+
], CacheBehavior);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Result, PipelineBehavior, PipelineScope } from '@quanticjs/core';
|
|
2
|
+
import type { Redis } from 'ioredis';
|
|
3
|
+
export declare class DistributedLockBehavior implements PipelineBehavior {
|
|
4
|
+
private readonly redis?;
|
|
5
|
+
readonly order: 70;
|
|
6
|
+
readonly scope: PipelineScope;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(redis?: Redis | undefined);
|
|
9
|
+
execute<T>(command: object, next: () => Promise<Result<T>>): Promise<Result<T>>;
|
|
10
|
+
private tryAcquire;
|
|
11
|
+
private release;
|
|
12
|
+
private sleep;
|
|
13
|
+
private interpolateKey;
|
|
14
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var DistributedLockBehavior_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.DistributedLockBehavior = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const core_1 = require("@quanticjs/core");
|
|
19
|
+
const uuid_1 = require("uuid");
|
|
20
|
+
let DistributedLockBehavior = DistributedLockBehavior_1 = class DistributedLockBehavior {
|
|
21
|
+
redis;
|
|
22
|
+
order = core_1.BehaviorOrder.DISTRIBUTED_LOCK;
|
|
23
|
+
scope = 'command';
|
|
24
|
+
logger = new common_1.Logger(DistributedLockBehavior_1.name);
|
|
25
|
+
constructor(redis) {
|
|
26
|
+
this.redis = redis;
|
|
27
|
+
}
|
|
28
|
+
async execute(command, next) {
|
|
29
|
+
const metadata = (0, core_1.getDistributedLockMetadata)(command.constructor);
|
|
30
|
+
if (!metadata || !this.redis) {
|
|
31
|
+
return next();
|
|
32
|
+
}
|
|
33
|
+
const lockKey = `lock:${this.interpolateKey(metadata.key, command)}`;
|
|
34
|
+
const lockValue = (0, uuid_1.v4)();
|
|
35
|
+
const acquired = await this.tryAcquire(lockKey, lockValue, metadata.lockTtlSeconds, metadata.acquireTimeoutSeconds);
|
|
36
|
+
if (!acquired) {
|
|
37
|
+
this.logger.warn(`Failed to acquire lock: ${lockKey}`);
|
|
38
|
+
return core_1.Result.failure(core_1.ErrorType.Conflict, `Resource is locked: ${metadata.key}`);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return await next();
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
await this.release(lockKey, lockValue);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async tryAcquire(key, value, ttlSeconds, timeoutSeconds) {
|
|
48
|
+
const deadline = Date.now() + timeoutSeconds * 1000;
|
|
49
|
+
const retryDelay = 50;
|
|
50
|
+
while (Date.now() < deadline) {
|
|
51
|
+
const result = await this.redis.set(key, value, 'EX', ttlSeconds, 'NX');
|
|
52
|
+
if (result === 'OK')
|
|
53
|
+
return true;
|
|
54
|
+
await this.sleep(retryDelay);
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
async release(key, value) {
|
|
59
|
+
const script = `
|
|
60
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
61
|
+
return redis.call("del", KEYS[1])
|
|
62
|
+
else
|
|
63
|
+
return 0
|
|
64
|
+
end
|
|
65
|
+
`;
|
|
66
|
+
try {
|
|
67
|
+
await this.redis.eval(script, 1, key, value);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
this.logger.warn(`Failed to release lock: ${key}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
sleep(ms) {
|
|
74
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
75
|
+
}
|
|
76
|
+
interpolateKey(template, command) {
|
|
77
|
+
return template.replace(/\{(\w+)\}/g, (_, prop) => {
|
|
78
|
+
const value = command[prop];
|
|
79
|
+
return value != null ? String(value) : '';
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
exports.DistributedLockBehavior = DistributedLockBehavior;
|
|
84
|
+
exports.DistributedLockBehavior = DistributedLockBehavior = DistributedLockBehavior_1 = __decorate([
|
|
85
|
+
(0, common_1.Injectable)(),
|
|
86
|
+
__param(0, (0, common_1.Optional)()),
|
|
87
|
+
__param(0, (0, common_1.Inject)(core_1.REDIS_CLIENT)),
|
|
88
|
+
__metadata("design:paramtypes", [Function])
|
|
89
|
+
], DistributedLockBehavior);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Result, PipelineBehavior, PipelineScope } from '@quanticjs/core';
|
|
2
|
+
import type { Redis } from 'ioredis';
|
|
3
|
+
export declare class InvalidateCacheBehavior implements PipelineBehavior {
|
|
4
|
+
private readonly redis?;
|
|
5
|
+
readonly order: 5;
|
|
6
|
+
readonly scope: PipelineScope;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(redis?: Redis | undefined);
|
|
9
|
+
execute<T>(command: object, next: () => Promise<Result<T>>): Promise<Result<T>>;
|
|
10
|
+
private interpolateKey;
|
|
11
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var InvalidateCacheBehavior_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.InvalidateCacheBehavior = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const core_1 = require("@quanticjs/core");
|
|
19
|
+
let InvalidateCacheBehavior = InvalidateCacheBehavior_1 = class InvalidateCacheBehavior {
|
|
20
|
+
redis;
|
|
21
|
+
order = core_1.BehaviorOrder.INVALIDATE_CACHE;
|
|
22
|
+
scope = 'command';
|
|
23
|
+
logger = new common_1.Logger(InvalidateCacheBehavior_1.name);
|
|
24
|
+
constructor(redis) {
|
|
25
|
+
this.redis = redis;
|
|
26
|
+
}
|
|
27
|
+
async execute(command, next) {
|
|
28
|
+
const metadata = (0, core_1.getInvalidateCacheMetadata)(command.constructor);
|
|
29
|
+
if (!metadata || !this.redis) {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
const result = await next();
|
|
33
|
+
if (!result.isSuccess) {
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
const resolvedKeys = metadata.keys.map((key) => this.interpolateKey(key, command));
|
|
37
|
+
if (resolvedKeys.length === 0) {
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
await this.redis.del(...resolvedKeys);
|
|
42
|
+
this.logger.debug(`Cache invalidated: ${resolvedKeys.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
this.logger.warn(`Cache invalidation failed for keys: ${resolvedKeys.join(', ')}`);
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
interpolateKey(template, command) {
|
|
50
|
+
return template.replace(/\{(\w+)\}/g, (_, prop) => {
|
|
51
|
+
const value = command[prop];
|
|
52
|
+
return value != null ? String(value) : '';
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.InvalidateCacheBehavior = InvalidateCacheBehavior;
|
|
57
|
+
exports.InvalidateCacheBehavior = InvalidateCacheBehavior = InvalidateCacheBehavior_1 = __decorate([
|
|
58
|
+
(0, common_1.Injectable)(),
|
|
59
|
+
__param(0, (0, common_1.Optional)()),
|
|
60
|
+
__param(0, (0, common_1.Inject)(core_1.REDIS_CLIENT)),
|
|
61
|
+
__metadata("design:paramtypes", [Function])
|
|
62
|
+
], InvalidateCacheBehavior);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var QuanticRedisModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.QuanticRedisModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const core_1 = require("@quanticjs/core");
|
|
13
|
+
const redis_module_1 = require("./redis.module");
|
|
14
|
+
const CacheBehavior_1 = require("./CacheBehavior");
|
|
15
|
+
const InvalidateCacheBehavior_1 = require("./InvalidateCacheBehavior");
|
|
16
|
+
const DistributedLockBehavior_1 = require("./DistributedLockBehavior");
|
|
17
|
+
let QuanticRedisModule = QuanticRedisModule_1 = class QuanticRedisModule {
|
|
18
|
+
static forRoot(options = {}) {
|
|
19
|
+
return {
|
|
20
|
+
module: QuanticRedisModule_1,
|
|
21
|
+
imports: [redis_module_1.RedisModule.forRoot(options)],
|
|
22
|
+
providers: [
|
|
23
|
+
CacheBehavior_1.CacheBehavior,
|
|
24
|
+
{ provide: core_1.PIPELINE_BEHAVIOR, useExisting: CacheBehavior_1.CacheBehavior, multi: true },
|
|
25
|
+
InvalidateCacheBehavior_1.InvalidateCacheBehavior,
|
|
26
|
+
{ provide: core_1.PIPELINE_BEHAVIOR, useExisting: InvalidateCacheBehavior_1.InvalidateCacheBehavior, multi: true },
|
|
27
|
+
DistributedLockBehavior_1.DistributedLockBehavior,
|
|
28
|
+
{ provide: core_1.PIPELINE_BEHAVIOR, useExisting: DistributedLockBehavior_1.DistributedLockBehavior, multi: true },
|
|
29
|
+
],
|
|
30
|
+
exports: [
|
|
31
|
+
redis_module_1.RedisModule,
|
|
32
|
+
CacheBehavior_1.CacheBehavior,
|
|
33
|
+
InvalidateCacheBehavior_1.InvalidateCacheBehavior,
|
|
34
|
+
DistributedLockBehavior_1.DistributedLockBehavior,
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.QuanticRedisModule = QuanticRedisModule;
|
|
40
|
+
exports.QuanticRedisModule = QuanticRedisModule = QuanticRedisModule_1 = __decorate([
|
|
41
|
+
(0, common_1.Global)(),
|
|
42
|
+
(0, common_1.Module)({})
|
|
43
|
+
], QuanticRedisModule);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { RedisModule } from './redis.module';
|
|
2
|
+
export type { RedisModuleOptions } from './redis.module';
|
|
3
|
+
export { CacheBehavior } from './CacheBehavior';
|
|
4
|
+
export { InvalidateCacheBehavior } from './InvalidateCacheBehavior';
|
|
5
|
+
export { DistributedLockBehavior } from './DistributedLockBehavior';
|
|
6
|
+
export { QuanticRedisModule } from './QuanticRedisModule';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QuanticRedisModule = exports.DistributedLockBehavior = exports.InvalidateCacheBehavior = exports.CacheBehavior = exports.RedisModule = void 0;
|
|
4
|
+
var redis_module_1 = require("./redis.module");
|
|
5
|
+
Object.defineProperty(exports, "RedisModule", { enumerable: true, get: function () { return redis_module_1.RedisModule; } });
|
|
6
|
+
var CacheBehavior_1 = require("./CacheBehavior");
|
|
7
|
+
Object.defineProperty(exports, "CacheBehavior", { enumerable: true, get: function () { return CacheBehavior_1.CacheBehavior; } });
|
|
8
|
+
var InvalidateCacheBehavior_1 = require("./InvalidateCacheBehavior");
|
|
9
|
+
Object.defineProperty(exports, "InvalidateCacheBehavior", { enumerable: true, get: function () { return InvalidateCacheBehavior_1.InvalidateCacheBehavior; } });
|
|
10
|
+
var DistributedLockBehavior_1 = require("./DistributedLockBehavior");
|
|
11
|
+
Object.defineProperty(exports, "DistributedLockBehavior", { enumerable: true, get: function () { return DistributedLockBehavior_1.DistributedLockBehavior; } });
|
|
12
|
+
var QuanticRedisModule_1 = require("./QuanticRedisModule");
|
|
13
|
+
Object.defineProperty(exports, "QuanticRedisModule", { enumerable: true, get: function () { return QuanticRedisModule_1.QuanticRedisModule; } });
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var RedisModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.RedisModule = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
16
|
+
const core_1 = require("@quanticjs/core");
|
|
17
|
+
let RedisModule = class RedisModule {
|
|
18
|
+
static { RedisModule_1 = this; }
|
|
19
|
+
static logger = new common_1.Logger('RedisModule');
|
|
20
|
+
static forRoot(options = {}) {
|
|
21
|
+
const redisProvider = {
|
|
22
|
+
provide: core_1.REDIS_CLIENT,
|
|
23
|
+
useFactory: () => {
|
|
24
|
+
const url = options.url || process.env.REDIS_URL || 'redis://localhost:6379';
|
|
25
|
+
const client = new ioredis_1.default(url, {
|
|
26
|
+
maxRetriesPerRequest: 3,
|
|
27
|
+
retryStrategy(times) {
|
|
28
|
+
const delay = Math.min(times * 200, 5000);
|
|
29
|
+
RedisModule_1.logger.warn(`Redis reconnecting (attempt ${times}, delay ${delay}ms)`);
|
|
30
|
+
return delay;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
client.on('connect', () => RedisModule_1.logger.log('Redis connected'));
|
|
34
|
+
client.on('error', (err) => RedisModule_1.logger.error('Redis error', err.message));
|
|
35
|
+
return client;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
module: RedisModule_1,
|
|
40
|
+
providers: [redisProvider],
|
|
41
|
+
exports: [core_1.REDIS_CLIENT],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.RedisModule = RedisModule;
|
|
46
|
+
exports.RedisModule = RedisModule = RedisModule_1 = __decorate([
|
|
47
|
+
(0, common_1.Global)(),
|
|
48
|
+
(0, common_1.Module)({})
|
|
49
|
+
], RedisModule);
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quanticjs/redis",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Redis integration for quanticjs — cache, invalidation, distributed locks",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"registry": "https://registry.npmjs.org",
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"test": "jest --passWithNoTests",
|
|
18
|
+
"clean": "rm -rf dist"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@quanticjs/core": "^2.0.0",
|
|
22
|
+
"uuid": "^11.1.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@nestjs/common": "^11.0.0",
|
|
26
|
+
"ioredis": "^5.0.0",
|
|
27
|
+
"reflect-metadata": ">=0.1.0"
|
|
28
|
+
}
|
|
29
|
+
}
|