namirasoft-node-redis 1.4.21 → 1.4.23
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 +1 -1
- package/dist/RedisScopeHashCounter.d.ts +4 -3
- package/dist/RedisScopeHashCounter.js +12 -2
- package/dist/RedisScopeHashCounter.js.map +1 -1
- package/dist/RedisScopeTotalCount.d.ts +14 -0
- package/dist/RedisScopeTotalCount.js +53 -0
- package/dist/RedisScopeTotalCount.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/RedisScope.ts +1 -1
- package/src/RedisScopeHashCounter.ts +12 -4
- package/src/RedisScopeTotalCount.ts +44 -0
- package/src/index.ts +2 -1
package/dist/RedisScope.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RedisInstance } from "./RedisInstance";
|
|
|
2
2
|
export declare abstract class RedisScope<KeyParameters, ValueType> {
|
|
3
3
|
protected instance: RedisInstance;
|
|
4
4
|
constructor(instance: RedisInstance);
|
|
5
|
-
|
|
5
|
+
abstract key(key_parameter: KeyParameters): string;
|
|
6
6
|
protected _get(key: string, base64: boolean): Promise<ValueType | null>;
|
|
7
7
|
protected _set(key: string, value: ValueType, base64: boolean, ttl_seconds?: number, NotExists?: boolean): Promise<"OK" | null>;
|
|
8
8
|
protected _del(key: string): Promise<void>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { RedisInstance } from "./RedisInstance";
|
|
2
2
|
import { RedisScope } from "./RedisScope";
|
|
3
|
-
export declare abstract class RedisScopeHashCounter<KeyParameters> extends RedisScope<KeyParameters, Date> {
|
|
3
|
+
export declare abstract class RedisScopeHashCounter<KeyParameters, FieldParameters> extends RedisScope<KeyParameters | null, Date> {
|
|
4
4
|
constructor(instance: RedisInstance);
|
|
5
|
-
|
|
6
|
-
increase(key_parameter: KeyParameters,
|
|
5
|
+
abstract field(field_parameter: FieldParameters): string;
|
|
6
|
+
increase(key_parameter: KeyParameters, field_parameter: FieldParameters, step: number): Promise<number>;
|
|
7
|
+
hget(key_parameter: KeyParameters, field_parameter: FieldParameters): Promise<number>;
|
|
7
8
|
listKeys(count?: number): Promise<string[]>;
|
|
8
9
|
listFields(key_parameter: KeyParameters): Promise<Record<string, string>>;
|
|
9
10
|
del(key_parameter: KeyParameters): Promise<void>;
|
|
@@ -19,16 +19,26 @@ class RedisScopeHashCounter extends RedisScope_1.RedisScope {
|
|
|
19
19
|
this.listFields = this.listFields.bind(this);
|
|
20
20
|
this.del = this.del.bind(this);
|
|
21
21
|
}
|
|
22
|
-
increase(key_parameter,
|
|
22
|
+
increase(key_parameter, field_parameter, step) {
|
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
24
|
let key = this.key(key_parameter);
|
|
25
|
+
let field = this.field(field_parameter);
|
|
25
26
|
return yield this.instance.client.hincrby(key, field, step);
|
|
26
27
|
});
|
|
27
28
|
}
|
|
29
|
+
hget(key_parameter, field_parameter) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
var _a;
|
|
32
|
+
let key = this.key(key_parameter);
|
|
33
|
+
let field = this.field(field_parameter);
|
|
34
|
+
let res = (_a = yield this.instance.client.hget(key, field)) !== null && _a !== void 0 ? _a : "0";
|
|
35
|
+
return parseInt(res);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
28
38
|
listKeys() {
|
|
29
39
|
return __awaiter(this, arguments, void 0, function* (count = 1) {
|
|
30
40
|
let cursor = "0";
|
|
31
|
-
const [_, foundKeys] = yield this.instance.client.scan(cursor, "MATCH", this.
|
|
41
|
+
const [_, foundKeys] = yield this.instance.client.scan(cursor, "MATCH", this.key(null), "COUNT", count);
|
|
32
42
|
return foundKeys;
|
|
33
43
|
});
|
|
34
44
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RedisScopeHashCounter.js","sourceRoot":"","sources":["../src/Red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,CAAC,mCAAI,GAAG,CAAC;YAC7D,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;QACzB,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;AAxCD,sDAwCC;AAAA,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
import { RedisScopeHashCounter } from "./RedisScopeHashCounter";
|
|
4
|
+
export declare class RedisScopeTotalCount<KeyParameters, FieldParameters> extends RedisScope<null, Date> {
|
|
5
|
+
total: RedisScopeHashCounter<KeyParameters, FieldParameters>;
|
|
6
|
+
count: RedisScopeHashCounter<KeyParameters, FieldParameters>;
|
|
7
|
+
constructor(instance: RedisInstance, total: RedisScopeHashCounter<KeyParameters, FieldParameters>, count: RedisScopeHashCounter<KeyParameters, FieldParameters>);
|
|
8
|
+
key(): string;
|
|
9
|
+
increase(key_parameter: KeyParameters, field_parameter: FieldParameters, threshold: number, step: number): Promise<{
|
|
10
|
+
success: boolean;
|
|
11
|
+
total: number;
|
|
12
|
+
count: number;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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.RedisScopeTotalCount = void 0;
|
|
13
|
+
const RedisScope_1 = require("./RedisScope");
|
|
14
|
+
class RedisScopeTotalCount extends RedisScope_1.RedisScope {
|
|
15
|
+
constructor(instance, total, count) {
|
|
16
|
+
super(instance);
|
|
17
|
+
this.total = total;
|
|
18
|
+
this.count = count;
|
|
19
|
+
this.increase = this.increase.bind(this);
|
|
20
|
+
}
|
|
21
|
+
key() {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
increase(key_parameter, field_parameter, threshold, step) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
let total_key = this.total.key(key_parameter);
|
|
27
|
+
let total_field = this.total.field(field_parameter);
|
|
28
|
+
let count_key = this.count.key(key_parameter);
|
|
29
|
+
let count_field = this.count.field(field_parameter);
|
|
30
|
+
const script = `
|
|
31
|
+
local total = tonumber(redis.call("HGET", KEYS[1], ARGV[1]) or "0")
|
|
32
|
+
local count = tonumber(redis.call("HGET", KEYS[2], ARGV[2]) or "0")
|
|
33
|
+
local threshold = tonumber(ARGV[3])
|
|
34
|
+
local step = tonumber(ARGV[4])
|
|
35
|
+
|
|
36
|
+
if total + step > threshold then
|
|
37
|
+
return {0, total, count}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
total = redis.call("HINCRBY", KEYS[1], ARGV[1], step)
|
|
41
|
+
count = redis.call("HINCRBY", KEYS[2], ARGV[2], step)
|
|
42
|
+
|
|
43
|
+
return {1, total, count}
|
|
44
|
+
`;
|
|
45
|
+
let res = yield this.instance.client.eval(script, 2, total_key, count_key, total_field, count_field, threshold, step);
|
|
46
|
+
let [success, total, count] = res;
|
|
47
|
+
return { success: success == 1, total, count };
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.RedisScopeTotalCount = RedisScopeTotalCount;
|
|
52
|
+
;
|
|
53
|
+
//# sourceMappingURL=RedisScopeTotalCount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RedisScopeTotalCount.js","sourceRoot":"","sources":["../src/RedisScopeTotalCount.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAA0C;AAG1C,MAAa,oBAAqD,SAAQ,uBAAsB;IAE5F,YAAY,QAAuB,EACxB,KAA4D,EAC5D,KAA4D;QAEnE,KAAK,CAAC,QAAQ,CAAC,CAAC;QAHT,UAAK,GAAL,KAAK,CAAuD;QAC5D,UAAK,GAAL,KAAK,CAAuD;QAGnE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IACQ,GAAG;QAER,OAAO,EAAE,CAAC;IACd,CAAC;IACK,QAAQ,CAAC,aAA4B,EAAE,eAAgC,EAAE,SAAiB,EAAE,IAAY;;YAE1G,IAAI,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAC9C,IAAI,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YACpD,IAAI,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YAC9C,IAAI,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAEpD,MAAM,MAAM,GAAG;;;;;;;;;;;;;;SAcd,CAAC;YACF,IAAI,GAAG,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;YACtH,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,GAAe,CAAC;YAC9C,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACnD,CAAC;KAAA;CACJ;AAvCD,oDAuCC;AAAA,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -20,4 +20,5 @@ __exportStar(require("./RedisScopeDelay"), exports);
|
|
|
20
20
|
__exportStar(require("./RedisScopeHashCounter"), exports);
|
|
21
21
|
__exportStar(require("./RedisScopeLimit"), exports);
|
|
22
22
|
__exportStar(require("./RedisScopeLock"), exports);
|
|
23
|
+
__exportStar(require("./RedisScopeTotalCount"), exports);
|
|
23
24
|
//# 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,0DAAwC;AACxC,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;AACjC,yDAAuC"}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"framework": "npm",
|
|
9
9
|
"application": "package",
|
|
10
10
|
"private": false,
|
|
11
|
-
"version": "1.4.
|
|
11
|
+
"version": "1.4.23",
|
|
12
12
|
"author": "Amir Abolhasani",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"main": "./dist/index.js",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@types/node": "^24.1.0",
|
|
21
|
-
"ioredis": "^5.
|
|
22
|
-
"namirasoft-core": "^1.4.
|
|
21
|
+
"ioredis": "^5.7.0",
|
|
22
|
+
"namirasoft-core": "^1.4.80",
|
|
23
23
|
"namirasoft-log": "^1.4.30"
|
|
24
24
|
}
|
|
25
25
|
}
|
package/src/RedisScope.ts
CHANGED
|
@@ -12,7 +12,7 @@ export abstract class RedisScope<KeyParameters, ValueType>
|
|
|
12
12
|
this._set = this._set.bind(this);
|
|
13
13
|
this._del = this._del.bind(this);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
public abstract key(key_parameter: KeyParameters): string;
|
|
16
16
|
protected async _get(key: string, base64: boolean): Promise<ValueType | null>
|
|
17
17
|
{
|
|
18
18
|
if (process.env.NAMIRASOFT_MUTE)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RedisInstance } from "./RedisInstance";
|
|
2
2
|
import { RedisScope } from "./RedisScope";
|
|
3
3
|
|
|
4
|
-
export abstract class RedisScopeHashCounter<KeyParameters> extends RedisScope<KeyParameters, Date>
|
|
4
|
+
export abstract class RedisScopeHashCounter<KeyParameters, FieldParameters> extends RedisScope<KeyParameters | null, Date>
|
|
5
5
|
{
|
|
6
6
|
constructor(instance: RedisInstance)
|
|
7
7
|
{
|
|
@@ -11,16 +11,24 @@ export abstract class RedisScopeHashCounter<KeyParameters> extends RedisScope<Ke
|
|
|
11
11
|
this.listFields = this.listFields.bind(this);
|
|
12
12
|
this.del = this.del.bind(this);
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
async increase(key_parameter: KeyParameters,
|
|
14
|
+
public abstract field(field_parameter: FieldParameters): string;
|
|
15
|
+
async increase(key_parameter: KeyParameters, field_parameter: FieldParameters, step: number): Promise<number>
|
|
16
16
|
{
|
|
17
17
|
let key = this.key(key_parameter);
|
|
18
|
+
let field = this.field(field_parameter);
|
|
18
19
|
return await this.instance.client.hincrby(key, field, step);
|
|
19
20
|
}
|
|
21
|
+
async hget(key_parameter: KeyParameters, field_parameter: FieldParameters): Promise<number>
|
|
22
|
+
{
|
|
23
|
+
let key = this.key(key_parameter);
|
|
24
|
+
let field = this.field(field_parameter);
|
|
25
|
+
let res = await this.instance.client.hget(key, field) ?? "0";
|
|
26
|
+
return parseInt(res);
|
|
27
|
+
}
|
|
20
28
|
async listKeys(count: number = 1)
|
|
21
29
|
{
|
|
22
30
|
let cursor = "0";
|
|
23
|
-
const [_, foundKeys] = await this.instance.client.scan(cursor, "MATCH", this.
|
|
31
|
+
const [_, foundKeys] = await this.instance.client.scan(cursor, "MATCH", this.key(null), "COUNT", count);
|
|
24
32
|
return foundKeys;
|
|
25
33
|
}
|
|
26
34
|
async listFields(key_parameter: KeyParameters)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
import { RedisScopeHashCounter } from "./RedisScopeHashCounter";
|
|
4
|
+
|
|
5
|
+
export class RedisScopeTotalCount<KeyParameters, FieldParameters> extends RedisScope<null, Date>
|
|
6
|
+
{
|
|
7
|
+
constructor(instance: RedisInstance,
|
|
8
|
+
public total: RedisScopeHashCounter<KeyParameters, FieldParameters>,
|
|
9
|
+
public count: RedisScopeHashCounter<KeyParameters, FieldParameters>)
|
|
10
|
+
{
|
|
11
|
+
super(instance);
|
|
12
|
+
this.increase = this.increase.bind(this);
|
|
13
|
+
}
|
|
14
|
+
override key(): string
|
|
15
|
+
{
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
async increase(key_parameter: KeyParameters, field_parameter: FieldParameters, threshold: number, step: number): Promise<{ success: boolean, total: number, count: number }>
|
|
19
|
+
{
|
|
20
|
+
let total_key = this.total.key(key_parameter);
|
|
21
|
+
let total_field = this.total.field(field_parameter);
|
|
22
|
+
let count_key = this.count.key(key_parameter);
|
|
23
|
+
let count_field = this.count.field(field_parameter);
|
|
24
|
+
|
|
25
|
+
const script = `
|
|
26
|
+
local total = tonumber(redis.call("HGET", KEYS[1], ARGV[1]) or "0")
|
|
27
|
+
local count = tonumber(redis.call("HGET", KEYS[2], ARGV[2]) or "0")
|
|
28
|
+
local threshold = tonumber(ARGV[3])
|
|
29
|
+
local step = tonumber(ARGV[4])
|
|
30
|
+
|
|
31
|
+
if total + step > threshold then
|
|
32
|
+
return {0, total, count}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
total = redis.call("HINCRBY", KEYS[1], ARGV[1], step)
|
|
36
|
+
count = redis.call("HINCRBY", KEYS[2], ARGV[2], step)
|
|
37
|
+
|
|
38
|
+
return {1, total, count}
|
|
39
|
+
`;
|
|
40
|
+
let res = await this.instance.client.eval(script, 2, total_key, count_key, total_field, count_field, threshold, step);
|
|
41
|
+
let [success, total, count] = res as number[];
|
|
42
|
+
return { success: success == 1, total, count };
|
|
43
|
+
}
|
|
44
|
+
};
|
package/src/index.ts
CHANGED