namirasoft-node-redis 1.4.33 → 1.4.35
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/RedisScopeShardCoordinator.d.ts +13 -0
- package/dist/RedisScopeShardCoordinator.js +44 -0
- package/dist/RedisScopeShardCoordinator.js.map +1 -0
- package/dist/RedisScopeUniqueness.d.ts +8 -0
- package/dist/RedisScopeUniqueness.js +38 -0
- package/dist/RedisScopeUniqueness.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 +6 -6
- package/src/RedisScopeShardCoordinator.ts +39 -0
- package/src/RedisScopeUniqueness.ts +25 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
export declare abstract class RedisScopeShardCoordinator extends RedisScope<void, number> {
|
|
4
|
+
private ttl_seconds;
|
|
5
|
+
protected id: string;
|
|
6
|
+
constructor(instance: RedisInstance, ttl_seconds: number);
|
|
7
|
+
heartbeat(ttl_seconds?: number): Promise<void>;
|
|
8
|
+
get(ttl_seconds?: number): Promise<{
|
|
9
|
+
id: string;
|
|
10
|
+
index: number;
|
|
11
|
+
total: number;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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.RedisScopeShardCoordinator = void 0;
|
|
13
|
+
const namirasoft_core_1 = require("namirasoft-core");
|
|
14
|
+
const RedisScope_1 = require("./RedisScope");
|
|
15
|
+
class RedisScopeShardCoordinator extends RedisScope_1.RedisScope {
|
|
16
|
+
constructor(instance, ttl_seconds) {
|
|
17
|
+
super(instance);
|
|
18
|
+
this.ttl_seconds = ttl_seconds;
|
|
19
|
+
this.id = `${new Date().getTime()}-${namirasoft_core_1.BaseUUID.uuid(5)}`;
|
|
20
|
+
}
|
|
21
|
+
heartbeat(ttl_seconds) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const ttl = ttl_seconds !== null && ttl_seconds !== void 0 ? ttl_seconds : this.ttl_seconds;
|
|
24
|
+
const key = this.key();
|
|
25
|
+
const key_instance = `${key}:${this.id}`;
|
|
26
|
+
yield this.instance.client.set(key_instance, "1", "EX", ttl);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
get(ttl_seconds) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
yield this.heartbeat(ttl_seconds);
|
|
32
|
+
const key = this.key();
|
|
33
|
+
const pattern = `${key}:*`;
|
|
34
|
+
const keys = yield this.instance.client.keys(pattern);
|
|
35
|
+
keys.sort();
|
|
36
|
+
const index = keys.indexOf(`${key}:${this.id}`);
|
|
37
|
+
const total = keys.length;
|
|
38
|
+
return { id: this.id, index, total };
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.RedisScopeShardCoordinator = RedisScopeShardCoordinator;
|
|
43
|
+
;
|
|
44
|
+
//# sourceMappingURL=RedisScopeShardCoordinator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"R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MAAM,CAAC;YAE1B,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACzC,CAAC;KAAA;CACJ;AAlCD,gEAkCC;AAAA,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
export declare abstract class RedisScopeUniqueness<KeyParameters> extends RedisScope<KeyParameters, number> {
|
|
4
|
+
private ttl_seconds;
|
|
5
|
+
constructor(instance: RedisInstance, ttl_seconds: number);
|
|
6
|
+
set(key_parameter: KeyParameters, ttl_seconds?: number): Promise<boolean>;
|
|
7
|
+
exists(key_parameter: KeyParameters): Promise<boolean>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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.RedisScopeUniqueness = void 0;
|
|
13
|
+
const RedisScope_1 = require("./RedisScope");
|
|
14
|
+
class RedisScopeUniqueness extends RedisScope_1.RedisScope {
|
|
15
|
+
constructor(instance, ttl_seconds) {
|
|
16
|
+
super(instance);
|
|
17
|
+
this.ttl_seconds = ttl_seconds;
|
|
18
|
+
this.set = this.set.bind(this);
|
|
19
|
+
this.exists = this.exists.bind(this);
|
|
20
|
+
}
|
|
21
|
+
set(key_parameter, ttl_seconds) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const ttl = ttl_seconds !== null && ttl_seconds !== void 0 ? ttl_seconds : this.ttl_seconds;
|
|
24
|
+
const ans = yield this._set(this.key(key_parameter), Math.random(), true, ttl, true);
|
|
25
|
+
return ans === "OK";
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exists(key_parameter) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
const key = this.key(key_parameter);
|
|
31
|
+
const exists = yield this.instance.client.exists(key);
|
|
32
|
+
return exists != 0;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.RedisScopeUniqueness = RedisScopeUniqueness;
|
|
37
|
+
;
|
|
38
|
+
//# sourceMappingURL=RedisScopeUniqueness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RedisScopeUniqueness.js","sourceRoot":"","sources":["../src/RedisScopeUniqueness.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAA0C;AAE1C,MAAsB,oBAAoC,SAAQ,uBAAiC;IAE/F,YAAY,QAAuB,EAAU,WAAmB;QAE5D,KAAK,CAAC,QAAQ,CAAC,CAAC;QAFyB,gBAAW,GAAX,WAAW,CAAQ;QAG5D,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IACK,GAAG,CAAC,aAA4B,EAAE,WAAoB;;YAExD,MAAM,GAAG,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,IAAI,CAAC,WAAW,CAAC;YAC5C,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;YACrF,OAAO,GAAG,KAAK,IAAI,CAAC;QACxB,CAAC;KAAA;IACK,MAAM,CAAC,aAA4B;;YAErC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;YACpC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAEtD,OAAO,MAAM,IAAI,CAAC,CAAC;QACvB,CAAC;KAAA;CACJ;AArBD,oDAqBC;AAAA,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -20,5 +20,6 @@ __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("./RedisScopeShardCoordinator"), exports);
|
|
23
24
|
__exportStar(require("./RedisScopeTotalCount"), exports);
|
|
24
25
|
//# 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;AACjC,yDAAuC"}
|
|
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,+DAA6C;AAC7C,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.35",
|
|
12
12
|
"author": "Amir Abolhasani",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"main": "./dist/index.js",
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
"build": ""
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@types/node": "^24.
|
|
21
|
-
"ioredis": "^5.
|
|
22
|
-
"namirasoft-core": "^1.4.
|
|
23
|
-
"namirasoft-log": "^1.4.
|
|
24
|
-
"namirasoft-node": "^1.4.
|
|
20
|
+
"@types/node": "^24.7.2",
|
|
21
|
+
"ioredis": "^5.8.1",
|
|
22
|
+
"namirasoft-core": "^1.4.98",
|
|
23
|
+
"namirasoft-log": "^1.4.37",
|
|
24
|
+
"namirasoft-node": "^1.4.128"
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BaseUUID } from "namirasoft-core";
|
|
2
|
+
import { RedisInstance } from "./RedisInstance";
|
|
3
|
+
import { RedisScope } from "./RedisScope";
|
|
4
|
+
|
|
5
|
+
export abstract class RedisScopeShardCoordinator extends RedisScope<void, number>
|
|
6
|
+
{
|
|
7
|
+
protected id: string;
|
|
8
|
+
constructor(instance: RedisInstance, private ttl_seconds: number)
|
|
9
|
+
{
|
|
10
|
+
super(instance);
|
|
11
|
+
this.id = `${new Date().getTime()}-${BaseUUID.uuid(5)}`;
|
|
12
|
+
}
|
|
13
|
+
async heartbeat(ttl_seconds?: number): Promise<void>
|
|
14
|
+
{
|
|
15
|
+
const ttl = ttl_seconds ?? this.ttl_seconds;
|
|
16
|
+
const key = this.key();
|
|
17
|
+
const key_instance = `${key}:${this.id}`;
|
|
18
|
+
await this.instance.client.set(key_instance, "1", "EX", ttl);
|
|
19
|
+
}
|
|
20
|
+
async get(ttl_seconds?: number): Promise<{
|
|
21
|
+
id: string,
|
|
22
|
+
index: number,
|
|
23
|
+
total: number
|
|
24
|
+
}>
|
|
25
|
+
{
|
|
26
|
+
await this.heartbeat(ttl_seconds);
|
|
27
|
+
|
|
28
|
+
const key = this.key();
|
|
29
|
+
const pattern = `${key}:*`;
|
|
30
|
+
|
|
31
|
+
const keys = await this.instance.client.keys(pattern);
|
|
32
|
+
keys.sort();
|
|
33
|
+
|
|
34
|
+
const index = keys.indexOf(`${key}:${this.id}`);
|
|
35
|
+
const total = keys.length;
|
|
36
|
+
|
|
37
|
+
return { id: this.id, index, total };
|
|
38
|
+
}
|
|
39
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { RedisInstance } from "./RedisInstance";
|
|
2
|
+
import { RedisScope } from "./RedisScope";
|
|
3
|
+
|
|
4
|
+
export abstract class RedisScopeUniqueness<KeyParameters> extends RedisScope<KeyParameters, number>
|
|
5
|
+
{
|
|
6
|
+
constructor(instance: RedisInstance, private ttl_seconds: number)
|
|
7
|
+
{
|
|
8
|
+
super(instance);
|
|
9
|
+
this.set = this.set.bind(this);
|
|
10
|
+
this.exists = this.exists.bind(this);
|
|
11
|
+
}
|
|
12
|
+
async set(key_parameter: KeyParameters, ttl_seconds?: number): Promise<boolean>
|
|
13
|
+
{
|
|
14
|
+
const ttl = ttl_seconds ?? this.ttl_seconds;
|
|
15
|
+
const ans = await this._set(this.key(key_parameter), Math.random(), true, ttl, true);
|
|
16
|
+
return ans === "OK";
|
|
17
|
+
}
|
|
18
|
+
async exists(key_parameter: KeyParameters): Promise<boolean>
|
|
19
|
+
{
|
|
20
|
+
const key = this.key(key_parameter);
|
|
21
|
+
const exists = await this.instance.client.exists(key);
|
|
22
|
+
|
|
23
|
+
return exists != 0;
|
|
24
|
+
}
|
|
25
|
+
};
|