namirasoft-node-redis 1.4.20 → 1.4.22
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/RedisScope.d.ts +4 -3
- package/dist/RedisScope.js +1 -0
- package/dist/RedisScope.js.map +1 -1
- package/dist/RedisScopeDelay.d.ts +4 -5
- package/dist/RedisScopeDelay.js +0 -1
- package/dist/RedisScopeDelay.js.map +1 -1
- package/dist/RedisScopeHashCounter.d.ts +10 -0
- package/dist/RedisScopeHashCounter.js +51 -0
- package/dist/RedisScopeHashCounter.js.map +1 -0
- package/dist/RedisScopeLimit.d.ts +5 -6
- package/dist/RedisScopeLimit.js +0 -1
- package/dist/RedisScopeLimit.js.map +1 -1
- package/dist/RedisScopeLock.d.ts +4 -5
- package/dist/RedisScopeLock.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +24 -24
- package/src/RedisInstance.ts +17 -17
- package/src/RedisScope.ts +54 -52
- package/src/RedisScopeDelay.ts +55 -57
- package/src/RedisScopeHashCounter.ts +37 -0
- package/src/RedisScopeLimit.ts +60 -62
- package/src/RedisScopeLock.ts +32 -33
- package/src/index.ts +5 -4
package/dist/RedisScope.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { RedisInstance } from "./RedisInstance";
|
|
2
|
-
export declare abstract class RedisScope<
|
|
2
|
+
export declare abstract class RedisScope<KeyParameters, ValueType> {
|
|
3
3
|
protected instance: RedisInstance;
|
|
4
4
|
constructor(instance: RedisInstance);
|
|
5
|
-
protected
|
|
6
|
-
protected
|
|
5
|
+
protected abstract key(key_parameter: KeyParameters): string;
|
|
6
|
+
protected _get(key: string, base64: boolean): Promise<ValueType | null>;
|
|
7
|
+
protected _set(key: string, value: ValueType, base64: boolean, ttl_seconds?: number, NotExists?: boolean): Promise<"OK" | null>;
|
|
7
8
|
protected _del(key: string): Promise<void>;
|
|
8
9
|
}
|
package/dist/RedisScope.js
CHANGED
|
@@ -14,6 +14,7 @@ const namirasoft_core_1 = require("namirasoft-core");
|
|
|
14
14
|
class RedisScope {
|
|
15
15
|
constructor(instance) {
|
|
16
16
|
this.instance = instance;
|
|
17
|
+
this.key = this.key.bind(this);
|
|
17
18
|
this._get = this._get.bind(this);
|
|
18
19
|
this._set = this._set.bind(this);
|
|
19
20
|
this._del = this._del.bind(this);
|
package/dist/RedisScope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisScope.js","sourceRoot":"","sources":["../src/RedisScope.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAoD;AAGpD,MAAsB,UAAU;IAG5B,YAAY,QAAuB;QAE/B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;
|
|
1
|
+
{"version":3,"file":"RedisScope.js","sourceRoot":"","sources":["../src/RedisScope.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAoD;AAGpD,MAAsB,UAAU;IAG5B,YAAY,QAAuB;QAE/B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAEe,IAAI,CAAC,GAAW,EAAE,MAAe;;YAE7C,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe;gBAC3B,OAAO,IAAI,CAAC;YAChB,IAAI,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/C,IAAI,IAAI,EACR,CAAC;gBACG,IAAI,MAAM;oBACN,IAAI,GAAG,mCAAiB,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;gBAChD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAc,CAAC;YACzC,CAAC;YACD,OAAO,IAAI,CAAC;QAChB,CAAC;KAAA;IACe,IAAI;6DAAC,GAAW,EAAE,KAAgB,EAAE,MAAe,EAAE,WAAoB,EAAE,YAAqB,KAAK;YAEjH,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe;gBAC3B,OAAO,IAAI,CAAC;YAChB,IAAI,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACjC,IAAI,MAAM;gBACN,IAAI,GAAG,mCAAiB,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YAChD,IAAI,SAAS,EACb,CAAC;gBACG,IAAI,WAAW;oBACX,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;gBAC9E,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAC3D,CAAC;iBAED,CAAC;gBACG,IAAI,WAAW;oBACX,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;gBACxE,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACrD,CAAC;QACL,CAAC;KAAA;IACe,IAAI,CAAC,GAAW;;YAE5B,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe;gBAC3B,OAAO;YACX,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACxC,CAAC;KAAA;CACJ;AAnDD,gCAmDC;AAAA,CAAC"}
|
|
@@ -8,11 +8,10 @@ export type RedisScopeDelayParameter = {
|
|
|
8
8
|
delay_value: number | null;
|
|
9
9
|
delay_unit: DurationUnit | null;
|
|
10
10
|
};
|
|
11
|
-
export declare abstract class RedisScopeDelay<
|
|
11
|
+
export declare abstract class RedisScopeDelay<KeyParameters> extends RedisScope<KeyParameters, Date> {
|
|
12
12
|
private locker;
|
|
13
13
|
private max_processing_time_in_seconds;
|
|
14
|
-
constructor(instance: RedisInstance, locker: RedisScopeLock<
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
release(key_parameter: Parameters, parameter: RedisScopeDelayParameter): Promise<void>;
|
|
14
|
+
constructor(instance: RedisInstance, locker: RedisScopeLock<KeyParameters>, max_processing_time_in_seconds?: number);
|
|
15
|
+
wait(key_parameter: KeyParameters, parameter: RedisScopeDelayParameter): Promise<void>;
|
|
16
|
+
release(key_parameter: KeyParameters, parameter: RedisScopeDelayParameter): Promise<void>;
|
|
18
17
|
}
|
package/dist/RedisScopeDelay.js
CHANGED
|
@@ -17,7 +17,6 @@ class RedisScopeDelay extends RedisScope_1.RedisScope {
|
|
|
17
17
|
super(instance);
|
|
18
18
|
this.locker = locker;
|
|
19
19
|
this.max_processing_time_in_seconds = max_processing_time_in_seconds;
|
|
20
|
-
this.key = this.key.bind(this);
|
|
21
20
|
this.wait = this.wait.bind(this);
|
|
22
21
|
this.release = this.release.bind(this);
|
|
23
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisScopeDelay.js","sourceRoot":"","sources":["../src/RedisScopeDelay.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAkE;AAElE,6CAA0C;AAU1C,MAAsB,
|
|
1
|
+
{"version":3,"file":"RedisScopeDelay.js","sourceRoot":"","sources":["../src/RedisScopeDelay.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAkE;AAElE,6CAA0C;AAU1C,MAAsB,eAA+B,SAAQ,uBAA+B;IAIxF,YAAY,QAAuB,EAAE,MAAqC,EAAE,iCAAyC,EAAE;QAEnH,KAAK,CAAC,QAAQ,CAAC,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,8BAA8B,GAAG,8BAA8B,CAAC;QACrE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;IACK,IAAI,CAAC,aAA4B,EAAE,SAAmC;;YAExE,IAAI,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,WAAW,IAAI,CAAC,SAAS,CAAC,UAAU;gBACnE,OAAO;YAEX,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAElC,IAAI,eAAe,GAAG,mCAAiB,CAAC,cAAc,CAAC,SAAS,CAAC,WAAW,EAAE,SAAS,CAAC,UAAU,CAAC,CAAC;YACpG,IAAI,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,8BAA8B,CAAC;YAC1F,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;YAE1D,IAAI,OAAO,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC1C,IAAI,CAAC,OAAO;gBACR,OAAO;YACX,OAAO,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;YAE5B,IAAI,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;YAC1D,IAAI,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,eAAe,GAAG,UAAU,EAAE,CAAC,CAAC,CAAC;YAE/D,IAAI,cAAc,GAAG,CAAC;gBAClB,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC;QAC1E,CAAC;KAAA;IACK,OAAO,CAAC,aAA4B,EAAE,SAAmC;;YAE3E,IAAI,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,WAAW,IAAI,CAAC,SAAS,CAAC,UAAU;gBACnE,OAAO;YAEX,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAClC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,EAAE,EAAE,KAAK,CAAC,CAAC;YACxC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAC5C,CAAC;KAAA;CACJ;AA3CD,0CA2CC;AAAA,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
export declare abstract class RedisScopeHashCounter<KeyParameters, FieldParameters> extends RedisScope<KeyParameters | null, Date> {
|
|
4
|
+
constructor(instance: RedisInstance);
|
|
5
|
+
protected abstract field(field_parameter: FieldParameters): string;
|
|
6
|
+
increase(key_parameter: KeyParameters, field_parameter: FieldParameters, step: number): Promise<number>;
|
|
7
|
+
listKeys(count?: number): Promise<string[]>;
|
|
8
|
+
listFields(key_parameter: KeyParameters): Promise<Record<string, string>>;
|
|
9
|
+
del(key_parameter: KeyParameters): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RedisScopeHashCounter = void 0;
|
|
13
|
+
const RedisScope_1 = require("./RedisScope");
|
|
14
|
+
class RedisScopeHashCounter extends RedisScope_1.RedisScope {
|
|
15
|
+
constructor(instance) {
|
|
16
|
+
super(instance);
|
|
17
|
+
this.increase = this.increase.bind(this);
|
|
18
|
+
this.listKeys = this.listKeys.bind(this);
|
|
19
|
+
this.listFields = this.listFields.bind(this);
|
|
20
|
+
this.del = this.del.bind(this);
|
|
21
|
+
}
|
|
22
|
+
increase(key_parameter, field_parameter, step) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
let key = this.key(key_parameter);
|
|
25
|
+
let field = this.field(field_parameter);
|
|
26
|
+
return yield this.instance.client.hincrby(key, field, step);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
listKeys() {
|
|
30
|
+
return __awaiter(this, arguments, void 0, function* (count = 1) {
|
|
31
|
+
let cursor = "0";
|
|
32
|
+
const [_, foundKeys] = yield this.instance.client.scan(cursor, "MATCH", this.key(null), "COUNT", count);
|
|
33
|
+
return foundKeys;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
listFields(key_parameter) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
let key = this.key(key_parameter);
|
|
39
|
+
return yield this.instance.client.hgetall(key);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
del(key_parameter) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
let key = this.key(key_parameter);
|
|
45
|
+
return yield this._del(key);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.RedisScopeHashCounter = RedisScopeHashCounter;
|
|
50
|
+
;
|
|
51
|
+
//# sourceMappingURL=RedisScopeHashCounter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RedisScopeHashCounter.js","sourceRoot":"","sources":["../src/RedisScopeHashCounter.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAA0C;AAE1C,MAAsB,qBAAsD,SAAQ,uBAAsC;IAEtH,YAAY,QAAuB;QAE/B,KAAK,CAAC,QAAQ,CAAC,CAAC;QAChB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC7C,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAEK,QAAQ,CAAC,aAA4B,EAAE,eAAgC,EAAE,IAAY;;YAEvF,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAClC,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YACxC,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAChE,CAAC;KAAA;IACK,QAAQ;6DAAC,QAAgB,CAAC;YAE5B,IAAI,MAAM,GAAG,GAAG,CAAC;YACjB,MAAM,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;YACxG,OAAO,SAAS,CAAC;QACrB,CAAC;KAAA;IACK,UAAU,CAAC,aAA4B;;YAEzC,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAClC,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACnD,CAAC;KAAA;IACK,GAAG,CAAC,aAA4B;;YAElC,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAClC,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;KAAA;CACJ;AAjCD,sDAiCC;AAAA,CAAC"}
|
|
@@ -9,11 +9,10 @@ export type RedisScopeLimitParameter = {
|
|
|
9
9
|
limit_per_value: number | null;
|
|
10
10
|
limit_per_unit: DurationUnit | null;
|
|
11
11
|
};
|
|
12
|
-
export declare abstract class RedisScopeLimit<
|
|
12
|
+
export declare abstract class RedisScopeLimit<KeyParameters> extends RedisScope<KeyParameters, number> {
|
|
13
13
|
constructor(instance: RedisInstance);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
check(key_parameter: Parameters, parameter: RedisScopeLimitParameter): Promise<void>;
|
|
14
|
+
getCount(key_parameter: KeyParameters, limit_per_value: number, limit_per_unit: DurationUnit): Promise<number>;
|
|
15
|
+
add(key_parameter: KeyParameters, limit_per_value: number, limit_per_unit: DurationUnit): Promise<void>;
|
|
16
|
+
del(key_parameter: KeyParameters): Promise<void>;
|
|
17
|
+
check(key_parameter: KeyParameters, parameter: RedisScopeLimitParameter): Promise<void>;
|
|
19
18
|
}
|
package/dist/RedisScopeLimit.js
CHANGED
|
@@ -15,7 +15,6 @@ const RedisScope_1 = require("./RedisScope");
|
|
|
15
15
|
class RedisScopeLimit extends RedisScope_1.RedisScope {
|
|
16
16
|
constructor(instance) {
|
|
17
17
|
super(instance);
|
|
18
|
-
this.key = this.key.bind(this);
|
|
19
18
|
this.getCount = this.getCount.bind(this);
|
|
20
19
|
this.add = this.add.bind(this);
|
|
21
20
|
this.del = this.del.bind(this);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisScopeLimit.js","sourceRoot":"","sources":["../src/RedisScopeLimit.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAiF;AAEjF,6CAA0C;AAW1C,MAAsB,
|
|
1
|
+
{"version":3,"file":"RedisScopeLimit.js","sourceRoot":"","sources":["../src/RedisScopeLimit.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAiF;AAEjF,6CAA0C;AAW1C,MAAsB,eAA+B,SAAQ,uBAAiC;IAE1F,YAAY,QAAuB;QAE/B,KAAK,CAAC,QAAQ,CAAC,CAAC;QAChB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IACK,QAAQ,CAAC,aAA4B,EAAE,eAAuB,EAAE,cAA4B;;YAE9F,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAElC,IAAI,YAAY,GAAG,mCAAiB,CAAC,cAAc,CAAC,eAAe,EAAE,cAAc,CAAC,CAAC;YACrF,IAAI,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC;YAC7B,IAAI,UAAU,GAAG,+BAAa,CAAC,eAAe,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;YAE1E,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,OAAO,EAAE,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC;QAC/F,CAAC;KAAA;IACK,GAAG,CAAC,aAA4B,EAAE,eAAuB,EAAE,cAA4B;;YAEzF,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAElC,IAAI,YAAY,GAAG,mCAAiB,CAAC,cAAc,CAAC,eAAe,EAAE,cAAc,CAAC,CAAC;YACrF,IAAI,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC;YAC7B,IAAI,UAAU,GAAG,+BAAa,CAAC,eAAe,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;YAEzE,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,OAAO,EAAE,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC;YACnF,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,CAAC,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9E,CAAC;KAAA;IACK,GAAG,CAAC,aAA4B;;YAElC,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAClC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;KAAA;IACK,KAAK,CAAC,aAA4B,EAAE,SAAmC;;YAEzE,IAAI,SAAS,CAAC,KAAK;gBACf,IAAI,SAAS,CAAC,WAAW,IAAI,SAAS,CAAC,eAAe,IAAI,SAAS,CAAC,cAAc,EAClF,CAAC;oBACG,IAAI,KAAK,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,SAAS,CAAC,eAAe,EAAE,SAAS,CAAC,cAAc,CAAC,CAAC;oBACpG,IAAI,KAAK,GAAG,SAAS,CAAC,WAAW;wBAC7B,MAAM,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,SAAS,CAAC,eAAe,EAAE,SAAS,CAAC,cAAc,CAAC,CAAC;;wBAEnF,MAAM,IAAI,KAAK,CAAC,0BAA0B,SAAS,CAAC,IAAI,MAAM,SAAS,CAAC,EAAE,SAAS,SAAS,CAAC,WAAW,mBAAmB,SAAS,CAAC,eAAe,IAAI,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC;gBAC5L,CAAC;QACT,CAAC;KAAA;CACJ;AA/CD,0CA+CC;AAAA,CAAC"}
|
package/dist/RedisScopeLock.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { RedisInstance } from "./RedisInstance";
|
|
2
2
|
import { RedisScope } from "./RedisScope";
|
|
3
|
-
export declare abstract class RedisScopeLock<
|
|
3
|
+
export declare abstract class RedisScopeLock<KeyParameters> extends RedisScope<KeyParameters, number> {
|
|
4
4
|
private ttl_seconds;
|
|
5
5
|
constructor(instance: RedisInstance, ttl_seconds: number);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
unlock(key_parameter: Parameters): Promise<void>;
|
|
6
|
+
lock(key_parameter: KeyParameters, ttl_seconds?: number): Promise<boolean>;
|
|
7
|
+
waitForLock(key_parameter: KeyParameters, ttl_seconds?: number): Promise<void>;
|
|
8
|
+
unlock(key_parameter: KeyParameters): Promise<void>;
|
|
10
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisScopeLock.js","sourceRoot":"","sources":["../src/RedisScopeLock.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAA0C;AAE1C,MAAsB,
|
|
1
|
+
{"version":3,"file":"RedisScopeLock.js","sourceRoot":"","sources":["../src/RedisScopeLock.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAA0C;AAE1C,MAAsB,cAA8B,SAAQ,uBAAiC;IAEzF,YAAY,QAAuB,EAAU,WAAmB;QAE5D,KAAK,CAAC,QAAQ,CAAC,CAAC;QAFyB,gBAAW,GAAX,WAAW,CAAQ;QAG5D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IACK,IAAI,CAAC,aAA4B,EAAE,WAAoB;;YAEzD,IAAI,IAAI,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,IAAI,CAAC,WAAW,CAAC;YAC3C,IAAI,GAAG,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YACpF,OAAO,GAAG,KAAK,IAAI,CAAC;QACxB,CAAC;KAAA;IACK,WAAW,CAAC,aAA4B,EAAE,WAAoB;;YAEhE,OAAO,IAAI,EACX,CAAC;gBACG,IAAI,GAAG,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;gBACtD,IAAI,GAAG;oBACH,MAAM;gBACV,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;YAClD,CAAC;QACL,CAAC;KAAA;IACK,MAAM,CAAC,aAA4B;;YAErC,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;QAC7C,CAAC;KAAA;CACJ;AA7BD,wCA6BC;AAAA,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./RedisInstance"), exports);
|
|
18
18
|
__exportStar(require("./RedisScope"), exports);
|
|
19
19
|
__exportStar(require("./RedisScopeDelay"), exports);
|
|
20
|
+
__exportStar(require("./RedisScopeHashCounter"), exports);
|
|
20
21
|
__exportStar(require("./RedisScopeLimit"), exports);
|
|
21
22
|
__exportStar(require("./RedisScopeLock"), exports);
|
|
22
23
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,+CAA6B;AAC7B,oDAAkC;AAClC,oDAAkC;AAClC,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,+CAA6B;AAC7B,oDAAkC;AAClC,0DAAwC;AACxC,oDAAkC;AAClC,mDAAiC"}
|
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "namirasoft-node-redis",
|
|
3
|
-
"title": "Namirasoft Node Redis NPM Package",
|
|
4
|
-
"description": "Namira Software Corporation Node Redis NPM Package",
|
|
5
|
-
"icon": "logo.png",
|
|
6
|
-
"logo": "https://static.namirasoft.com/image/application/redis/logo/base.png",
|
|
7
|
-
"language": "ts",
|
|
8
|
-
"framework": "npm",
|
|
9
|
-
"application": "package",
|
|
10
|
-
"private": false,
|
|
11
|
-
"version": "1.4.
|
|
12
|
-
"author": "Amir Abolhasani",
|
|
13
|
-
"license": "MIT",
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": ""
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@types/node": "^
|
|
21
|
-
"ioredis": "^5.
|
|
22
|
-
"namirasoft-core": "^1.4.
|
|
23
|
-
"namirasoft-log": "^1.4.30"
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "namirasoft-node-redis",
|
|
3
|
+
"title": "Namirasoft Node Redis NPM Package",
|
|
4
|
+
"description": "Namira Software Corporation Node Redis NPM Package",
|
|
5
|
+
"icon": "logo.png",
|
|
6
|
+
"logo": "https://static.namirasoft.com/image/application/redis/logo/base.png",
|
|
7
|
+
"language": "ts",
|
|
8
|
+
"framework": "npm",
|
|
9
|
+
"application": "package",
|
|
10
|
+
"private": false,
|
|
11
|
+
"version": "1.4.22",
|
|
12
|
+
"author": "Amir Abolhasani",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": ""
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/node": "^24.1.0",
|
|
21
|
+
"ioredis": "^5.7.0",
|
|
22
|
+
"namirasoft-core": "^1.4.80",
|
|
23
|
+
"namirasoft-log": "^1.4.30"
|
|
24
|
+
}
|
|
25
25
|
}
|
package/src/RedisInstance.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as Redis from "ioredis";
|
|
2
|
-
import { Logger } from "namirasoft-log";
|
|
3
|
-
|
|
4
|
-
export abstract class RedisInstance
|
|
5
|
-
{
|
|
6
|
-
public client!: Redis.Redis;
|
|
7
|
-
constructor(host: string, port: number, password: string, db: number = 0)
|
|
8
|
-
{
|
|
9
|
-
if (!process.env.NAMIRASOFT_MUTE)
|
|
10
|
-
this.client = new Redis.Redis({ host, port, password, db });
|
|
11
|
-
Logger.main?.success(`Redis was connected to: ${host}:${port}`);
|
|
12
|
-
this.disconnect = this.disconnect.bind(this);
|
|
13
|
-
}
|
|
14
|
-
disconnect()
|
|
15
|
-
{
|
|
16
|
-
this.client?.disconnect();
|
|
17
|
-
}
|
|
1
|
+
import * as Redis from "ioredis";
|
|
2
|
+
import { Logger } from "namirasoft-log";
|
|
3
|
+
|
|
4
|
+
export abstract class RedisInstance
|
|
5
|
+
{
|
|
6
|
+
public client!: Redis.Redis;
|
|
7
|
+
constructor(host: string, port: number, password: string, db: number = 0)
|
|
8
|
+
{
|
|
9
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
10
|
+
this.client = new Redis.Redis({ host, port, password, db });
|
|
11
|
+
Logger.main?.success(`Redis was connected to: ${host}:${port}`);
|
|
12
|
+
this.disconnect = this.disconnect.bind(this);
|
|
13
|
+
}
|
|
14
|
+
disconnect()
|
|
15
|
+
{
|
|
16
|
+
this.client?.disconnect();
|
|
17
|
+
}
|
|
18
18
|
};
|
package/src/RedisScope.ts
CHANGED
|
@@ -1,53 +1,55 @@
|
|
|
1
|
-
import { EncodingOperation } from "namirasoft-core";
|
|
2
|
-
import { RedisInstance } from "./RedisInstance";
|
|
3
|
-
|
|
4
|
-
export abstract class RedisScope<
|
|
5
|
-
{
|
|
6
|
-
protected instance: RedisInstance;
|
|
7
|
-
constructor(instance: RedisInstance)
|
|
8
|
-
{
|
|
9
|
-
this.instance = instance;
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
import { EncodingOperation } from "namirasoft-core";
|
|
2
|
+
import { RedisInstance } from "./RedisInstance";
|
|
3
|
+
|
|
4
|
+
export abstract class RedisScope<KeyParameters, ValueType>
|
|
5
|
+
{
|
|
6
|
+
protected instance: RedisInstance;
|
|
7
|
+
constructor(instance: RedisInstance)
|
|
8
|
+
{
|
|
9
|
+
this.instance = instance;
|
|
10
|
+
this.key = this.key.bind(this);
|
|
11
|
+
this._get = this._get.bind(this);
|
|
12
|
+
this._set = this._set.bind(this);
|
|
13
|
+
this._del = this._del.bind(this);
|
|
14
|
+
}
|
|
15
|
+
protected abstract key(key_parameter: KeyParameters): string;
|
|
16
|
+
protected async _get(key: string, base64: boolean): Promise<ValueType | null>
|
|
17
|
+
{
|
|
18
|
+
if (process.env.NAMIRASOFT_MUTE)
|
|
19
|
+
return null;
|
|
20
|
+
let data = await this.instance.client.get(key);
|
|
21
|
+
if (data)
|
|
22
|
+
{
|
|
23
|
+
if (base64)
|
|
24
|
+
data = EncodingOperation.Base64Decode(data);
|
|
25
|
+
return JSON.parse(data) as ValueType;
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
protected async _set(key: string, value: ValueType, base64: boolean, ttl_seconds?: number, NotExists: boolean = false)
|
|
30
|
+
{
|
|
31
|
+
if (process.env.NAMIRASOFT_MUTE)
|
|
32
|
+
return null;
|
|
33
|
+
let data = JSON.stringify(value);
|
|
34
|
+
if (base64)
|
|
35
|
+
data = EncodingOperation.Base64Encode(data);
|
|
36
|
+
if (NotExists)
|
|
37
|
+
{
|
|
38
|
+
if (ttl_seconds)
|
|
39
|
+
return await this.instance.client.set(key, data, "EX", ttl_seconds, "NX");
|
|
40
|
+
return await this.instance.client.set(key, data, "NX");
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
{
|
|
44
|
+
if (ttl_seconds)
|
|
45
|
+
return await this.instance.client.set(key, data, "EX", ttl_seconds);
|
|
46
|
+
return await this.instance.client.set(key, data);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
protected async _del(key: string)
|
|
50
|
+
{
|
|
51
|
+
if (process.env.NAMIRASOFT_MUTE)
|
|
52
|
+
return;
|
|
53
|
+
await this.instance.client.del(key);
|
|
54
|
+
}
|
|
53
55
|
};
|
package/src/RedisScopeDelay.ts
CHANGED
|
@@ -1,58 +1,56 @@
|
|
|
1
|
-
import { DurationUnit, TimeUnitOperation } from "namirasoft-core";
|
|
2
|
-
import { RedisInstance } from "./RedisInstance";
|
|
3
|
-
import { RedisScope } from "./RedisScope";
|
|
4
|
-
import { RedisScopeLock } from "./RedisScopeLock";
|
|
5
|
-
|
|
6
|
-
export type RedisScopeDelayParameter = {
|
|
7
|
-
id: string;
|
|
8
|
-
delay: boolean;
|
|
9
|
-
delay_value: number | null;
|
|
10
|
-
delay_unit: DurationUnit | null;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export abstract class RedisScopeDelay<
|
|
14
|
-
{
|
|
15
|
-
private locker: RedisScopeLock<
|
|
16
|
-
private max_processing_time_in_seconds: number;
|
|
17
|
-
constructor(instance: RedisInstance, locker: RedisScopeLock<
|
|
18
|
-
{
|
|
19
|
-
super(instance);
|
|
20
|
-
this.locker = locker;
|
|
21
|
-
this.max_processing_time_in_seconds = max_processing_time_in_seconds;
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
await this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
await this.locker.unlock(key_parameter);
|
|
57
|
-
}
|
|
1
|
+
import { DurationUnit, TimeUnitOperation } from "namirasoft-core";
|
|
2
|
+
import { RedisInstance } from "./RedisInstance";
|
|
3
|
+
import { RedisScope } from "./RedisScope";
|
|
4
|
+
import { RedisScopeLock } from "./RedisScopeLock";
|
|
5
|
+
|
|
6
|
+
export type RedisScopeDelayParameter = {
|
|
7
|
+
id: string;
|
|
8
|
+
delay: boolean;
|
|
9
|
+
delay_value: number | null;
|
|
10
|
+
delay_unit: DurationUnit | null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export abstract class RedisScopeDelay<KeyParameters> extends RedisScope<KeyParameters, Date>
|
|
14
|
+
{
|
|
15
|
+
private locker: RedisScopeLock<KeyParameters>;
|
|
16
|
+
private max_processing_time_in_seconds: number;
|
|
17
|
+
constructor(instance: RedisInstance, locker: RedisScopeLock<KeyParameters>, max_processing_time_in_seconds: number = 60)
|
|
18
|
+
{
|
|
19
|
+
super(instance);
|
|
20
|
+
this.locker = locker;
|
|
21
|
+
this.max_processing_time_in_seconds = max_processing_time_in_seconds;
|
|
22
|
+
this.wait = this.wait.bind(this);
|
|
23
|
+
this.release = this.release.bind(this);
|
|
24
|
+
}
|
|
25
|
+
async wait(key_parameter: KeyParameters, parameter: RedisScopeDelayParameter): Promise<void>
|
|
26
|
+
{
|
|
27
|
+
if (!parameter.delay || !parameter.delay_value || !parameter.delay_unit)
|
|
28
|
+
return;
|
|
29
|
+
|
|
30
|
+
let key = this.key(key_parameter);
|
|
31
|
+
|
|
32
|
+
let delay_requested = TimeUnitOperation.toMilliseconds(parameter.delay_value, parameter.delay_unit);
|
|
33
|
+
let ttl_seconds = Math.ceil(delay_requested / 1000) + this.max_processing_time_in_seconds;
|
|
34
|
+
await this.locker.waitForLock(key_parameter, ttl_seconds);
|
|
35
|
+
|
|
36
|
+
let LastRun = await this._get(key, false);
|
|
37
|
+
if (!LastRun)
|
|
38
|
+
return;
|
|
39
|
+
LastRun = new Date(LastRun);
|
|
40
|
+
|
|
41
|
+
let difference = new Date().getTime() - LastRun.getTime();
|
|
42
|
+
let delay_required = Math.max(delay_requested - difference, 0);
|
|
43
|
+
|
|
44
|
+
if (delay_required > 0)
|
|
45
|
+
await new Promise(resolve => setTimeout(resolve, delay_required));
|
|
46
|
+
}
|
|
47
|
+
async release(key_parameter: KeyParameters, parameter: RedisScopeDelayParameter)
|
|
48
|
+
{
|
|
49
|
+
if (!parameter.delay || !parameter.delay_value || !parameter.delay_unit)
|
|
50
|
+
return;
|
|
51
|
+
|
|
52
|
+
let key = this.key(key_parameter);
|
|
53
|
+
await this._set(key, new Date(), false);
|
|
54
|
+
await this.locker.unlock(key_parameter);
|
|
55
|
+
}
|
|
58
56
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
|
|
4
|
+
export abstract class RedisScopeHashCounter<KeyParameters, FieldParameters> extends RedisScope<KeyParameters | null, Date>
|
|
5
|
+
{
|
|
6
|
+
constructor(instance: RedisInstance)
|
|
7
|
+
{
|
|
8
|
+
super(instance);
|
|
9
|
+
this.increase = this.increase.bind(this);
|
|
10
|
+
this.listKeys = this.listKeys.bind(this);
|
|
11
|
+
this.listFields = this.listFields.bind(this);
|
|
12
|
+
this.del = this.del.bind(this);
|
|
13
|
+
}
|
|
14
|
+
protected abstract field(field_parameter: FieldParameters): string;
|
|
15
|
+
async increase(key_parameter: KeyParameters, field_parameter: FieldParameters, step: number): Promise<number>
|
|
16
|
+
{
|
|
17
|
+
let key = this.key(key_parameter);
|
|
18
|
+
let field = this.field(field_parameter);
|
|
19
|
+
return await this.instance.client.hincrby(key, field, step);
|
|
20
|
+
}
|
|
21
|
+
async listKeys(count: number = 1)
|
|
22
|
+
{
|
|
23
|
+
let cursor = "0";
|
|
24
|
+
const [_, foundKeys] = await this.instance.client.scan(cursor, "MATCH", this.key(null), "COUNT", count);
|
|
25
|
+
return foundKeys;
|
|
26
|
+
}
|
|
27
|
+
async listFields(key_parameter: KeyParameters)
|
|
28
|
+
{
|
|
29
|
+
let key = this.key(key_parameter);
|
|
30
|
+
return await this.instance.client.hgetall(key);
|
|
31
|
+
}
|
|
32
|
+
async del(key_parameter: KeyParameters)
|
|
33
|
+
{
|
|
34
|
+
let key = this.key(key_parameter);
|
|
35
|
+
return await this._del(key);
|
|
36
|
+
}
|
|
37
|
+
};
|
package/src/RedisScopeLimit.ts
CHANGED
|
@@ -1,63 +1,61 @@
|
|
|
1
|
-
import { DurationUnit, TimeOperation, TimeUnitOperation } from "namirasoft-core";
|
|
2
|
-
import { RedisInstance } from "./RedisInstance";
|
|
3
|
-
import { RedisScope } from "./RedisScope";
|
|
4
|
-
|
|
5
|
-
export type RedisScopeLimitParameter = {
|
|
6
|
-
id: string;
|
|
7
|
-
name: string;
|
|
8
|
-
limit: boolean;
|
|
9
|
-
limit_count: number | null;
|
|
10
|
-
limit_per_value: number | null;
|
|
11
|
-
limit_per_unit: DurationUnit | null;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export abstract class RedisScopeLimit<
|
|
15
|
-
{
|
|
16
|
-
constructor(instance: RedisInstance)
|
|
17
|
-
{
|
|
18
|
-
super(instance);
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
let
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
let
|
|
38
|
-
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
import { DurationUnit, TimeOperation, TimeUnitOperation } from "namirasoft-core";
|
|
2
|
+
import { RedisInstance } from "./RedisInstance";
|
|
3
|
+
import { RedisScope } from "./RedisScope";
|
|
4
|
+
|
|
5
|
+
export type RedisScopeLimitParameter = {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
limit: boolean;
|
|
9
|
+
limit_count: number | null;
|
|
10
|
+
limit_per_value: number | null;
|
|
11
|
+
limit_per_unit: DurationUnit | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export abstract class RedisScopeLimit<KeyParameters> extends RedisScope<KeyParameters, number>
|
|
15
|
+
{
|
|
16
|
+
constructor(instance: RedisInstance)
|
|
17
|
+
{
|
|
18
|
+
super(instance);
|
|
19
|
+
this.getCount = this.getCount.bind(this);
|
|
20
|
+
this.add = this.add.bind(this);
|
|
21
|
+
this.del = this.del.bind(this);
|
|
22
|
+
}
|
|
23
|
+
async getCount(key_parameter: KeyParameters, limit_per_value: number, limit_per_unit: DurationUnit): Promise<number>
|
|
24
|
+
{
|
|
25
|
+
let key = this.key(key_parameter);
|
|
26
|
+
|
|
27
|
+
let milliseconds = TimeUnitOperation.toMilliseconds(limit_per_value, limit_per_unit);
|
|
28
|
+
let date_finish = new Date();
|
|
29
|
+
let date_start = TimeOperation.millisecondsAgo(milliseconds, date_finish);
|
|
30
|
+
|
|
31
|
+
return await this.instance.client.zcount(key, date_start.getTime(), date_finish.getTime());
|
|
32
|
+
}
|
|
33
|
+
async add(key_parameter: KeyParameters, limit_per_value: number, limit_per_unit: DurationUnit)
|
|
34
|
+
{
|
|
35
|
+
let key = this.key(key_parameter);
|
|
36
|
+
|
|
37
|
+
let milliseconds = TimeUnitOperation.toMilliseconds(limit_per_value, limit_per_unit);
|
|
38
|
+
let date_finish = new Date();
|
|
39
|
+
let date_start = TimeOperation.millisecondsAgo(milliseconds, date_finish)
|
|
40
|
+
|
|
41
|
+
await this.instance.client.zadd(key, date_finish.getTime(), date_finish.getTime());
|
|
42
|
+
await this.instance.client.zremrangebyscore(key, 0, date_start.getTime());
|
|
43
|
+
}
|
|
44
|
+
async del(key_parameter: KeyParameters)
|
|
45
|
+
{
|
|
46
|
+
let key = this.key(key_parameter);
|
|
47
|
+
await this._del(key);
|
|
48
|
+
}
|
|
49
|
+
async check(key_parameter: KeyParameters, parameter: RedisScopeLimitParameter)
|
|
50
|
+
{
|
|
51
|
+
if (parameter.limit)
|
|
52
|
+
if (parameter.limit_count && parameter.limit_per_value && parameter.limit_per_unit)
|
|
53
|
+
{
|
|
54
|
+
let count = await this.getCount(key_parameter, parameter.limit_per_value, parameter.limit_per_unit);
|
|
55
|
+
if (count < parameter.limit_count)
|
|
56
|
+
await this.add(key_parameter, parameter.limit_per_value, parameter.limit_per_unit);
|
|
57
|
+
else
|
|
58
|
+
throw new Error(`Limit has reached for '${parameter.name}' '${parameter.id}' for ${parameter.limit_count} messages every ${parameter.limit_per_value} ${parameter.limit_per_unit}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
63
61
|
};
|
package/src/RedisScopeLock.ts
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
import { RedisInstance } from "./RedisInstance";
|
|
2
|
-
import { RedisScope } from "./RedisScope";
|
|
3
|
-
|
|
4
|
-
export abstract class RedisScopeLock<
|
|
5
|
-
{
|
|
6
|
-
constructor(instance: RedisInstance, private ttl_seconds: number)
|
|
7
|
-
{
|
|
8
|
-
super(instance);
|
|
9
|
-
this.lock = this.lock.bind(this);
|
|
10
|
-
this.waitForLock = this.waitForLock.bind(this);
|
|
11
|
-
this.unlock = this.unlock.bind(this);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
|
|
4
|
+
export abstract class RedisScopeLock<KeyParameters> extends RedisScope<KeyParameters, number>
|
|
5
|
+
{
|
|
6
|
+
constructor(instance: RedisInstance, private ttl_seconds: number)
|
|
7
|
+
{
|
|
8
|
+
super(instance);
|
|
9
|
+
this.lock = this.lock.bind(this);
|
|
10
|
+
this.waitForLock = this.waitForLock.bind(this);
|
|
11
|
+
this.unlock = this.unlock.bind(this);
|
|
12
|
+
}
|
|
13
|
+
async lock(key_parameter: KeyParameters, ttl_seconds?: number): Promise<boolean>
|
|
14
|
+
{
|
|
15
|
+
let time = ttl_seconds ?? this.ttl_seconds;
|
|
16
|
+
let ans = await this._set(this.key(key_parameter), Math.random(), true, time, true);
|
|
17
|
+
return ans === "OK";
|
|
18
|
+
}
|
|
19
|
+
async waitForLock(key_parameter: KeyParameters, ttl_seconds?: number): Promise<void>
|
|
20
|
+
{
|
|
21
|
+
while (true)
|
|
22
|
+
{
|
|
23
|
+
let ans = await this.lock(key_parameter, ttl_seconds);
|
|
24
|
+
if (ans)
|
|
25
|
+
break;
|
|
26
|
+
await new Promise(res => setTimeout(res, 10));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async unlock(key_parameter: KeyParameters)
|
|
30
|
+
{
|
|
31
|
+
await this._del(this.key(key_parameter));
|
|
32
|
+
}
|
|
34
33
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from "./RedisInstance";
|
|
2
|
-
export * from "./RedisScope";
|
|
3
|
-
export * from "./RedisScopeDelay";
|
|
4
|
-
export * from "./
|
|
1
|
+
export * from "./RedisInstance";
|
|
2
|
+
export * from "./RedisScope";
|
|
3
|
+
export * from "./RedisScopeDelay";
|
|
4
|
+
export * from "./RedisScopeHashCounter";
|
|
5
|
+
export * from "./RedisScopeLimit";
|
|
5
6
|
export * from "./RedisScopeLock";
|