StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)

时间:2021-11-11 08:42:43

本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/

目录

一、基础配置封装

二、String字符串类型数据操作封装

三、Hash散列类型数据操作封装

四、List列表类型数据操作封装(建议自行封装)

五、Set集合类型数据操作封装(建议自行封装)

六、Sort Set集合数据类型操作封装(建议自行封装)

七、发布订阅(Pub/Sub)模式在StackExchange.Redis中的使用

八、主从配置,哨兵相关配置

二、String字符串类型数据操作封装

下面这段画如果看一遍没看懂,请看过代码后再次来阅读:

我们有必要先提到ConnectionMultiplexer类,它是StackExchange提供给我们的一个类,它将多服务器的详细信息隐藏,因为这个类为我们做了很多事情,它的设计目的是为了在调用者间共享和重用。你不用每次操作都创建这样一个ConnectionMultiplexer,它是完全线程安全的。它拥有ConnectionMultiplexer.Connect和onnectionMultiplexer.ConnectAsync来链接Redis。链接 参数是一个字符串或者一个ConfigurationOptions对象。这个类实现了IDisposable接口,你可以在你不需要的时候释放对象,通过using或者dispose。但是你不用经常来释放它,因为我们要经常复用。你有三种需求时,需要使用ConnectionMultiplexer,链接Redis,发布订阅模式,访问一个单独的服务器或者监控的目的。除了基本使用,更多的请看github文档,https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Basics.md

我们在IRedisClient中定义如下String数据类型方法:

如果你觉得代码太多,当VS按下快捷键ctrl+M+O吧

 #region 程序集 RedisRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Author:吴双 2016.8.28 联系邮箱 wscoder@outlook.com
