@@ -9,11 +9,9 @@ type RedisFunctions = {
99} ;
1010
1111export 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 }
0 commit comments