1
1
import { ICacheAdapter } from './adapter.ts'
2
2
// Not in deps.ts to allow optional dep loading
3
- import {
4
- connect ,
5
- Redis ,
6
- RedisConnectOptions ,
7
- RedisValue
8
- } from 'https://deno.land/x/redis@v0.25.1/mod.ts'
3
+ import { createClient , RedisClientOptions } from 'npm:redis@4.6.13'
9
4
10
5
/** Redis Cache Adapter for using Redis as a cache-provider. */
11
6
export class RedisCacheAdapter implements ICacheAdapter {
12
- _redis : Promise < Redis >
13
- redis ?: Redis
7
+ _redis : Promise < ReturnType < typeof createClient > >
8
+ redis ?: ReturnType < typeof createClient >
14
9
ready : boolean = false
15
10
readonly _expireIntervalTimer : number = 5000
16
11
private _expireInterval ?: number
17
12
18
- constructor ( options : RedisConnectOptions ) {
19
- this . _redis = connect ( options )
13
+ constructor ( options : RedisClientOptions ) {
14
+ this . _redis = createClient ( options ) . connect ( )
20
15
this . _redis . then (
21
16
( redis ) => {
22
17
this . redis = redis
@@ -31,17 +26,17 @@ export class RedisCacheAdapter implements ICacheAdapter {
31
26
32
27
private _startExpireInterval ( ) : void {
33
28
this . _expireInterval = setInterval ( ( ) => {
34
- this . redis ?. scan ( 0 , { pattern : '*:expires' } ) . then ( ( [ _ , names ] ) => {
29
+ this . redis ?. scan ( 0 , { MATCH : '*:expires' } ) . then ( ( { keys : names } ) => {
35
30
for ( const name of names ) {
36
- this . redis ?. hvals ( name ) . then ( ( vals ) => {
31
+ this . redis ?. hVals ( name ) . then ( ( vals ) => {
37
32
for ( const val of vals ) {
38
33
const expireVal : {
39
34
name : string
40
35
key : string
41
36
at : number
42
37
} = JSON . parse ( val )
43
38
const expired = new Date ( ) . getTime ( ) > expireVal . at
44
- if ( expired ) this . redis ?. hdel ( expireVal . name , expireVal . key )
39
+ if ( expired ) this . redis ?. hDel ( expireVal . name , expireVal . key )
45
40
}
46
41
} )
47
42
}
@@ -55,7 +50,7 @@ export class RedisCacheAdapter implements ICacheAdapter {
55
50
56
51
async get < T > ( cacheName : string , key : string ) : Promise < T | undefined > {
57
52
await this . _checkReady ( )
58
- const cache = await this . redis ?. hget ( cacheName , key )
53
+ const cache = await this . redis ?. hGet ( cacheName , key )
59
54
if ( cache === undefined ) return
60
55
try {
61
56
return JSON . parse ( cache ) as T
@@ -71,15 +66,13 @@ export class RedisCacheAdapter implements ICacheAdapter {
71
66
expire ?: number
72
67
) : Promise < void > {
73
68
await this . _checkReady ( )
74
- await this . redis ?. hset (
69
+ await this . redis ?. hSet (
75
70
cacheName ,
76
71
key ,
77
- typeof value === 'object'
78
- ? JSON . stringify ( value )
79
- : ( value as unknown as RedisValue )
72
+ typeof value === 'object' ? JSON . stringify ( value ) : ( value as any )
80
73
)
81
74
if ( expire !== undefined ) {
82
- await this . redis ?. hset (
75
+ await this . redis ?. hSet (
83
76
`${ cacheName } :expires` ,
84
77
key ,
85
78
JSON . stringify ( {
@@ -93,18 +86,18 @@ export class RedisCacheAdapter implements ICacheAdapter {
93
86
94
87
async delete ( cacheName : string , ...keys : string [ ] ) : Promise < boolean > {
95
88
await this . _checkReady ( )
96
- return ( ( await this . redis ?. hdel ( cacheName , ... keys ) ) ?? 0 ) === keys . length
89
+ return ( ( await this . redis ?. hDel ( cacheName , keys ) ) ?? 0 ) === keys . length
97
90
}
98
91
99
92
async array < T > ( cacheName : string ) : Promise < T [ ] | undefined > {
100
93
await this . _checkReady ( )
101
- const data = await this . redis ?. hvals ( cacheName )
94
+ const data = await this . redis ?. hVals ( cacheName )
102
95
return data ?. map ( ( e : string ) => JSON . parse ( e ) )
103
96
}
104
97
105
98
async keys ( cacheName : string ) : Promise < string [ ] | undefined > {
106
99
await this . _checkReady ( )
107
- return this . redis ?. hkeys ( cacheName )
100
+ return this . redis ?. hKeys ( cacheName )
108
101
}
109
102
110
103
async deleteCache ( cacheName : string ) : Promise < boolean > {
@@ -114,6 +107,6 @@ export class RedisCacheAdapter implements ICacheAdapter {
114
107
115
108
async size ( cacheName : string ) : Promise < number | undefined > {
116
109
await this . _checkReady ( )
117
- return this . redis ?. hlen ( cacheName )
110
+ return this . redis ?. hLen ( cacheName )
118
111
}
119
112
}
0 commit comments