#endregion
using System;
using System.Collections.Generic;
using StackExchange.Redis; namespace RedisRepository
{
public interface IRedisClient
{
#region Redis String类型操作
/// <summary>
/// Redis String类型 新增一条记录
/// </summary>
/// <typeparam name="T">generic refrence type</typeparam>
/// <param name="key">unique key of value</param>
/// <param name="value">value of key of type object</param>
/// <param name="expiresAt">time span of expiration</param>
/// <param name= "when">枚举类型</param>
/// <param name="commandFlags"></param>
/// <returns>true or false</returns>
bool StringSet<T>(string key, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; /// <summary>
/// Redis String类型 新增一条记录
/// </summary>
/// <typeparam name="T">generic refrence type</typeparam>
/// <param name="key">unique key of value</param>
/// <param name="value">value of key of type object</param>
/// <param name="expiresAt">time span of expiration</param>
/// <param name= "when">枚举类型</param>
/// <param name="commandFlags"></param>
/// <returns>true or false</returns>
bool StringSet<T>(string key, T value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; /// <summary>
/// 更新时应使用此方法,代码更可读。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiresAt"></param>
/// <param name="when"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; /// <summary>
/// Redis String类型 Get
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="commandFlags"></param>
/// <returns>T</returns>
T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class; /// <summary>
/// Redis String数据类型 获取指定key中字符串长度
/// </summary>
/// <param name="key"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
long StringLength(string key, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// Redis String数据类型 返回拼接后总长度
/// </summary>
/// <param name="key"></param>
/// <param name="appendVal"></param>
/// <param name="commandFlags"></param>
/// <returns>总长度</returns>
long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// 设置新值并且返回旧值
/// </summary>
/// <param name="key"></param>
/// <param name="newVal"></param>
/// <param name="commandFlags"></param>
/// <returns>OldVal</returns>
string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="val"></param>
/// <param name="commandFlags"></param>
/// <returns>增长后的值</returns>
double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// Redis String数据类型
/// 类似于模糊查询 key* 查出所有key开头的键
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="pageSize"></param>
/// <param name="commandFlags"></param>
/// <returns>返回List<T></returns>
List<T> StringGetList<T>(string key, int pageSize = , CommandFlags commandFlags = CommandFlags.None) where T : class;
#endregion #region Redis各数据类型公用 /// <summary>
/// Redis中是否存在指定Key
/// </summary>
/// <param name="key"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// 从Redis中移除键
/// </summary>
/// <param name="key"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// 从Redis中移除多个键
/// </summary>
/// <param name="keys"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None); /// <summary>
/// Dispose DB connection 释放DB相关链接
/// </summary>
void DbConnectionStop();
#endregion
}
}

在RedisClient.cs中实现如下:

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using StackExchange.Redis; namespace RedisRepository
{
public class RedisClient : IRedisClient
{ #region 初始化 private static readonly IDatabase _db;
private static readonly ConnectionMultiplexer _redis; /// <summary>
/// 构造函数,在其中注册Redis事件
/// </summary>
static RedisClient()
{
const string configuration = "{0},abortConnect=false,defaultDatabase={1},ssl=false,ConnectTimeout={2},allowAdmin=true,connectRetry={3}";
_redis = ConnectionMultiplexer
.Connect(string.Format(configuration, RedisClientConfigurations.Url,
RedisClientConfigurations.DefaultDatabase, RedisClientConfigurations.ConnectTimeout,
RedisClientConfigurations.ConnectRetry));
_redis.PreserveAsyncOrder = RedisClientConfigurations.PreserveAsyncOrder;
//_redis.ConnectionFailed;
_db = _redis.GetDatabase();
} #endregion #region Redis String数据类型操作 /// <summary>
/// Redis String类型 新增一条记录
/// </summary>
/// <typeparam name="T">generic refrence type</typeparam>
/// <param name="key">unique key of value</param>
/// <param name="value">value of key of type T</param>
/// <param name="expiresAt">time span of expiration</param>
/// <returns>true or false</returns>
public bool StringSet<T>(string key, T value, TimeSpan? expiresAt = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class
{
var stringContent = SerializeContent(value);
return _db.StringSet(key, stringContent, expiresAt, when, commandFlags);
} /// <summary>
/// Redis String类型 新增一条记录
/// </summary>
/// <typeparam name="T">generic refrence type</typeparam>
/// <param name="key">unique key of value</param>
/// <param name="value">value of key of type object</param>
/// <param name="expiresAt">time span of expiration</param>
/// <returns>true or false</returns>
public bool StringSet<T>(string key, object value, TimeSpan? expiresAt = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class
{
var stringContent = SerializeContent(value); return _db.StringSet(key, stringContent, expiresAt, when, commandFlags);
} /// <summary>
/// Redis String数据类型 获取指定key中字符串长度
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public long StringLength(string key, CommandFlags commandFlags = CommandFlags.None)
{
return _db.StringLength(key, commandFlags);
} /// <summary>
/// Redis String数据类型 返回拼接后总长度
/// </summary>
/// <param name="key"></param>
/// <param name="appendVal"></param>
/// <returns>总长度</returns>
public long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None)
{
return _db.StringAppend(key, appendVal, commandFlags);
} /// <summary>
/// 设置新值并且返回旧值
/// </summary>
/// <param name="key"></param>
/// <param name="newVal"></param>
/// <param name="commandFlags"></param>
/// <returns>OldVal</returns>
public string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None)
{
return DeserializeContent<string>(_db.StringGetSet(key, newVal, commandFlags));
} /// <summary>
/// 更新时应使用此方法,代码更可读。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiresAt"></param>
/// <param name="when"></param>
/// <param name="commandFlags"></param>
/// <returns></returns>
public bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class
{
var stringContent = SerializeContent(value);
return _db.StringSet(key, stringContent, expiresAt, when, commandFlags);
} /// <summary>
/// 为数字增长val
/// </summary>
/// <param name="key"></param>
/// <param name="val">可以为负</param>
/// <param name="commandFlags"></param>
/// <returns>增长后的值</returns>
public double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None)
{
return _db.StringIncrement(key, val, commandFlags);
} /// <summary>
/// Redis String类型 Get
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns>T</returns>
public T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class
{
try
{
RedisValue myString = _db.StringGet(key, commandFlags);
if (myString.HasValue && !myString.IsNullOrEmpty)
{
return DeserializeContent<T>(myString);
}
else
{
return null;
}
}
catch (Exception)
{
// Log Exception
return null;
}
} /// <summary>
/// Redis String类型
/// 类似于模糊查询 key* 查出所有key开头的键
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="pageSize"></param>
/// <param name="commandFlags"></param>
/// <returns>List<T></returns>
public List<T> StringGetList<T>(string key, int pageSize = , CommandFlags commandFlags = CommandFlags.None) where T : class
{
try
{
var server = _redis.GetServer(host: RedisClientConfigurations.Url,
port: RedisClientConfigurations.Port);
var keys = server.Keys(_db.Database, key, pageSize, commandFlags);
var keyValues = _db.StringGet(keys.ToArray(), commandFlags); var result = new List<T>();
foreach (var redisValue in keyValues)
{
if (redisValue.HasValue && !redisValue.IsNullOrEmpty)
{
var item = DeserializeContent<T>(redisValue);
result.Add(item);
}
} return result;
}
catch (Exception)
{
// Log Exception
return null;
}
} #endregion #region Redis Hash散列数据类型操作 #endregion #region Redis List列表数据类型操作 #endregion #region Redis Set集合数据类型操作 #endregion #region Redis Sort Set有序集合数据类型操作 #endregion #region Redis各数据类型公用 /// <summary>
/// Redis中是否存在指定Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None)
{
return _db.KeyExists(key, commandFlags);
} /// <summary>
/// Dispose DB connection 释放DB相关链接
/// </summary>
public void DbConnectionStop()
{
_redis.Dispose();
} /// <summary>
/// 从Redis中移除键
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None)
{
return _db.KeyDelete(key, commandFlags);
}
/// <summary>
/// 从Redis中移除多个键
/// </summary>
/// <param name="keys"></param>
public void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None)
{
_db.KeyDelete(keys, commandFlags);
}
#endregion #region 私有公用方法 // serialize and Deserialize content in separate functions as redis can save value as array of binary.
// so, any time you need to change the way of handling value, do it here. private string SerializeContent(object value)
{
return JsonConvert.SerializeObject(value);
} private T DeserializeContent<T>(RedisValue myString)
{
return JsonConvert.DeserializeObject<T>(myString);
} #endregion
}
}

下面简单介绍一下本文中的方法细节之处。

首先在RedisClient类的构造方法中初始化Redis数据操作对象_db。每个方法更多的详尽信息请注意方法注释。如果关于Redis命令还不了解,请看前期Redis命令拾遗系列文章 http://www.cnblogs.com/tdws/tag/NoSql/

有园友指出问题说

Because the ConnectionMultiplexer does a lot, it is designed to be shared and reused between callers. You should not create a ConnectionMultiplexer per operation. It is fully thread-safe and ready for this usage.
这是官方文档的原话,ConnectionMultiplexer比较“重”,建议共用,不要每次操作就创建一个

所以暂时将构造函数改为静态构造。如果有问题,欢迎提之处

30日,反思了一下,还是加锁的单例更为合适。