Skip to content

Commit 38eb98b

Browse files
author
Alexander Dmitrjuk
committed
add Redis prefix
1 parent 38956fc commit 38eb98b

File tree

4 files changed

+20
-45
lines changed

4 files changed

+20
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const A = () => {};
2828
const B = () => {};
2929
const C = () => {};
3030
// ... //
31-
let balancer = new RedisBalancer([A, B, C], redisClient);
31+
let balancer = new RedisBalancer([A, B, C], redisClient, 'example-redis-key');
3232
// or reuse balancer variable with another functions
3333
balancer.setData([A, B]);
3434
// ... //

index.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ class RedisBalancer {
3030
* @param redisPrefix
3131
*/
3232
constructor(data, redisClient, redisPrefix) {
33-
this._STORE_PREFIX = 'balancer';
3433
this.INC_VALUE = 1;
3534
this.redisPrefix = redisPrefix;
3635
this._redisClient = redisClient;
3736
this._data = data;
38-
this._storeKey = this.makeStoreKey(data);
3937
// Initialize Redis functions as async await
4038
this._functions = {
4139
delAsync: util_1.promisify(redisClient.DEL).bind(this._redisClient),
@@ -46,7 +44,6 @@ class RedisBalancer {
4644
}
4745
setData(data) {
4846
this._data = data;
49-
this._storeKey = this.makeStoreKey(data);
5047
}
5148
increaseRank(record, incValue = this.INC_VALUE) {
5249
return __awaiter(this, void 0, void 0, function* () {
@@ -56,7 +53,7 @@ class RedisBalancer {
5653
}
5754
increaseRankByIndex(index, incValue = this.INC_VALUE) {
5855
return __awaiter(this, void 0, void 0, function* () {
59-
yield this._functions.zIncRbyAsync(this._storeKey, incValue, index.toString());
56+
yield this._functions.zIncRbyAsync(this.redisPrefix, incValue, index.toString());
6057
});
6158
}
6259
getAsyncIterator() {
@@ -75,31 +72,22 @@ class RedisBalancer {
7572
}
7673
resetStore() {
7774
return __awaiter(this, void 0, void 0, function* () {
78-
yield this._functions.delAsync(this._storeKey);
75+
yield this._functions.delAsync(this.redisPrefix);
7976
});
8077
}
8178
getStoreKey() {
82-
return this._storeKey;
79+
return this.redisPrefix;
8380
}
84-
/**
85-
* Return redis key to store list of data with ranks
86-
* @param data
87-
* @protected
88-
*/
89-
makeStoreKey(data) {
90-
let storeKeyArray = [this._STORE_PREFIX, this.redisPrefix];
91-
data.forEach((method, index) => {
92-
storeKeyArray.push(index.toString());
93-
});
94-
return storeKeyArray.join('.');
81+
setStoreKey(key) {
82+
this.redisPrefix = key;
9583
}
9684
/**
9785
* Returns an Array stored in Redis in Rank order
9886
* @private
9987
*/
10088
getRange() {
10189
return __awaiter(this, void 0, void 0, function* () {
102-
let storedMethodNames = yield this._functions.zRangeAsync(this._storeKey, 0, -1);
90+
let storedMethodNames = yield this._functions.zRangeAsync(this.redisPrefix, 0, -1);
10391
// If Redis store is not initialized yield in default order
10492
if (storedMethodNames.length !== this._data.length) {
10593
let args = [], result = [];
@@ -108,7 +96,7 @@ class RedisBalancer {
10896
args.push("1", index.toString());
10997
result.push(index.toString());
11098
});
111-
yield this._functions.zAddAsync(this._storeKey, 'NX', ...args);
99+
yield this._functions.zAddAsync(this.redisPrefix, 'NX', ...args);
112100
return result;
113101
}
114102
return storedMethodNames;

index.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ type RedisFunctions = {
99
};
1010

1111
export default class RedisBalancer<T> {
12-
private _storeKey: string;
1312
private _data: Array<T>;
14-
private readonly _STORE_PREFIX = 'balancer';
1513
private readonly _redisClient: RedisClient;
16-
private readonly redisPrefix: string;
14+
private redisPrefix: string;
1715
private readonly INC_VALUE = 1;
1816

1917
private readonly _functions: RedisFunctions;
@@ -28,7 +26,6 @@ export default class RedisBalancer<T> {
2826
this.redisPrefix = redisPrefix;
2927
this._redisClient = redisClient;
3028
this._data = data;
31-
this._storeKey = this.makeStoreKey(data);
3229

3330
// Initialize Redis functions as async await
3431
this._functions = {
@@ -41,7 +38,6 @@ export default class RedisBalancer<T> {
4138

4239
public setData(data: Array<T>) {
4340
this._data = data;
44-
this._storeKey = this.makeStoreKey(data);
4541
}
4642

4743
public async increaseRank(record: T, incValue: number = this.INC_VALUE) {
@@ -50,7 +46,7 @@ export default class RedisBalancer<T> {
5046
}
5147

5248
protected async increaseRankByIndex(index: number, incValue: number = this.INC_VALUE) {
53-
await this._functions.zIncRbyAsync(this._storeKey, incValue, index.toString());
49+
await this._functions.zIncRbyAsync(this.redisPrefix, incValue, index.toString());
5450
}
5551

5652
public async* getAsyncIterator(): AsyncIterableIterator<T> {
@@ -68,33 +64,23 @@ export default class RedisBalancer<T> {
6864
}
6965

7066
public async resetStore(): Promise<void> {
71-
await this._functions.delAsync(this._storeKey);
67+
await this._functions.delAsync(this.redisPrefix);
7268
}
7369

7470
public getStoreKey(): string {
75-
return this._storeKey;
71+
return this.redisPrefix;
7672
}
7773

78-
/**
79-
* Return redis key to store list of data with ranks
80-
* @param data
81-
* @protected
82-
*/
83-
protected makeStoreKey(data: Array<T>): string {
84-
let storeKeyArray: Array<string> = [this._STORE_PREFIX, this.redisPrefix];
85-
data.forEach((method: T, index: number) => {
86-
storeKeyArray.push(index.toString());
87-
});
88-
89-
return storeKeyArray.join('.');
74+
public setStoreKey(key: string): void {
75+
this.redisPrefix = key;
9076
}
9177

9278
/**
9379
* Returns an Array stored in Redis in Rank order
9480
* @private
9581
*/
9682
protected async getRange(): Promise<Array<string>> {
97-
let storedMethodNames = await this._functions.zRangeAsync(this._storeKey, 0, -1) as Array<string>;
83+
let storedMethodNames = await this._functions.zRangeAsync(this.redisPrefix, 0, -1) as Array<string>;
9884
// If Redis store is not initialized yield in default order
9985
if (storedMethodNames.length !== this._data.length) {
10086
let args: Array<string> = [],
@@ -105,7 +91,7 @@ export default class RedisBalancer<T> {
10591
args.push("1", index.toString());
10692
result.push(index.toString());
10793
});
108-
await this._functions.zAddAsync(this._storeKey, 'NX', ...args);
94+
await this._functions.zAddAsync(this.redisPrefix, 'NX', ...args);
10995

11096
return result;
11197
}

test/iterator.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ let balancer: RedisBalancer<Function>;
1515
let zRangeAsync = promisify(redisClient.zrange).bind(redisClient);
1616
describe('Test Callable Balancer', async function () {
1717
beforeEach(async () => {
18-
balancer = new RedisBalancer(methods, redisClient)
18+
balancer = new RedisBalancer(methods, redisClient, 'example');
1919
await balancer.resetStore();
2020
});
2121

2222
it('check store key generated', async () => {
23-
assert.strictEqual('balancer.0.1.2', balancer.getStoreKey());
23+
assert.strictEqual('example', balancer.getStoreKey());
2424
balancer.setData([C, B, A]);
25-
assert.strictEqual('balancer.0.1.2', balancer.getStoreKey());
25+
balancer.setStoreKey('example2');
26+
assert.strictEqual('example2', balancer.getStoreKey());
2627
});
2728

2829
it('check iterator first run in default order', async () => {

0 commit comments

Comments
 (0)