Skip to content

Commit cf78243

Browse files
committed
Fix "Unknown reply on integer response"
1 parent cb96e99 commit cf78243

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

src/Microsoft.Extensions.Caching.ServiceStackRedis/ServiceStackRedisCache.cs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Microsoft.Extensions.Caching.ServiceStackRedis
1111
{
12-
public class ServiceStackRedisCache : IDistributedCache, IDisposable
12+
public class ServiceStackRedisCache : IDistributedCache
1313
{
14-
private readonly IRedisNativeClient _cache;
14+
private readonly IRedisClientsManager _redisManager;
1515
private readonly ServiceStackRedisCacheOptions _options;
1616

1717
public ServiceStackRedisCache(IOptions<ServiceStackRedisCacheOptions> optionsAccessor)
@@ -24,13 +24,7 @@ public ServiceStackRedisCache(IOptions<ServiceStackRedisCacheOptions> optionsAcc
2424
_options = optionsAccessor.Value;
2525

2626
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);
3428
}
3529

3630
public byte[] Get(string key)
@@ -40,11 +34,13 @@ public byte[] Get(string key)
4034
throw new ArgumentNullException(nameof(key));
4135
}
4236

43-
if (_cache.Exists(key) == 1)
37+
using (var client = _redisManager.GetClient() as IRedisNativeClient)
4438
{
45-
return _cache.Get(key);
39+
if (client.Exists(key) == 1)
40+
{
41+
return client.Get(key);
42+
}
4643
}
47-
4844
return null;
4945
}
5046

@@ -70,15 +66,18 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
7066
throw new ArgumentNullException(nameof(options));
7167
}
7268

73-
var expireInSeconds = GetExpireInSeconds(options);
74-
if (expireInSeconds > 0)
69+
using (var client = _redisManager.GetClient() as IRedisNativeClient)
7570
{
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+
}
8281
}
8382
}
8483

@@ -94,15 +93,18 @@ public void Refresh(string key)
9493
throw new ArgumentNullException(nameof(key));
9594
}
9695

97-
if (_cache.Exists(key) == 1)
96+
using (var client = _redisManager.GetClient() as IRedisNativeClient)
9897
{
99-
var value = _cache.Get(key);
100-
if (value != null)
98+
if (client.Exists(key) == 1)
10199
{
102-
var expirationValue = _cache.Get(GetExpirationKey(key));
103-
if (expirationValue != null)
100+
var value = client.Get(key);
101+
if (value != null)
104102
{
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+
}
106108
}
107109
}
108110
}
@@ -125,21 +127,16 @@ public void Remove(string key)
125127
throw new ArgumentNullException(nameof(key));
126128
}
127129

128-
_cache.Del(key);
130+
using (var client = _redisManager.GetClient() as IRedisNativeClient)
131+
{
132+
client.Del(key);
133+
}
129134
}
130135

131136
public async Task RemoveAsync(string key)
132137
{
133138
Remove(key);
134-
}
135-
136-
public void Dispose()
137-
{
138-
if (_cache != null)
139-
{
140-
_cache.Dispose();
141-
}
142-
}
139+
}
143140

144141
private int GetExpireInSeconds(DistributedCacheEntryOptions options)
145142
{

src/Microsoft.Extensions.Caching.ServiceStackRedis/ServiceStackRedisCacheServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static IServiceCollection AddDistributedServiceStackRedisCache(this IServ
2323

2424
services.AddOptions();
2525
services.Configure(setupAction);
26-
services.Add(ServiceDescriptor.Transient<IDistributedCache, ServiceStackRedisCache>());
26+
services.Add(ServiceDescriptor.Singleton<IDistributedCache, ServiceStackRedisCache>());
2727

2828
return services;
2929
}

src/Microsoft.Extensions.Caching.ServiceStackRedis/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0.1",
2+
"version": "1.0.0.2",
33
"name": "ServiceStackRedisCache",
44
"description": "Distributed cache implementation of Microsoft.Extensions.Caching.Distributed.IDistributedCache using ServiceStack.Redis.Core",
55
"projectUrl": "https://github.com/cnblogs/ServiceStackRedisCache",

0 commit comments

Comments
 (0)