Skip to content

Commit

Permalink
add Redis prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryuk committed Jan 19, 2021
1 parent 38956fc commit 38eb98b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const A = () => {};
const B = () => {};
const C = () => {};
// ... //
let balancer = new RedisBalancer([A, B, C], redisClient);
let balancer = new RedisBalancer([A, B, C], redisClient, 'example-redis-key');
// or reuse balancer variable with another functions
balancer.setData([A, B]);
// ... //
Expand Down
26 changes: 7 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ class RedisBalancer {
* @param redisPrefix
*/
constructor(data, redisClient, redisPrefix) {
this._STORE_PREFIX = 'balancer';
this.INC_VALUE = 1;
this.redisPrefix = redisPrefix;
this._redisClient = redisClient;
this._data = data;
this._storeKey = this.makeStoreKey(data);
// Initialize Redis functions as async await
this._functions = {
delAsync: util_1.promisify(redisClient.DEL).bind(this._redisClient),
Expand All @@ -46,7 +44,6 @@ class RedisBalancer {
}
setData(data) {
this._data = data;
this._storeKey = this.makeStoreKey(data);
}
increaseRank(record, incValue = this.INC_VALUE) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -56,7 +53,7 @@ class RedisBalancer {
}
increaseRankByIndex(index, incValue = this.INC_VALUE) {
return __awaiter(this, void 0, void 0, function* () {
yield this._functions.zIncRbyAsync(this._storeKey, incValue, index.toString());
yield this._functions.zIncRbyAsync(this.redisPrefix, incValue, index.toString());
});
}
getAsyncIterator() {
Expand All @@ -75,31 +72,22 @@ class RedisBalancer {
}
resetStore() {
return __awaiter(this, void 0, void 0, function* () {
yield this._functions.delAsync(this._storeKey);
yield this._functions.delAsync(this.redisPrefix);
});
}
getStoreKey() {
return this._storeKey;
return this.redisPrefix;
}
/**
* Return redis key to store list of data with ranks
* @param data
* @protected
*/
makeStoreKey(data) {
let storeKeyArray = [this._STORE_PREFIX, this.redisPrefix];
data.forEach((method, index) => {
storeKeyArray.push(index.toString());
});
return storeKeyArray.join('.');
setStoreKey(key) {
this.redisPrefix = key;
}
/**
* Returns an Array stored in Redis in Rank order
* @private
*/
getRange() {
return __awaiter(this, void 0, void 0, function* () {
let storedMethodNames = yield this._functions.zRangeAsync(this._storeKey, 0, -1);
let storedMethodNames = yield this._functions.zRangeAsync(this.redisPrefix, 0, -1);
// If Redis store is not initialized yield in default order
if (storedMethodNames.length !== this._data.length) {
let args = [], result = [];
Expand All @@ -108,7 +96,7 @@ class RedisBalancer {
args.push("1", index.toString());
result.push(index.toString());
});
yield this._functions.zAddAsync(this._storeKey, 'NX', ...args);
yield this._functions.zAddAsync(this.redisPrefix, 'NX', ...args);
return result;
}
return storedMethodNames;
Expand Down
30 changes: 8 additions & 22 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ type RedisFunctions = {
};

export default class RedisBalancer<T> {
private _storeKey: string;
private _data: Array<T>;
private readonly _STORE_PREFIX = 'balancer';
private readonly _redisClient: RedisClient;
private readonly redisPrefix: string;
private redisPrefix: string;
private readonly INC_VALUE = 1;

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

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

public setData(data: Array<T>) {
this._data = data;
this._storeKey = this.makeStoreKey(data);
}

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

protected async increaseRankByIndex(index: number, incValue: number = this.INC_VALUE) {
await this._functions.zIncRbyAsync(this._storeKey, incValue, index.toString());
await this._functions.zIncRbyAsync(this.redisPrefix, incValue, index.toString());
}

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

public async resetStore(): Promise<void> {
await this._functions.delAsync(this._storeKey);
await this._functions.delAsync(this.redisPrefix);
}

public getStoreKey(): string {
return this._storeKey;
return this.redisPrefix;
}

/**
* Return redis key to store list of data with ranks
* @param data
* @protected
*/
protected makeStoreKey(data: Array<T>): string {
let storeKeyArray: Array<string> = [this._STORE_PREFIX, this.redisPrefix];
data.forEach((method: T, index: number) => {
storeKeyArray.push(index.toString());
});

return storeKeyArray.join('.');
public setStoreKey(key: string): void {
this.redisPrefix = key;
}

/**
* Returns an Array stored in Redis in Rank order
* @private
*/
protected async getRange(): Promise<Array<string>> {
let storedMethodNames = await this._functions.zRangeAsync(this._storeKey, 0, -1) as Array<string>;
let storedMethodNames = await this._functions.zRangeAsync(this.redisPrefix, 0, -1) as Array<string>;
// If Redis store is not initialized yield in default order
if (storedMethodNames.length !== this._data.length) {
let args: Array<string> = [],
Expand All @@ -105,7 +91,7 @@ export default class RedisBalancer<T> {
args.push("1", index.toString());
result.push(index.toString());
});
await this._functions.zAddAsync(this._storeKey, 'NX', ...args);
await this._functions.zAddAsync(this.redisPrefix, 'NX', ...args);

return result;
}
Expand Down
7 changes: 4 additions & 3 deletions test/iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ let balancer: RedisBalancer<Function>;
let zRangeAsync = promisify(redisClient.zrange).bind(redisClient);
describe('Test Callable Balancer', async function () {
beforeEach(async () => {
balancer = new RedisBalancer(methods, redisClient)
balancer = new RedisBalancer(methods, redisClient, 'example');
await balancer.resetStore();
});

it('check store key generated', async () => {
assert.strictEqual('balancer.0.1.2', balancer.getStoreKey());
assert.strictEqual('example', balancer.getStoreKey());
balancer.setData([C, B, A]);
assert.strictEqual('balancer.0.1.2', balancer.getStoreKey());
balancer.setStoreKey('example2');
assert.strictEqual('example2', balancer.getStoreKey());
});

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

0 comments on commit 38eb98b

Please sign in to comment.