2121 * purchase a proprietary commercial license. Please contact us at
2222 * <[email protected] > to get commercial licensing options. 2323 */
24- import { ILogger , IRedisClient , RedisCache } from '@imqueue/rpc' ;
25- import { Multi } from 'redis ' ;
24+ import { ILogger , RedisCache } from '@imqueue/rpc' ;
25+ import { Redis , ChainableCommander } from 'ioredis ' ;
2626
2727export const REDIS_INIT_ERROR = 'Redis engine is not initialized!' ;
2828
2929/**
3030 * Empty function used to ignore promises, for cases, when we do not care
31- * about results and just want to execute some routines in background
31+ * about results and just want to execute some routines in the background
3232 */
3333function ignore ( ) { /* do nothing */ }
3434
3535// noinspection JSUnusedGlobalSymbols
3636export class TagCache {
3737
3838 public logger : ILogger ;
39- public redis ?: IRedisClient ;
39+ public redis ?: Redis ;
4040 public readonly key : ( key : string ) => string ;
4141
4242 // noinspection TypeScriptUnresolvedVariable,JSUnusedGlobalSymbols
@@ -52,8 +52,8 @@ export class TagCache {
5252
5353 // noinspection JSUnusedGlobalSymbols
5454 /**
55- * Returns data stored under given keys. If a single key provided will
56- * return single result, otherwise will return an array of results
55+ * Returns data stored under given keys. If a single key provided
56+ * returns a single result, otherwise it will return an array of results
5757 * associated with the keys
5858 *
5959 * @param {string[] } keys
@@ -68,18 +68,18 @@ export class TagCache {
6868
6969 try {
7070 if ( keys . length === 1 ) {
71- const value : string = await this . redis . get (
71+ const value = await this . redis . get (
7272 this . key ( keys [ 0 ] ) ,
73- ) as any as string ;
73+ ) ;
7474
7575 return value ? JSON . parse ( value ) : null ;
7676 }
7777
78- return ( await this . redis . mget (
78+ const values = await this . redis . mget (
7979 keys . map ( key => this . key ( key ) ) ,
80- ) as any as string [ ] ) . map ( ( value : string ) =>
81- value ? JSON . parse ( value ) : null
8280 ) ;
81+
82+ return values . map ( value => value ? JSON . parse ( value ) : null ) ;
8383 } catch ( err ) {
8484 this . logger . warn ( 'TagCache: get error:' , err . stack ) ;
8585
@@ -89,14 +89,14 @@ export class TagCache {
8989
9090 // noinspection JSUnusedGlobalSymbols
9191 /**
92- * Stores given value under given kay , tagging it with the given tags
92+ * Stores given value under a given key , tagging it with the given tags
9393 *
9494 * @param {string } key - name of the key to store data under
9595 * @param {any } value - data to store in cache
9696 * @param {string[] } tags - tag strings to mark data with
9797 * @param {number } [ttl] - TTL in milliseconds
9898 */
99- public async set (
99+ public async set < T = any > (
100100 key : string ,
101101 value : any ,
102102 tags : string [ ] ,
@@ -107,7 +107,7 @@ export class TagCache {
107107 }
108108
109109 try {
110- const multi : Multi = this . redis . multi ( ) ;
110+ const multi : ChainableCommander = this . redis . multi ( ) ;
111111 const setKey = this . key ( key ) ;
112112
113113 for ( const tag of tags ) {
@@ -162,31 +162,31 @@ export class TagCache {
162162 return new Promise ( resolve => {
163163 redis . smembers (
164164 tag ,
165- ( ( err , reply ) => resolve ( reply ) ) ,
165+ ( ( _ , reply ) => resolve ( reply ) ) ,
166166 ) ;
167167 } ) ;
168168 } ) ,
169- ) as unknown as string [ ]
169+ ) as unknown as string [ ] ,
170170 ) ) ] ;
171171
172172 if ( ! keys . length ) {
173173 // nothing to do, no keys found
174174 return true ;
175175 }
176176
177- const multi : Multi = this . redis . multi ( ) ;
177+ const multi : ChainableCommander = this . redis . multi ( ) ;
178178 let cursor = '0' ;
179179
180180 multi . del ( ...keys ) ;
181181
182182 do {
183- const reply : any [ ] = ( await this . redis . scan (
183+ const reply = await this . redis . scan (
184184 cursor ,
185185 'MATCH' ,
186186 this . key ( 'tag:*' ) ,
187187 'COUNT' ,
188188 '1000' ,
189- ) ) as unknown as any [ ] ;
189+ ) ;
190190
191191 cursor = reply [ 0 ] ;
192192
@@ -195,7 +195,9 @@ export class TagCache {
195195 }
196196 } while ( cursor !== '0' ) ;
197197
198- multi . exec ( ) ;
198+ multi . exec ( ) . catch ( err => this . logger . warn (
199+ 'TagCache: invalidate error:' , err . stack ,
200+ ) ) ;
199201
200202 return true ;
201203 } catch ( err ) {
0 commit comments