我正在使用Azure Redis缓存进行开发,并想验证我处理异常的方式。根据最佳实践,有可能面对RedisConnectionExceptions并解决此问题,我们必须处置旧的ConnectionMultiplexer并创建一个新的ConnectionMultiplexer。如果abortConnect设置为false,那么多路复用器将在不引发错误的情况下静默重试连接。因此,如果引发异常,则只有在尝试重新连接后仍会失败。我对此的理解正确吗? 这是我的连接字符串-
cachename.redis.cache.windows.net:6380,password=Password,ssl=True,abortConnect=False
我相信仅当您尝试在多路复用器上调用GetConnection()时,连接异常才会发生。在下面找到我的代码-
static Lazy<ConnectionMultiplexer> multiplexer = CreateMultiplexer();
public static ConnectionMultiplexer GetConnection() => multiplexer.Value;
private static Lazy<ConnectionMultiplexer> CreateMultiplexer()
{
return new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString));
}
private static void CloseMultiplexer(Lazy<ConnectionMultiplexer> oldMultiplexer)
{
if (oldMultiplexer != null)
{
oldMultiplexer.Value.Close();
}
}
public static void Reconnect()
{
var oldMultiplexer = multiplexer;
CloseMultiplexer(multiplexer);
multiplexer = CreateMultiplexer();
}
我正在下面的另一堂课中使用它-
public class RedisCacheManager
{
private static IDatabase _cache;
private TimeSpan expiry = new TimeSpan(hours: 6, minutes: 0, seconds: 0);
public RedisCacheManager()
{
try
{
_cache = RedisCacheHelper.GetConnection().GetDatabase();
}
catch(RedisConnectionException)
{
RedisCacheHelper.Reconnect();
new RedisCacheManager();
}
}
public async Task<RedisValue[]> GetFromCacheAsync(List<string> keys)
{
var cacheValues = await _cache.StringGetAsync(keys.Select(k => (RedisKey)k).ToArray());
return cacheValues;
}
public async Task SaveInCacheAsync<TValue>(Dictionary<string, TValue> kvps)
{
var tasks = new List<Task>();
foreach(var kvp in kvps)
{
tasks.Add(_cache.StringSetAsync(kvp.Key, JsonConvert.SerializeObject(kvp), expiry));
}
await Task.WhenAll(tasks);
}
}
我不确定id在catch块中调用构造函数是否是一个好习惯。还有在调用StringGetAsync和StringSetAsync时需要处理的其他任何异常吗?
答案 0 :(得分:0)
CacheManager如下所示:
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public sealed class RedisCacheManager : IDisposable
{
private readonly TimeSpan _expiry;
private readonly Lazy<ConnectionMultiplexer> _lazyConnection;
private ConnectionMultiplexer Connection { get => _lazyConnection.Value; }
public RedisCacheManager(string connectionString, TimeSpan expiry)
{
_expiry = expiry;
_lazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString));
}
public async Task<RedisValue[]> GetFromCacheAsync(IEnumerable<string> keys)
{
var cacheValues = await Connection.GetDatabase()
.StringGetAsync(keys.Select(key => (RedisKey)key).ToArray()).ConfigureAwait(false);
return cacheValues;
}
public async Task SaveInCacheAsync<TValue>(Dictionary<string, TValue> kvps)
{
var tasks = kvps
.Select(kvp => Connection.GetDatabase().StringSetAsync(kvp.Key, JsonConvert.SerializeObject(kvp), _expiry))
.ToArray();
await Task.WhenAll(tasks).ConfigureAwait(false);
}
public void Dispose()
{
if (_lazyConnection.IsValueCreated)
{
_lazyConnection.Value.Dispose();
}
}
}
使用:
public readonly static RedisCacheManager RedisCacheManager = new RedisCacheManager("connection string", TimeSpan.FromHours(6));
备注:
旨在 abortConnect = false (这意味着即使未建立与Azure Redis缓存的连接,调用也会成功),并且不应从构造函数中抛出任何异常Redis例外
从GetDatabase返回的对象是cheap pass-thru object,不需要存储。
GetFromCacheAsync / SaveInCacheAsync 方法可以向外部抛出异常,这是可以的。您可以应用Retry-policy来解决瞬态故障。
如果您有任何IoC容器,则应使用单个实例范围(例如Autofac registration)创建 RedisCacheManager