@@ -9,11 +9,9 @@ type RedisFunctions = {
9
9
} ;
10
10
11
11
export default class RedisBalancer < T > {
12
- private _storeKey : string ;
13
12
private _data : Array < T > ;
14
- private readonly _STORE_PREFIX = 'balancer' ;
15
13
private readonly _redisClient : RedisClient ;
16
- private readonly redisPrefix : string ;
14
+ private redisPrefix : string ;
17
15
private readonly INC_VALUE = 1 ;
18
16
19
17
private readonly _functions : RedisFunctions ;
@@ -28,7 +26,6 @@ export default class RedisBalancer<T> {
28
26
this . redisPrefix = redisPrefix ;
29
27
this . _redisClient = redisClient ;
30
28
this . _data = data ;
31
- this . _storeKey = this . makeStoreKey ( data ) ;
32
29
33
30
// Initialize Redis functions as async await
34
31
this . _functions = {
@@ -41,7 +38,6 @@ export default class RedisBalancer<T> {
41
38
42
39
public setData ( data : Array < T > ) {
43
40
this . _data = data ;
44
- this . _storeKey = this . makeStoreKey ( data ) ;
45
41
}
46
42
47
43
public async increaseRank ( record : T , incValue : number = this . INC_VALUE ) {
@@ -50,7 +46,7 @@ export default class RedisBalancer<T> {
50
46
}
51
47
52
48
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 ( ) ) ;
54
50
}
55
51
56
52
public async * getAsyncIterator ( ) : AsyncIterableIterator < T > {
@@ -68,33 +64,23 @@ export default class RedisBalancer<T> {
68
64
}
69
65
70
66
public async resetStore ( ) : Promise < void > {
71
- await this . _functions . delAsync ( this . _storeKey ) ;
67
+ await this . _functions . delAsync ( this . redisPrefix ) ;
72
68
}
73
69
74
70
public getStoreKey ( ) : string {
75
- return this . _storeKey ;
71
+ return this . redisPrefix ;
76
72
}
77
73
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 ;
90
76
}
91
77
92
78
/**
93
79
* Returns an Array stored in Redis in Rank order
94
80
* @private
95
81
*/
96
82
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 > ;
98
84
// If Redis store is not initialized yield in default order
99
85
if ( storedMethodNames . length !== this . _data . length ) {
100
86
let args : Array < string > = [ ] ,
@@ -105,7 +91,7 @@ export default class RedisBalancer<T> {
105
91
args . push ( "1" , index . toString ( ) ) ;
106
92
result . push ( index . toString ( ) ) ;
107
93
} ) ;
108
- await this . _functions . zAddAsync ( this . _storeKey , 'NX' , ...args ) ;
94
+ await this . _functions . zAddAsync ( this . redisPrefix , 'NX' , ...args ) ;
109
95
110
96
return result ;
111
97
}
0 commit comments