It was not possible to connect to the redis server(s); to create a disconnec
普遍是因为没有设置abortConnect=false,在某些情况不会重连造成的 。并且开始的连接有问题。
使用redis连接类的时候一定要注意关闭和开启,主要是要注意连接的释放和管理,这个是排查问题之后修改的代码
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace CN.YHY.LIB.Common.Redis
{
public class RedisHelper
{
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionString = ConfigurationManager.AppSettings["RedisSet"];
/// <summary>
/// 锁
/// </summary>
private readonly object _lock = new object();
/// <summary>
/// 连接对象
/// </summary>
private volatile IConnectionMultiplexer _connection;
/// <summary>
/// 数据库
/// </summary>
private IDatabase _db;
public RedisHelper()
{
_connection = ConnectionMultiplexer.Connect(ConnectionString);
_db = GetDatabase();
}
/// <summary>
/// 获取连接
/// </summary>
/// <returns></returns>
protected IConnectionMultiplexer GetConnection()
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
lock (_lock)
{
if (_connection != null && _connection.IsConnected)
{
return _connection;
}
if (_connection != null)
{
_connection.Dispose();
}
_connection = ConnectionMultiplexer.Connect(ConnectionString);
}
return _connection;
}
/// <summary>
/// 获取数据库
/// </summary>
/// <param name="db"></param>
/// <returns></returns>
public IDatabase GetDatabase(int? db = null)
{
return GetConnection().GetDatabase(db ?? -1);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="key">键</param>
/// <param name="data">值</param>
/// <param name="cacheTime">时间</param>
public virtual void Set(string key, object data, int cacheTime)
{
if (data == null)
{
return;
}
var entryBytes = Serialize(data);
var expiresIn = TimeSpan.FromMinutes(cacheTime);
_db.StringSet(key, entryBytes, expiresIn);
}
/// <summary>
/// 根据键获取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual T Get<T>(string key)
{
var rValue = _db.StringGet(key);
if (!rValue.HasValue)
{
return default(T);
}
var result = Deserialize<T>(rValue);
return result;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializedObject"></param>
/// <returns></returns>
protected virtual T Deserialize<T>(byte[] serializedObject)
{
if (serializedObject == null)
{
return default(T);
}
var json = Encoding.UTF8.GetString(serializedObject);
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 判断是否已经设置
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual bool IsSet(string key)
{
return _db.KeyExists(key);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="data"></param>
/// <returns>byte[]</returns>
private byte[] Serialize(object data)
{
var json = JsonConvert.SerializeObject(data);
return Encoding.UTF8.GetBytes(json);
}
#region string类型操作
/// <summary>
/// set or update the value for string key
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetStringValue(string key, string value)
{
return _db.StringSet(key, value);
}
/// <summary>
/// 保存单个key value
/// </summary>
/// <param name="key">Redis Key</param>
/// <param name="value">保存的值</param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
{
return _db.StringSet(key, value, expiry);
}
/// <summary>
/// 保存一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <returns></returns>
public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
{
string json = JsonConvert.SerializeObject(obj);
return _db.StringSet(key, json, expiry);
}
/// <summary>
/// 获取一个key的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetStringKey<T>(string key) where T : class
{
var result = _db.StringGet(key);
if (string.IsNullOrEmpty(result))
{
return null;
}
return JsonConvert.DeserializeObject<T>(result);
}
/// <summary>
/// get the value for string key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetStringValue(string key)
{
return _db.StringGet(key);
}
/// <summary>
/// Delete the value for string key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool DeleteStringKey(string key)
{
return _db.KeyDelete(key);
}
#endregion
#region 哈希类型操作
/// <summary>
/// set or update the HashValue for string key
/// </summary>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetHashValue(string key, string hashkey, string value)
{
return _db.HashSet(key, hashkey, value);
}
/// <summary>
/// set or update the HashValue for string key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <param name="t">defined class</param>
/// <returns></returns>
public bool SetHashValue<T>(String key, string hashkey, T t) where T : class
{
var json = JsonConvert.SerializeObject(t);
return _db.HashSet(key, hashkey, json);
}
/// <summary>
/// 保存一个集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">Redis Key</param>
/// <param name="list">数据集合</param>
/// <param name="getModelId"></param>
public void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
{
List<HashEntry> listHashEntry = new List<HashEntry>();
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
listHashEntry.Add(new HashEntry(getModelId(item), json));
}
_db.HashSet(key, listHashEntry.ToArray());
}
/// <summary>
/// 获取hashkey所有的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public List<T> HashGetAll<T>(string key) where T : class
{
List<T> result = new List<T>();
HashEntry[] arr = _db.HashGetAll(key);
foreach (var item in arr)
{
if (!item.Value.IsNullOrEmpty)
{
result.Add(JsonConvert.DeserializeObject<T>(item.Value));
}
}
return result;
//result =JsonHelper.DeserializeJsonToList<T>(arr.ToString());
//return result;
}
/// <summary>
/// get the HashValue for string key and hashkey
/// </summary>
/// <param name="key">Represents a key that can be stored in redis</param>
/// <param name="hashkey"></param>
/// <returns></returns>
public RedisValue GetHashValue(string key, string hashkey)
{
RedisValue result = _db.HashGet(key, hashkey);
return result;
}
/// <summary>
/// get the HashValue for string key and hashkey
/// </summary>
/// <param name="key">Represents a key that can be stored in redis</param>
/// <param name="hashkey"></param>
/// <returns></returns>
public T GetHashValue<T>(string key, string hashkey) where T : class
{
RedisValue result = _db.HashGet(key, hashkey);
if (string.IsNullOrEmpty(result))
{
return null;
}
return JsonConvert.DeserializeObject<T>(result);
}
/// <summary>
/// delete the HashValue for string key and hashkey
/// </summary>
/// <param name="key"></param>
/// <param name="hashkey"></param>
/// <returns></returns>
public bool DeleteHashValue(string key, string hashkey)
{
return _db.HashDelete(key, hashkey);
}
#endregion
}
}