干货.NET/C#中使用Redis
Posted 新阁Net社群
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了干货.NET/C#中使用Redis相关的知识,希望对你有一定的参考价值。
一
简介
在C#中使用Redis,一般有两种方式:
1、ServiceStack.Redis,据说是Redis官方推荐使用的驱动类库,但是是收费的。
2、StackExchange.Redis,可能性能要比ServiceStack.Redis差点,但是是免费的。
本次使用StackExchange.Redis来实现Redis操作。
二
添加StackExchange.Redis引用
想要在C#中使用Redis,首先得要有个Redis支持的C#版的驱动。
通过网络下载或nuget安装,得到Redis相关的dll,添加到项目中引用。这里介绍下通过NuGet方式添加
第一步:在项目中右键,选择管理NuGet管理包
第二步:搜索StackExchange.Redis添加,我这里已经添加过了所以没有添加按钮
通过这两步,会在项目中自动添加StackExchange.Redis引用
有需要的可以自行添加Newtonsoft.Json引用
StackExchange.Redis.dll:Redis驱动程序
三
连接数据库
在连接数据库之前,请确保Redis数据库服务已经启动。
StackExchange.Redis核心代码都在ConnectionMultiplexer类中,这个类的实例不需要在每个操作中不停的创建,一般都会封装为单例模式共享使用该对象实例。
1.连接字符串(redis数据库默认端口为:6379)
private static readonly string ConnectionWriteString = "127.0.0.1:6379";
2.创建连接
private static readonly IConnectionMultiplexer ConnMultiplexer = ConnectionMultiplexer.Connect(ConnectionWriteString);
3.数据库
private readonly IDatabase _db=ConnMultiplexer.GetDatabase(0);//参数为创建的数据库的位置
四
操作String类型方法封装
/// <summary>
/// 设置 key 并保存字符串(如果 key 已存在,则覆盖值)
/// </summary>
/// <param name="redisKey">名称</param>
/// <param name="redisValue">值</param>
/// <param name="expiry">时间</param>
/// <returns></returns>
public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null)
{
return _db.StringSet(redisKey, redisValue, expiry);
}
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="redisKey">名称</param>
/// <param name="expiry">时间</param>
/// <returns></returns>
public string StringGet(string redisKey, TimeSpan? expiry = null)
{
return _db.StringGet(redisKey);
}
/// <summary>
/// 存储一个对象(该对象会被序列化保存)
/// </summary>
/// <param name="redisKey">名称</param>
/// <param name="redisValue">值</param>
/// <param name="expiry">时间</param>
/// <returns></returns>
public bool StringSet<T>(string redisKey, T redisValue, TimeSpan? expiry = null)
{
var json = Serialize(redisValue);
return _db.StringSet(redisKey, json, expiry);
}
/// <summary>
/// 获取一个对象(会进行反序列化)
/// </summary>
/// <param name="redisKey">名称</param>
/// <param name="expiry">时间</param>
/// <returns></returns>
public T StringGet<T>(string redisKey, TimeSpan? expiry = null)
{
return Deserialize<T>(_db.StringGet(redisKey));
}
五
操作Hash类型方法封装
/// <summary>
/// 判断该字段是否存在 hash 中
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public bool HashExists(string redisKey, string hashField)
{
return _db.HashExists(redisKey, hashField);
}
/// <summary>
/// 从 hash 中移除指定字段
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public bool HashDelete(string redisKey, string hashField)
{
return _db.HashDelete(redisKey, hashField);
}
/// <summary>
/// 从 hash 中移除指定字段(多个删除)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public long HashDelete(string redisKey, IEnumerable<RedisValue> hashField)
{
return _db.HashDelete(redisKey, hashField.ToArray());
}
/// <summary>
/// 在 hash 设定值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool HashSet(string redisKey, string hashField, string value)
{
return _db.HashSet(redisKey, hashField, value);
}
/// <summary>
/// 在 hash 中设定值(多个)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
public void HashSet(string redisKey, IEnumerable<HashEntry> hashFields)
{
_db.HashSet(redisKey, hashFields.ToArray());
}
/// <summary>
/// 在 hash 中获取值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public RedisValue HashGet(string redisKey, string hashField)
{
return _db.HashGet(redisKey, hashField);
}
/// <summary>
/// 在 hash 中获取值(多个)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <returns></returns>
public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value)
{
return _db.HashGet(redisKey, hashField);
}
/// <summary>
/// 从 hash 返回所有的字段值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public IEnumerable<RedisValue> HashKeys(string redisKey)
{
return _db.HashKeys(redisKey);
}
/// <summary>
/// 返回 hash 中的所有值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public RedisValue[] HashValues(string redisKey)
{
return _db.HashValues(redisKey);
}
/// <summary>
/// 在 hash 设定值(序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool HashSet<T>(string redisKey, string hashField, T value)
{
var json = Serialize(value);
return _db.HashSet(redisKey, hashField, json);
}
/// <summary>
/// 在 hash 中获取值(反序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public T HashGet<T>(string redisKey, string hashField)
{
return Deserialize<T>(_db.HashGet(redisKey, hashField));
}
六
操作List类型方法封装
/// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public string ListLeftPop(string redisKey)
{
return _db.ListLeftPop(redisKey);
}
/// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public string ListRightPop(string redisKey)
{
return _db.ListRightPop(redisKey);
}
/// <summary>
/// 移除列表指定键上与该值相同的元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListRemove(string redisKey, string redisValue)
{
return _db.ListRemove(redisKey, redisValue);
}
/// <summary>
/// 在列表尾部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListRightPush(string redisKey, string redisValue)
{
return _db.ListRightPush(redisKey, redisValue);
}
/// <summary>
/// 在列表头部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListLeftPush(string redisKey, string redisValue)
{
return _db.ListLeftPush(redisKey, redisValue);
}
/// <summary>
/// 返回列表上该键的长度,如果不存在,返回 0
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public long ListLength(string redisKey)
{
return _db.ListLength(redisKey);
}
/// <summary>
/// 返回在该列表上键所对应的元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public IEnumerable<RedisValue> ListRange(string redisKey)
{
return _db.ListRange(redisKey);
}
/// <summary>
/// 返回在该列表上键所对应的元素
/// </summary>
/// <param name="redisKey">开始行</param>
/// <param name="startRow">结束行</param>
/// <returns></returns>
public IEnumerable<RedisValue> ListRange(string redisKey, int startRow, int endRow)
{
return _db.ListRange(redisKey, startRow, endRow);
}
/// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public T ListLeftPop<T>(string redisKey)
{
return Deserialize<T>(_db.ListLeftPop(redisKey));
}
/// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public T ListRightPop<T>(string redisKey)
{
return Deserialize<T>(_db.ListRightPop(redisKey));
}
/// <summary>
/// 在列表尾部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListRightPush<T>(string redisKey, T redisValue)
{
return _db.ListRightPush(redisKey, Serialize(redisValue));
}
/// <summary>
/// 在列表头部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListLeftPush<T>(string redisKey, T redisValue)
{
return _db.ListLeftPush(redisKey, Serialize(redisValue));
}
七
操作SortedSet类型方法封装
/// <summary>
/// SortedSet 新增
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="score"></param>
/// <returns></returns>
public bool SortedSetAdd(string redisKey, string member, double score)
{
return _db.SortedSetAdd(redisKey, member, score);
}
/// <summary>
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public IEnumerable<RedisValue> SortedSetRangeByRank(string redisKey)
{
return _db.SortedSetRangeByRank(redisKey);
}
/// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public long SortedSetLength(string redisKey)
{
return _db.SortedSetLength(redisKey);
}
/// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <param name="memebr"></param>
/// <returns></returns>
public bool SortedSetLength(string redisKey, string memebr)
{
return _db.SortedSetRemove(redisKey, memebr);
}
/// <summary>
/// SortedSet 新增
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="score"></param>
/// <returns></returns>
public bool SortedSetAdd<T>(string redisKey, T member, double score)
{
var json = Serialize(member);
return _db.SortedSetAdd(redisKey, json, score);
}
#region SortedSet-Async
/// <summary>
/// SortedSet 新增
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="score"></param>
/// <returns></returns>
public async Task<bool> SortedSetAddAsync(string redisKey, string member, double score)
{
return await _db.SortedSetAddAsync(redisKey, member, score);
}
/// <summary>
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<IEnumerable<RedisValue>> SortedSetRangeByRankAsync(string redisKey)
{
return await _db.SortedSetRangeByRankAsync(redisKey);
}
/// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<long> SortedSetLengthAsync(string redisKey)
{
return await _db.SortedSetLengthAsync(redisKey);
}
/// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <param name="memebr"></param>
/// <returns></returns>
public async Task<bool> SortedSetRemoveAsync(string redisKey, string memebr)
{
return await _db.SortedSetRemoveAsync(redisKey, memebr);
}
/// <summary>
/// SortedSet 新增
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="score"></param>
/// <returns></returns>
public async Task<bool> SortedSetAddAsync<T>(string redisKey, T member, double score)
{
var json = Serialize(member);
return await _db.SortedSetAddAsync(redisKey, json, score);
}
八
操作key类型方法封装
/// <summary>
/// 移除指定 Key
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public bool KeyDelete(string redisKey)
{
return _db.KeyDelete(redisKey);
}
/// <summary>
/// 移除指定 Key
/// </summary>
/// <param name="redisKeys"></param>
/// <returns></returns>
public long KeyDelete(IEnumerable<string> redisKeys)
{
var keys = redisKeys.Select(x => (RedisKey)x);
return _db.KeyDelete(keys.ToArray());
}
/// <summary>
/// 校验 Key 是否存在
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public bool KeyExists(string redisKey)
{
return _db.KeyExists(redisKey);
}
/// <summary>
/// 重命名 Key
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisNewKey"></param>
/// <returns></returns>
public bool KeyRename(string redisKey, string redisNewKey)
{
return _db.KeyRename(redisKey, redisNewKey);
}
/// <summary>
/// 设置 Key 的时间
/// </summary>
/// <param name="redisKey"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public bool KeyExpire(string redisKey, TimeSpan? expiry)
{
return _db.KeyExpire(redisKey, expiry);
}
#region key-async
/// <summary>
/// 移除指定 Key
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<bool> KeyDeleteAsync(string redisKey)
{
return await _db.KeyDeleteAsync(redisKey);
}
/// <summary>
/// 移除指定 Key
/// </summary>
/// <param name="redisKeys"></param>
/// <returns></returns>
public async Task<long> KeyDeleteAsync(IEnumerable<string> redisKeys)
{
var keys = redisKeys.Select(x => (RedisKey)x);
return await _db.KeyDeleteAsync(keys.ToArray());
}
/// <summary>
/// 校验 Key 是否存在
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<bool> KeyExistsAsync(string redisKey)
{
return await _db.KeyExistsAsync(redisKey);
}
/// <summary>
/// 重命名 Key
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisNewKey"></param>
/// <returns></returns>
public async Task<bool> KeyRenameAsync(string redisKey, string redisNewKey)
{
return await _db.KeyRenameAsync(redisKey, redisNewKey);
}
/// <summary>
/// 设置 Key 的时间
/// </summary>
/// <param name="redisKey"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public async Task<bool> KeyExpireAsync(string redisKey, TimeSpan? expiry)
{
return await _db.KeyExpireAsync(redisKey, expiry);
}
九
发布订阅方法封装
/// <summary>
/// 订阅
/// </summary>
/// <param name="channel"></param>
/// <param name="handle"></param>
public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle)
{
var sub = ConnMultiplexer.GetSubscriber();
sub.Subscribe(channel, handle);
}
/// <summary>
/// 发布
/// </summary>
/// <param name="channel"></param>
/// <param name="message"></param>
/// <returns></returns>
public long Publish(RedisChannel channel, RedisValue message)
{
var sub = ConnMultiplexer.GetSubscriber();
return sub.Publish(channel, message);
}
/// <summary>
/// 发布(使用序列化)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="message"></param>
/// <returns></returns>
public long Publish<T>(RedisChannel channel, T message)
{
var sub = ConnMultiplexer.GetSubscriber();
return sub.Publish(channel, Serialize(message));
}
#region 发布订阅-async
/// <summary>
/// 订阅
/// </summary>
/// <param name="channel"></param>
/// <param name="handle"></param>
public async Task SubscribeAsync(RedisChannel channel, Action<RedisChannel, RedisValue> handle)
{
var sub = ConnMultiplexer.GetSubscriber();
await sub.SubscribeAsync(channel, handle);
}
/// <summary>
/// 发布
/// </summary>
/// <param name="channel"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task<long> PublishAsync(RedisChannel channel, RedisValue message)
{
var sub = ConnMultiplexer.GetSubscriber();
return await sub.PublishAsync(channel, message);
}
/// <summary>
/// 发布(使用序列化)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="channel"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task<long> PublishAsync<T>(RedisChannel channel, T message)
{
var sub = ConnMultiplexer.GetSubscriber();
return await sub.PublishAsync(channel, Serialize(message));
}
十
事件方法封装
/// <summary>
/// 添加注册事件
/// </summary>
private static void AddRegisterEvent()
{
ConnMultiplexer.ConnectionRestored += ConnMultiplexer_ConnectionRestored;
ConnMultiplexer.ConnectionFailed += ConnMultiplexer_ConnectionFailed;
ConnMultiplexer.ErrorMessage += ConnMultiplexer_ErrorMessage;
ConnMultiplexer.ConfigurationChanged += ConnMultiplexer_ConfigurationChanged;
ConnMultiplexer.HashSlotMoved += ConnMultiplexer_HashSlotMoved;
ConnMultiplexer.InternalError += ConnMultiplexer_InternalError;
ConnMultiplexer.ConfigurationChangedBroadcast += ConnMultiplexer_ConfigurationChangedBroadcast;
}
/// <summary>
/// 重新配置广播时(通常意味着主从同步更改)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}");
}
/// <summary>
/// 发生内部错误时(主要用于调试)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}");
}
/// <summary>
/// 更改集群时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_HashSlotMoved(object sender, HashSlotMovedEventArgs e)
{
Console.WriteLine(
$"{nameof(ConnMultiplexer_HashSlotMoved)}: {nameof(e.OldEndPoint)}-{e.OldEndPoint} To {nameof(e.NewEndPoint)}-{e.NewEndPoint}, ");
}
/// <summary>
/// 配置更改时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}");
}
/// <summary>
/// 发生错误时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}");
}
/// <summary>
/// 物理连接失败时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}");
}
/// <summary>
/// 建立物理连接时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e)
{
Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}");
}
十一
序列化方法封装
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private static byte[] Serialize(object obj)
{
if (obj == null)
return null;
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, obj);
var data = memoryStream.ToArray();
return data;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
private static T Deserialize<T>(byte[] data)
{
if (data == null)
return default(T);
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream(data))
{
var result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
十二
调用Redis方法
RedisBaseTools redis = new RedisBaseTools();
redis.StringSet("keyname", "1234567", new TimeSpan(0, 10, 0));
redis.StringGet("keyname");
其他类型方法调用方式相似
END
●
●
●
●
●
●
以上是关于干货.NET/C#中使用Redis的主要内容,如果未能解决你的问题,请参考以下文章
#yyds干货盘点#愚公系列2023年02月 .NET/C#知识点-区块链概念和实现
#yyds干货盘点#愚公系列2023年02月 .NET/C#知识点-EF Core性能优化之显示编译