using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RedisCommon
{
public static class RedisConnectionHelp
{
/// <summary>
/// 系统自定义Key前缀
/// </summary>
public static readonly string SysCustomKey = System.Configuration.ConfigurationManager.AppSettings["RedisKey"] ?? "";
/// <summary>
/// 连接字符串
/// </summary>
private static readonly string RedisConnectionString = System.Configuration.ConfigurationManager.AppSettings["RedisHosts"];//?? "127.0.0.1:6379";
private static readonly object Locker = new object();
private static ConnectionMultiplexer _instance;
/// <summary>
/// 线程安全的字典
/// </summary>
private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
/// <summary>
/// 单例获取
/// </summary>
public static ConnectionMultiplexer Instance
{
get
{
if (_instance == null)
{
lock (Locker)
{
if (_instance == null || !_instance.IsConnected)
{
_instance = GetManager();
}
}
}
return _instance;
}
}
/// <summary>
/// 缓存获取
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
{
if (!ConnectionCache.ContainsKey(connectionString))
{
ConnectionCache[connectionString] = GetManager(connectionString);
}
return ConnectionCache[connectionString];
}
/// <summary>
/// 获取连接
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
private static ConnectionMultiplexer GetManager(string connectionString = null)
{
connectionString = connectionString ?? RedisConnectionString;
if (string.IsNullOrWhiteSpace(connectionString))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("配置文件沒有配置連接字符串,請參考:");
sb.AppendLine("<appSettings>");
sb.AppendLine(" <add key=\"RedisHosts\" value=\"192.168.31.42:6380, password = pisenmaster, allowAdmin = true, defaultDatabase = 1\"/>");
sb.AppendLine("</appSettings>");
Console.WriteLine(sb.ToString());
System.Diagnostics.Debug.WriteLine(sb.ToString());
throw new Exception("沒有配置redis連接字符串");
}
ConnectionMultiplexer connect = null;
try
{
connect = ConnectionMultiplexer.Connect(connectionString);
}
catch (Exception)
{
const string msg = "無法連接redis服務器,請檢查連接字符串,或redis主機";
Console.WriteLine(msg);
System.Diagnostics.Debug.WriteLine(msg);
throw new Exception(msg);
}
//注册如下事件
connect.ConnectionFailed += MuxerConnectionFailed;
connect.ConnectionRestored += MuxerConnectionRestored;
connect.ErrorMessage += MuxerErrorMessage;
connect.ConfigurationChanged += MuxerConfigurationChanged;
connect.HashSlotMoved += MuxerHashSlotMoved;
connect.InternalError += MuxerInternalError;
connect.ConfigurationChangedBroadcast += MuxerConfigurationChangedBroadcast;
return connect;
}
#region 事件
/// <summary>
/// 重新配置广播时(通常意味着主从同步更改)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConfigurationChangedBroadcast(object sender, EndPointEventArgs e)
{
Console.WriteLine($"{nameof(MuxerConfigurationChangedBroadcast)}: {e.EndPoint}");
}
/// <summary>
/// 配置更改时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
{
Console.WriteLine("Configuration changed: " + e.EndPoint);
}
/// <summary>
/// 发生内部错误时(主要用于调试)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
{
Console.WriteLine("ErrorMessage: " + e.Message);
}
/// <summary>
/// 重新建立连接之前的错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine("ConnectionRestored: " + e.EndPoint);
}
/// <summary>
/// 连接失败 , 如果重新连接成功你将不会收到这个通知
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
}
/// <summary>
/// 更改集群时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
{
Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
}
/// <summary>
/// redis类库错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
{
Console.WriteLine("InternalError:Message" + e.Exception.Message);
}
#endregion
}
}
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RedisCommon
{
/// <summary>
/// Redis操作
/// </summary>
public class RedisPubSubHelper
{
private readonly ConnectionMultiplexer _conn;
public string CustomKey;
#region 构造函数
public RedisPubSubHelper(string readWriteHosts="")
{
_conn =
string.IsNullOrWhiteSpace(readWriteHosts) ?
RedisConnectionHelp.Instance :
RedisConnectionHelp.GetConnectionMultiplexer(readWriteHosts);
}
#endregion 构造函数
#region 发布订阅
#region 同步
/// <summary>
/// 订阅
/// </summary>
/// <param name="subChannel"></param>
/// <param name="handler"></param>
public void Subscribe(string subChannel, Action<RedisChannel, RedisValue> handler = null)
{
ISubscriber sub = _conn.GetSubscriber();
sub.Subscribe(subChannel, (channel, message) =>
{
if (handler == null)
{
Console.WriteLine(subChannel + " 订阅收到消息:" + message);
}
else
{
handler(channel, message);
}
});
}
/// <summary>
/// 发布
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="msg"></param>
/// <returns></returns>
public long Publish<T>(string channel, T msg)
{
ISubscriber sub = _conn.GetSubscriber();
return sub.Publish(channel, ConvertJson(msg));
}
/// <summary>
/// 取消订阅
/// </summary>
/// <param name="channel"></param>
public void Unsubscribe(string channel)
{
ISubscriber sub = _conn.GetSubscriber();
sub.Unsubscribe(channel);
}
/// <summary>
/// 取消全部订阅
/// </summary>
public void UnsubscribeAll()
{
ISubscriber sub = _conn.GetSubscriber();
sub.UnsubscribeAll();
}
#endregion 发布订阅
#region 异步
/// <summary>
/// 订阅
/// </summary>
/// <param name="subChannel"></param>
/// <param name="handler"></param>
public async Task SubscribeAsync(string subChannel, Action<RedisChannel, RedisValue> handler = null)
{
ISubscriber sub = _conn.GetSubscriber();
await sub.SubscribeAsync(subChannel, (channel, message) =>
{
if (handler == null)
{
Console.WriteLine(subChannel + " 订阅收到消息:" + message);
}
else
{
handler(channel, message);
}
});
}
/// <summary>
/// 发布
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="msg"></param>
/// <returns></returns>
public async Task<long> PublishAsync<T>(string channel, T msg)
{
ISubscriber sub = _conn.GetSubscriber();
return await sub.PublishAsync(channel, ConvertJson(msg));
}
/// <summary>
/// 取消订阅
/// </summary>
/// <param name="channel"></param>
public async Task UnsubscribeAsync(string channel)
{
ISubscriber sub = _conn.GetSubscriber();
await sub.UnsubscribeAsync(channel);
}
/// <summary>
/// 取消全部订阅
/// </summary>
public async Task UnsubscribeAllAsync()
{
ISubscriber sub = _conn.GetSubscriber();
await sub.UnsubscribeAllAsync();
}
#endregion 发布订阅
#endregion
private string ConvertJson<T>(T value)
{
string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
return result;
}
}
}