@@ -15,7 +15,6 @@ export class MeasurableCache implements ICacheClient {
1515
1616 public static readonly methods = {
1717 GET : 'get' ,
18- GET_ASYNC : 'getAsync' ,
1918 SET : 'set' ,
2019 DELETE : 'delete' ,
2120 MSET : 'mSet' ,
@@ -26,18 +25,11 @@ export class MeasurableCache implements ICacheClient {
2625 } ;
2726
2827 private cacheType : string ;
29- private callMap : Map < string , string [ ] > ;
30-
31- public constructor (
32- decorated : ICacheClient ,
33- cacheMethodsCounter : Counter ,
34- cacheType : string ,
35- callMap : Map < string , string [ ] > ,
36- ) {
28+
29+ public constructor ( decorated : ICacheClient , cacheMethodsCounter : Counter , cacheType : string ) {
3730 this . decorated = decorated ;
3831 this . cacheMethodsCounter = cacheMethodsCounter ;
3932 this . cacheType = cacheType ;
40- this . callMap = callMap ;
4133 }
4234
4335 /**
@@ -62,7 +54,7 @@ export class MeasurableCache implements ICacheClient {
6254 * @returns The cached value if found, otherwise null.
6355 */
6456 public async get ( key : string , callingMethod : string ) : Promise < any > {
65- this . count ( callingMethod , MeasurableCache . methods . GET_ASYNC ) ;
57+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . GET ) . inc ( 1 ) ;
6658 return await this . decorated . get ( key , callingMethod ) ;
6759 }
6860
@@ -76,7 +68,7 @@ export class MeasurableCache implements ICacheClient {
7668 * @param ttl - Time to live for the cached value in milliseconds (optional).
7769 */
7870 public async set ( key : string , value : any , callingMethod : string , ttl ?: number ) : Promise < void > {
79- this . count ( callingMethod , MeasurableCache . methods . SET ) ;
71+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . SET ) . inc ( 1 ) ;
8072 return await this . decorated . set ( key , value , callingMethod , ttl ) ;
8173 }
8274
@@ -90,8 +82,8 @@ export class MeasurableCache implements ICacheClient {
9082 * @returns A Promise that resolves when the values are cached.
9183 */
9284 public async multiSet ( keyValuePairs : Record < string , any > , callingMethod : string , ttl ?: number ) : Promise < void > {
85+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . MSET ) . inc ( 1 ) ;
9386 await this . decorated . multiSet ( keyValuePairs , callingMethod , ttl ) ;
94- this . count ( callingMethod , MeasurableCache . methods . MSET ) ;
9587 }
9688
9789 /**
@@ -104,8 +96,8 @@ export class MeasurableCache implements ICacheClient {
10496 * @returns A Promise that resolves when the values are cached.
10597 */
10698 public async pipelineSet ( keyValuePairs : Record < string , any > , callingMethod : string , ttl ?: number ) : Promise < void > {
99+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . PIPELINE ) . inc ( 1 ) ;
107100 await this . decorated . pipelineSet ( keyValuePairs , callingMethod , ttl ) ;
108- this . count ( callingMethod , MeasurableCache . methods . PIPELINE ) ;
109101 }
110102
111103 /**
@@ -116,7 +108,7 @@ export class MeasurableCache implements ICacheClient {
116108 * @param callingMethod - The name of the method calling the cache.
117109 */
118110 public async delete ( key : string , callingMethod : string ) : Promise < void > {
119- this . count ( callingMethod , MeasurableCache . methods . DELETE ) ;
111+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . DELETE ) . inc ( 1 ) ;
120112 await this . decorated . delete ( key , callingMethod ) ;
121113 }
122114
@@ -147,7 +139,7 @@ export class MeasurableCache implements ICacheClient {
147139 * @returns The value of the key after incrementing
148140 */
149141 public async incrBy ( key : string , amount : number , callingMethod : string ) : Promise < number > {
150- this . count ( callingMethod , MeasurableCache . methods . INCR_BY ) ;
142+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . INCR_BY ) . inc ( 1 ) ;
151143 return await this . decorated . incrBy ( key , amount , callingMethod ) ;
152144 }
153145
@@ -162,7 +154,7 @@ export class MeasurableCache implements ICacheClient {
162154 * @returns The list of elements in the range
163155 */
164156 public async lRange ( key : string , start : number , end : number , callingMethod : string ) : Promise < any [ ] > {
165- this . count ( callingMethod , MeasurableCache . methods . LRANGE ) ;
157+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . LRANGE ) . inc ( 1 ) ;
166158 return await this . decorated . lRange ( key , start , end , callingMethod ) ;
167159 }
168160
@@ -176,26 +168,7 @@ export class MeasurableCache implements ICacheClient {
176168 * @returns The length of the list after pushing
177169 */
178170 public async rPush ( key : string , value : any , callingMethod : string ) : Promise < number > {
179- this . count ( callingMethod , MeasurableCache . methods . RPUSH ) ;
171+ this . cacheMethodsCounter . labels ( callingMethod , this . cacheType , MeasurableCache . methods . RPUSH ) . inc ( 1 ) ;
180172 return await this . decorated . rPush ( key , value , callingMethod ) ;
181173 }
182-
183- /**
184- * Counts the number of occurrences of the given caching related operation.
185- * Depending on the underlying client implementation, the actual caching behavior may vary.
186- * The `callMap` allows us to account for these differences when counting occurrences.
187- *
188- * For example, if the underlying cache mechanism (such as LRU) does not provide an lRange method,
189- * we can implement it ourselves by using get and set instead. We want to count each lRange call
190- * as corresponding get and set calls then.
191- *
192- * @param caller The name of the calling method
193- * @param callee Actual caching operation
194- * @private
195- */
196- private count ( caller : string , callee : string ) : void {
197- ( this . callMap . get ( callee ) || [ callee ] ) . forEach ( ( value : string ) =>
198- this . cacheMethodsCounter . labels ( caller , this . cacheType , value ) . inc ( 1 ) ,
199- ) ;
200- }
201174}
0 commit comments