9
9
10
10
namespace Microsoft . Extensions . Caching . ServiceStackRedis
11
11
{
12
- public class ServiceStackRedisCache : IDistributedCache , IDisposable
12
+ public class ServiceStackRedisCache : IDistributedCache
13
13
{
14
- private readonly IRedisNativeClient _cache ;
14
+ private readonly IRedisClientsManager _redisManager ;
15
15
private readonly ServiceStackRedisCacheOptions _options ;
16
16
17
17
public ServiceStackRedisCache ( IOptions < ServiceStackRedisCacheOptions > optionsAccessor )
@@ -24,13 +24,7 @@ public ServiceStackRedisCache(IOptions<ServiceStackRedisCacheOptions> optionsAcc
24
24
_options = optionsAccessor . Value ;
25
25
26
26
var host = $ "{ _options . Password } @{ _options . Host } :{ _options . Port } ";
27
- var manager = new RedisManagerPool ( host ) ;
28
- var client = manager . GetClient ( ) as IRedisNativeClient ;
29
- if ( client == null )
30
- {
31
- throw new ArgumentNullException ( nameof ( client ) ) ;
32
- }
33
- _cache = client ;
27
+ _redisManager = new RedisManagerPool ( host ) ;
34
28
}
35
29
36
30
public byte [ ] Get ( string key )
@@ -40,11 +34,13 @@ public byte[] Get(string key)
40
34
throw new ArgumentNullException ( nameof ( key ) ) ;
41
35
}
42
36
43
- if ( _cache . Exists ( key ) == 1 )
37
+ using ( var client = _redisManager . GetClient ( ) as IRedisNativeClient )
44
38
{
45
- return _cache . Get ( key ) ;
39
+ if ( client . Exists ( key ) == 1 )
40
+ {
41
+ return client . Get ( key ) ;
42
+ }
46
43
}
47
-
48
44
return null ;
49
45
}
50
46
@@ -70,15 +66,18 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
70
66
throw new ArgumentNullException ( nameof ( options ) ) ;
71
67
}
72
68
73
- var expireInSeconds = GetExpireInSeconds ( options ) ;
74
- if ( expireInSeconds > 0 )
69
+ using ( var client = _redisManager . GetClient ( ) as IRedisNativeClient )
75
70
{
76
- _cache . SetEx ( key , expireInSeconds , value ) ;
77
- _cache . SetEx ( GetExpirationKey ( key ) , expireInSeconds , Encoding . UTF8 . GetBytes ( expireInSeconds . ToString ( ) ) ) ;
78
- }
79
- else
80
- {
81
- _cache . Set ( key , value ) ;
71
+ var expireInSeconds = GetExpireInSeconds ( options ) ;
72
+ if ( expireInSeconds > 0 )
73
+ {
74
+ client . SetEx ( key , expireInSeconds , value ) ;
75
+ client . SetEx ( GetExpirationKey ( key ) , expireInSeconds , Encoding . UTF8 . GetBytes ( expireInSeconds . ToString ( ) ) ) ;
76
+ }
77
+ else
78
+ {
79
+ client . Set ( key , value ) ;
80
+ }
82
81
}
83
82
}
84
83
@@ -94,15 +93,18 @@ public void Refresh(string key)
94
93
throw new ArgumentNullException ( nameof ( key ) ) ;
95
94
}
96
95
97
- if ( _cache . Exists ( key ) == 1 )
96
+ using ( var client = _redisManager . GetClient ( ) as IRedisNativeClient )
98
97
{
99
- var value = _cache . Get ( key ) ;
100
- if ( value != null )
98
+ if ( client . Exists ( key ) == 1 )
101
99
{
102
- var expirationValue = _cache . Get ( GetExpirationKey ( key ) ) ;
103
- if ( expirationValue != null )
100
+ var value = client . Get ( key ) ;
101
+ if ( value != null )
104
102
{
105
- _cache . Expire ( key , int . Parse ( Encoding . UTF8 . GetString ( expirationValue ) ) ) ;
103
+ var expirationValue = client . Get ( GetExpirationKey ( key ) ) ;
104
+ if ( expirationValue != null )
105
+ {
106
+ client . Expire ( key , int . Parse ( Encoding . UTF8 . GetString ( expirationValue ) ) ) ;
107
+ }
106
108
}
107
109
}
108
110
}
@@ -125,21 +127,16 @@ public void Remove(string key)
125
127
throw new ArgumentNullException ( nameof ( key ) ) ;
126
128
}
127
129
128
- _cache . Del ( key ) ;
130
+ using ( var client = _redisManager . GetClient ( ) as IRedisNativeClient )
131
+ {
132
+ client . Del ( key ) ;
133
+ }
129
134
}
130
135
131
136
public async Task RemoveAsync ( string key )
132
137
{
133
138
Remove ( key ) ;
134
- }
135
-
136
- public void Dispose ( )
137
- {
138
- if ( _cache != null )
139
- {
140
- _cache . Dispose ( ) ;
141
- }
142
- }
139
+ }
143
140
144
141
private int GetExpireInSeconds ( DistributedCacheEntryOptions options )
145
142
{
0 commit comments