原文链接:https://www.cnblogs.com/hua66/p/9600085.html
在使用Redis中,我们可能会遇到以下场景:
例如:
某用户向服务器中发送一个请求,服务器将用户请求加入Redis任务队列,任务完成则移出队列。
以上场景有几点疑问:
- Redis队列中数据如果不仅仅来自于我们的应用程序,那么我们怎么把这个数据加入Redis?
- 当Redis队列中用户的请求达程序所能处理的峰值。那么我们该如何处理这些用户请求?
解决方案:
- 对外提供接口,将请求数据添加至DB。启动一个定时服务,在规定时间扫描DB中的请求数据并添加至Redis队列。
- 使用分布式异步队列。
以上解决方案都可以使用插件来实现。
一、
关于Quartz.Net可以通过上面链接获取官方API。它与SQL Server中的代理作业有着同样功能。
代码示例:
/// <summary>
/// 添加Job并以周期的形式运行
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jobName">job名称</param>
/// <param name="jobGroupName">job组名称</param>
/// <param name="replace">job是否可修改</param>
/// <param name="triggerName">job触发器的名称</param>
/// <param name="minutes">job执行的时间间隔,以分为单位</param>
/// <returns></returns>
public DateTimeOffset AddJob<T>(string jobName, string jobGroupName, bool replace, string triggerName, int minutes) where T : IJob
{
IJobDetail jobDetail = JobBuilder.Create<T>().WithIdentity(jobName, jobGroupName).Build();
_sched.AddJob(jobDetail, replace);
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(triggerName, jobGroupName)
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(minutes)//表示分钟的时间间隔
.RepeatForever())
.Build();
return _sched.ScheduleJob(jobDetail, trigger).Result;
}
以上的代码中是基于Quartz封装一个添加了Job的方法。这个方法依赖于 “IJobDetail” 和 “ITrigger” 这两个对象。
“IJobDetail” 表示Job的身份信息,“ITrigger” 则包含了Job执行信息,它表示Job该如何执行。
以下为调用示例:
QuartzHelper quartzHelper = QuartzHelper.CreateInstance();
quartzHelper.AddJob<TestJob>("testJob", "testJob_Group",false, "testJob_Trigger",*);
上述实例中的 “TestJob” 实现Quartz.Net中的 "IJob" 接口。这个接口只有一个方法 “Execute”,并由Quartz.Net框架自行调用该方法。
你可以在此方法中执行你的代码。并在添加该Job制定你的执行策略 “ITrigger” 对象。然后框架会根据你制定的策略进行调用。调用参数请参见上述封装。
下面是向Redis队列插入数据的示例Job:
public class TestJob : IJob
{
Task IJob.Execute(IJobExecutionContext context)
{
//JobDataMap dataMap = context.JobDetail.JobDataMap;
Task task = Task.Run(
() =>
{
Console.WriteLine(string.Format("{0}开始执行!当前系统时间{1}", this.GetType().Name, DateTime.Now));
try
{
string redisKey = this.GetType().Name;
RedisHelper redisHelper = new RedisHelper();
if (redisHelper.KeyExists(redisKey))
{
redisHelper.KeyDelete(redisKey);
}; for (int i = ; i <= ; i++)
{
User user = new User()
{
ID = i,
Name = "user" + DateTime.Now.Ticks +"_"+ i
};
redisHelper.ListLeftPush<User>(redisKey, user);//模拟DB用户数据
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0}任务出现异常,异常信息:{1}!当前系统时间{2}", this.GetType().Name, ex.Message, DateTime.Now));
}
}
);
return task;
}
}
上面的 “TestJob” 模拟了从DB加载用户请求数据至Redis队列。至此我们已经解决了上面的第一个问题。
二、
在.Net中Redis的插件不多。比较流行有 "ServiceStack.Redis" 和 "StackExchange.Redis" 。
"ServiceStack.Redis" 为官方推出的插件。非开源插件,且普通版最高只支持 6000/S 读写。高级版是要收费的。为了后续扩展,这里我们采用 "StackExchange.Redis" 。
关于StackExchange.Redis可以通过上面链接获取官方API,目前是开源的。
在第一个问题中,已经通过定时Job的方式向Redis队列中填充数据。下面我们通过 "StackExchange.Redis" 获取Redis队列中的请求并处理这些请求。
1.加载数据至Redis:
using APP_Test.Job;
using Common.Quartz;
using Common.Redis.StackExchange;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace APP_Test
{
class Program
{
static void Main(string[] args)
{
{
RedisHelper redisHelperA = new RedisHelper();
RedisHelper redisHelperB = new RedisHelper();
string stra = redisHelperA.StringGet("mykey");
string strb = redisHelperB.StringGet("mykey");
if (stra== strb)
{
Console.WriteLine(string.Format("***********{0}=={1}***********", stra, strb));
}
else
{
Console.WriteLine(string.Format("***********{0}!={1}***********", stra, strb));
}
} { QuartzHelper quartzHelper = QuartzHelper.CreateInstance();
quartzHelper.AddJob<TestJob>("testJob", "testJob_Group",false, "testJob_Trigger",*);//这里设置了以秒为单位
} Console.ReadKey();
} }
}
可以看到上面代码执行的时间节点与我们所添加job中的 ”ITrigger “ 的触发策略完全一致。至此,我们第一步已得到验证。
2.启动处理Redis队列中请求的程序。
using APP_Test.Models;
using Common.Redis.StackExchange;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace APP_RedisClientTest
{
class Program
{
static void Main(string[] args)
{
RedisHelper redisHelper = new RedisHelper();
string redisKey = "TestJob";
while (true)
{
Action action = new Action(() =>
{
User user = redisHelper.ListLeftPop<User>(redisKey);//获取请求数据并移出队列
if (user!=null)
{
Console.WriteLine(string.Format("*******{0}*******", user.Name));
}
}
);
action.EndInvoke(action.BeginInvoke(null, null));
}
Console.ReadKey();
}
}
}
上面我启动3个客户端实例,他们一起处理Redis队列中的请求。每当Job向Redis队列中添加请求对象后就会立即被我们处理请求的程序获取并消费,每当一个请求被消费就会被移出Redis队列。并且遵循先入先出的准则。按照上述,如果出现主程序请求量过高情形,我们只需要启动多个处理请求的辅助程序即可缓解主程序的压力。
至此上面的两个问题已得到验证。
如下附个人基于 "Quartz.Net" 和 "StackExchange.Redis" 封装的帮助类
using System;
using System.Collections.Generic; namespace Common.Quartz
{
using global::Quartz;
using global::Quartz.Impl;
using global::Quartz.Impl.Matchers; /// <summary>
/// V:3.0.6.0
/// </summary>
public class QuartzHelper
{
private readonly static object _obj = new object();//单例锁 //private ISchedulerFactory _sf = null; private static IScheduler _sched = null;
/// <summary>
/// 提供IScheduler对象,访问异步方法
/// </summary>
public IScheduler Scheduler { get { return _sched; } } private static QuartzHelper _quartzHelper = null;//单例对象 private QuartzHelper()
{
//_sf = new StdSchedulerFactory();
//_sched = _sf.GetScheduler().Result;
_sched = StdSchedulerFactory.GetDefaultScheduler().Result;
_sched.Start();
} /// <summary>
/// 获取单例对象
/// </summary>
/// <returns></returns>
public static QuartzHelper CreateInstance()
{
if (_quartzHelper == null) //双if +lock
{
lock (_obj)
{
if (_quartzHelper == null)
{
_quartzHelper = new QuartzHelper();
}
}
}
return _quartzHelper;
}
public bool CheckExists(TriggerKey triggerKey)
{
return _sched.CheckExists(triggerKey).Result;
}
public bool CheckExists(JobKey jobKey)
{
return _sched.CheckExists(jobKey).Result;
}
public IReadOnlyCollection<IJobExecutionContext> GetCurrentlyExecutingJobs()
{
return _sched.GetCurrentlyExecutingJobs().Result;
} /// <summary>
/// 添加Job并以周期的形式运行
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jobName">job名称</param>
/// <param name="jobGroupName">job组名称</param>
/// <param name="replace">job是否可修改</param>
/// <param name="triggerName">job触发器的名称</param>
/// <param name="minutes">job执行的时间间隔,以分为单位</param>
/// <returns></returns>
public DateTimeOffset AddJob<T>(string jobName, string jobGroupName, bool replace, string triggerName, int minutes) where T : IJob
{
IJobDetail jobDetail = JobBuilder.Create<T>().WithIdentity(jobName, jobGroupName).Build();
_sched.AddJob(jobDetail, replace);
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(triggerName, jobGroupName)
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(minutes)//seconds表示秒的时间间隔
//.WithIntervalInMinutes(minutes)//表示分钟的时间间隔
.RepeatForever())
.Build();
return _sched.ScheduleJob(jobDetail, trigger).Result;
}
public bool DeleteJobs(IReadOnlyCollection<JobKey> jobKeys)
{
return _sched.DeleteJobs(jobKeys).Result;
}
public IJobDetail GetJobDetail(JobKey jobKey)
{
return _sched.GetJobDetail(jobKey).Result;
}
public IReadOnlyCollection<string> GetJobGroupNames()
{
return _sched.GetJobGroupNames().Result;
}
public IReadOnlyCollection<JobKey> GetJobKeys(GroupMatcher<JobKey> matcher)
{
return _sched.GetJobKeys(matcher).Result;
}
public bool Interrupt(JobKey jobKey)
{
return _sched.Interrupt(jobKey).Result;
}
public bool IsJobGroupPaused(string groupName)
{
return _sched.IsJobGroupPaused(groupName).Result;
}
public ITrigger GetTrigger(TriggerKey triggerKey)
{
return _sched.GetTrigger(triggerKey).Result;
}
public IReadOnlyCollection<string> GetTriggerGroupNames()
{
return _sched.GetTriggerGroupNames().Result;
}
public IReadOnlyCollection<TriggerKey> GetTriggerKeys(GroupMatcher<TriggerKey> matcher)
{
return _sched.GetTriggerKeys(matcher).Result;
}
public IReadOnlyCollection<ITrigger> GetTriggersOfJob(JobKey jobKey)
{
return _sched.GetTriggersOfJob(jobKey).Result;
}
public TriggerState GetTriggerState(TriggerKey triggerKey)
{
return _sched.GetTriggerState(triggerKey).Result;
}
public IReadOnlyCollection<string> GetPausedTriggerGroups()
{
return _sched.GetPausedTriggerGroups().Result;
}
public bool Interrupt(string fireInstanceId)
{
return _sched.Interrupt(fireInstanceId).Result;
}
public bool IsTriggerGroupPaused(string groupName)
{
return _sched.IsTriggerGroupPaused(groupName).Result;
}
public void PauseAll()
{
_sched.PauseAll();
}
public void PauseJobs(GroupMatcher<JobKey> matcher)
{
_sched.PauseJobs(matcher);
}
public void PauseTriggers(GroupMatcher<TriggerKey> matcher)
{
_sched.PauseTriggers(matcher);
}
public void ResumeAll()
{
_sched.ResumeAll();
}
public void ResumeJobs(GroupMatcher<JobKey> matcher)
{
_sched.ResumeJobs(matcher);
}
public void ResumeTriggers(GroupMatcher<TriggerKey> matcher)
{
_sched.ResumeTriggers(matcher);
}
public void ScheduleJobs(IReadOnlyDictionary<IJobDetail, IReadOnlyCollection<ITrigger>> triggersAndJobs, bool replace)
{
_sched.ScheduleJobs(triggersAndJobs, replace);
}
public DateTimeOffset? RescheduleJob(TriggerKey triggerKey, ITrigger newTrigger)
{
return _sched.RescheduleJob(triggerKey, newTrigger).Result;
}
public void Shutdown(bool waitForJobsToComplete)
{
_sched.Shutdown(waitForJobsToComplete);
}
public void Clear()
{
_sched.Clear();
} }
}
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using Newtonsoft.Json; namespace Common.Redis.StackExchange
{
/// <summary>
/// V:1.2.6.0
/// </summary>
public class RedisHelper
{
#region private field /// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionString; /// <summary>
/// redis 连接对象
/// </summary>
private static IConnectionMultiplexer _connMultiplexer; /// <summary>
/// 默认的 Key 值(用来当作 RedisKey 的前缀)
/// </summary>
private static readonly string DefaultKey; /// <summary>
/// 锁
/// </summary>
private static readonly object Locker = new object(); /// <summary>
/// 数据库
/// </summary>
private readonly IDatabase _db; #endregion private field #region 构造函数 static RedisHelper()
{
ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;
_connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);
DefaultKey = ConfigurationManager.AppSettings["Redis.DefaultKey"];
AddRegisterEvent();
} public RedisHelper(int db = )
{
_db = _connMultiplexer.GetDatabase(db);
} #endregion 构造函数 #region 其它 /// <summary>
/// 获取 Redis 连接对象
/// </summary>
/// <returns></returns>
public IConnectionMultiplexer GetConnectionRedisMultiplexer()
{
if (_connMultiplexer == null || !_connMultiplexer.IsConnected)
lock (Locker)
{
if (_connMultiplexer == null || !_connMultiplexer.IsConnected)
_connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);
} return _connMultiplexer;
} public ITransaction GetTransaction()
{
return _db.CreateTransaction();
} #endregion 其它 #region 类型封装 #region 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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.StringSet(redisKey, redisValue, expiry);
} /// <summary>
/// 保存多个 Key-value
/// </summary>
/// <param name="keyValuePairs"></param>
/// <returns></returns>
public bool StringSet(IEnumerable<KeyValuePair<string, string>> keyValuePairs)
{
var pairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value));
return _db.StringSet(pairs.ToArray());
} /// <summary>
/// 获取字符串
/// </summary>
/// <param name="redisKey"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public string StringGet(string redisKey, TimeSpan? expiry = null)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(_db.StringGet(redisKey));
} #region async /// <summary>
/// 保存一个字符串值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public async Task<bool> StringSetAsync(string redisKey, string redisValue, TimeSpan? expiry = null)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.StringSetAsync(redisKey, redisValue, expiry);
} /// <summary>
/// 保存一组字符串值
/// </summary>
/// <param name="keyValuePairs"></param>
/// <returns></returns>
public async Task<bool> StringSetAsync(IEnumerable<KeyValuePair<string, string>> keyValuePairs)
{
var pairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value));
return await _db.StringSetAsync(pairs.ToArray());
} /// <summary>
/// 获取单个值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public async Task<string> StringGetAsync(string redisKey, string redisValue, TimeSpan? expiry = null)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.StringGetAsync(redisKey);
} /// <summary>
/// 存储一个对象(该对象会被序列化保存)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public async Task<bool> StringSetAsync<T>(string redisKey, T redisValue, TimeSpan? expiry = null)
{
redisKey = AddKeyPrefix(redisKey);
var json = Serialize(redisValue);
return await _db.StringSetAsync(redisKey, json, expiry);
} /// <summary>
/// 获取一个对象(会进行反序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public async Task<T> StringGetAsync<T>(string redisKey, TimeSpan? expiry = null)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(await _db.StringGetAsync(redisKey));
} #endregion async #endregion String 操作 #region Hash 操作 /// <summary>
/// 判断该字段是否存在 hash 中
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public bool HashExists(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.HashDelete(redisKey, hashField);
} /// <summary>
/// 从 hash 中移除指定字段
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
/// <returns></returns>
public long HashDelete(string redisKey, IEnumerable<string> hashFields)
{
redisKey = AddKeyPrefix(redisKey);
var fields = hashFields.Select(x => (RedisValue)x); return _db.HashDelete(redisKey, fields.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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.HashSet(redisKey, hashField, value);
} /// <summary>
/// 在 hash 中设定值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
public void HashSet(string redisKey, IEnumerable<KeyValuePair<string, string>> hashFields)
{
redisKey = AddKeyPrefix(redisKey);
var entries = hashFields.Select(x => new HashEntry(x.Key, x.Value)); _db.HashSet(redisKey, entries.ToArray());
} /// <summary>
/// 在 hash 中获取值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public string HashGet(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
return _db.HashGet(redisKey, hashField);
} /// <summary>
/// 在 hash 中获取值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
/// <returns></returns>
public IEnumerable<string> HashGet(string redisKey, IEnumerable<string> hashFields)
{
redisKey = AddKeyPrefix(redisKey);
var fields = hashFields.Select(x => (RedisValue)x); return ConvertStrings(_db.HashGet(redisKey, fields.ToArray()));
} /// <summary>
/// 从 hash 返回所有的字段值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public IEnumerable<string> HashKeys(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(_db.HashKeys(redisKey));
} /// <summary>
/// 返回 hash 中的所有值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public IEnumerable<string> HashValues(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(_db.HashValues(redisKey));
} /// <summary>
/// 在 hash 设定值(序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public bool HashSet<T>(string redisKey, string hashField, T redisValue)
{
redisKey = AddKeyPrefix(redisKey);
var json = Serialize(redisValue); 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)
{
redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.HashGet(redisKey, hashField));
} #region async /// <summary>
/// 判断该字段是否存在 hash 中
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public async Task<bool> HashExistsAsync(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.HashExistsAsync(redisKey, hashField);
} /// <summary>
/// 从 hash 中移除指定字段
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public async Task<bool> HashDeleteAsync(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.HashDeleteAsync(redisKey, hashField);
} /// <summary>
/// 从 hash 中移除指定字段
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
/// <returns></returns>
public async Task<long> HashDeleteAsync(string redisKey, IEnumerable<string> hashFields)
{
redisKey = AddKeyPrefix(redisKey);
var fields = hashFields.Select(x => (RedisValue)x); return await _db.HashDeleteAsync(redisKey, fields.ToArray());
} /// <summary>
/// 在 hash 设定值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<bool> HashSetAsync(string redisKey, string hashField, string value)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.HashSetAsync(redisKey, hashField, value);
} /// <summary>
/// 在 hash 中设定值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
public async Task HashSetAsync(string redisKey, IEnumerable<KeyValuePair<string, string>> hashFields)
{
redisKey = AddKeyPrefix(redisKey);
var entries = hashFields.Select(x => new HashEntry(AddKeyPrefix(x.Key), x.Value));
await _db.HashSetAsync(redisKey, entries.ToArray());
} /// <summary>
/// 在 hash 中获取值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public async Task<string> HashGetAsync(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.HashGetAsync(redisKey, hashField);
} /// <summary>
/// 在 hash 中获取值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashFields"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> HashGetAsync(string redisKey, IEnumerable<string> hashFields,
string value)
{
redisKey = AddKeyPrefix(redisKey);
var fields = hashFields.Select(x => (RedisValue)x); return ConvertStrings(await _db.HashGetAsync(redisKey, fields.ToArray()));
} /// <summary>
/// 从 hash 返回所有的字段值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> HashKeysAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(await _db.HashKeysAsync(redisKey));
} /// <summary>
/// 返回 hash 中的所有值
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> HashValuesAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(await _db.HashValuesAsync(redisKey));
} /// <summary>
/// 在 hash 设定值(序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<bool> HashSetAsync<T>(string redisKey, string hashField, T value)
{
redisKey = AddKeyPrefix(redisKey);
var json = Serialize(value);
return await _db.HashSetAsync(redisKey, hashField, json);
} /// <summary>
/// 在 hash 中获取值(反序列化)
/// </summary>
/// <param name="redisKey"></param>
/// <param name="hashField"></param>
/// <returns></returns>
public async Task<T> HashGetAsync<T>(string redisKey, string hashField)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(await _db.HashGetAsync(redisKey, hashField));
} #endregion async #endregion Hash 操作 #region List 操作 /// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public string ListLeftPop(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListLeftPop(redisKey);
} /// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public string ListRightPop(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListRightPop(redisKey);
} /// <summary>
/// 移除列表指定键上与该值相同的元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListRemove(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListRemove(redisKey, redisValue);
} /// <summary>
/// 在列表尾部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListRightPush(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListRightPush(redisKey, redisValue);
} /// <summary>
/// 在列表头部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public long ListLeftPush(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListLeftPush(redisKey, redisValue);
} /// <summary>
/// 返回列表上该键的长度,如果不存在,返回 0
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public long ListLength(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListLength(redisKey);
} /// <summary>
/// 返回在该列表上键所对应的元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public IEnumerable<string> ListRange(string redisKey, long start = 0L, long stop = -1L)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(_db.ListRange(redisKey, start, stop));
} /// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public T ListLeftPop<T>(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(_db.ListLeftPop(redisKey));
} /// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public T ListRightPop<T>(string redisKey)
{
redisKey = AddKeyPrefix(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)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.ListLeftPush(redisKey, Serialize(redisValue));
} #region List-async /// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<string> ListLeftPopAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListLeftPopAsync(redisKey);
} /// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<string> ListRightPopAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListRightPopAsync(redisKey);
} /// <summary>
/// 移除列表指定键上与该值相同的元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public async Task<long> ListRemoveAsync(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListRemoveAsync(redisKey, redisValue);
} /// <summary>
/// 在列表尾部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public async Task<long> ListRightPushAsync(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListRightPushAsync(redisKey, redisValue);
} /// <summary>
/// 在列表头部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public async Task<long> ListLeftPushAsync(string redisKey, string redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListLeftPushAsync(redisKey, redisValue);
} /// <summary>
/// 返回列表上该键的长度,如果不存在,返回 0
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<long> ListLengthAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListLengthAsync(redisKey);
} /// <summary>
/// 返回在该列表上键所对应的元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, long start = 0L, long stop = -1L)
{
redisKey = AddKeyPrefix(redisKey);
var query = await _db.ListRangeAsync(redisKey, start, stop);
return query.Select(x => x.ToString());
} /// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<T> ListLeftPopAsync<T>(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(await _db.ListLeftPopAsync(redisKey));
} /// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<T> ListRightPopAsync<T>(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return Deserialize<T>(await _db.ListRightPopAsync(redisKey));
} /// <summary>
/// 在列表尾部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public async Task<long> ListRightPushAsync<T>(string redisKey, T redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListRightPushAsync(redisKey, Serialize(redisValue));
} /// <summary>
/// 在列表头部插入值。如果键不存在,先创建再插入值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="redisValue"></param>
/// <returns></returns>
public async Task<long> ListLeftPushAsync<T>(string redisKey, T redisValue)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.ListLeftPushAsync(redisKey, Serialize(redisValue));
} #endregion List-async #endregion List 操作 #region 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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.SortedSetAdd(redisKey, member, score);
} /// <summary>
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。
/// </summary>
/// <param name="redisKey"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <param name="order"></param>
/// <returns></returns>
public IEnumerable<string> SortedSetRangeByRank(string redisKey, long start = 0L, long stop = -1L,
Order order = Order.Ascending)
{
redisKey = AddKeyPrefix(redisKey);
return _db.SortedSetRangeByRank(redisKey, start, stop, (Order)order).Select(x => x.ToString());
} /// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public long SortedSetLength(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return _db.SortedSetLength(redisKey);
} /// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <param name="memebr"></param>
/// <returns></returns>
public bool SortedSetLength(string redisKey, string memebr)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
var json = Serialize(member); return _db.SortedSetAdd(redisKey, json, score);
} /// <summary>
/// 增量的得分排序的集合中的成员存储键值键按增量
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="value"></param>
/// <returns></returns>
public double SortedSetIncrement(string redisKey, string member, double value = )
{
redisKey = AddKeyPrefix(redisKey);
return _db.SortedSetIncrement(redisKey, member, value);
} #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)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.SortedSetAddAsync(redisKey, member, score);
} /// <summary>
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> SortedSetRangeByRankAsync(string redisKey)
{
redisKey = AddKeyPrefix(redisKey);
return ConvertStrings(await _db.SortedSetRangeByRankAsync(redisKey));
} /// <summary>
/// 返回有序集合的元素个数
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<long> SortedSetLengthAsync(string redisKey)
{
redisKey = AddKeyPrefix(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)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
var json = Serialize(member); return await _db.SortedSetAddAsync(redisKey, json, score);
} /// <summary>
/// 增量的得分排序的集合中的成员存储键值键按增量
/// </summary>
/// <param name="redisKey"></param>
/// <param name="member"></param>
/// <param name="value"></param>
/// <returns></returns>
public Task<double> SortedSetIncrementAsync(string redisKey, string member, double value = )
{
redisKey = AddKeyPrefix(redisKey);
return _db.SortedSetIncrementAsync(redisKey, member, value);
} #endregion SortedSet-Async #endregion SortedSet 操作 #endregion 类型封装 #region 将object序列化读写
public void ListSet<T>(string key, List<T> value)
{
foreach (var single in value)
{
var jsonobj = JsonConvert.SerializeObject(single); //序列化
this.ListLeftPush(key, jsonobj); //要一个个的插入 }
}
public List<T> ListGet<T>(string key)
{
var jsonArr = this.ListRange(key);
List<T> result = new List<T>();
foreach (var item in jsonArr)
{
var model = JsonConvert.DeserializeObject<T>(item); //反序列化
result.Add(model);
}
return result;
}
#endregion #region KEY 操作 /// <summary>
/// 删除单个key
/// </summary>
/// <param name="redisKey"></param>
/// <returns>是否删除成功</returns>
public bool KeyDelete(string redisKey)
{
redisKey = AddKeyPrefix(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)AddKeyPrefix(x));
return _db.KeyDelete(keys.ToArray());
} /// <summary>
/// 校验 Key 是否存在
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public bool KeyExists(string redisKey)
{
redisKey = AddKeyPrefix(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)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
return _db.KeyExpire(redisKey, expiry);
} #region key-async /// <summary>
/// 移除指定 Key
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<bool> KeyDeleteAsync(string redisKey)
{
redisKey = AddKeyPrefix(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)AddKeyPrefix(x));
return await _db.KeyDeleteAsync(keys.ToArray());
} /// <summary>
/// 校验 Key 是否存在
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<bool> KeyExistsAsync(string redisKey)
{
redisKey = AddKeyPrefix(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)
{
redisKey = AddKeyPrefix(redisKey);
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)
{
redisKey = AddKeyPrefix(redisKey);
return await _db.KeyExpireAsync(redisKey, expiry);
} #endregion key-async #endregion KEY 操作 #region private method /// <summary>
/// 添加 Key 的前缀
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static string AddKeyPrefix(string key)
{
return DefaultKey + ":" + key;
} /// <summary>
/// 转换为字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
private static IEnumerable<string> ConvertStrings<T>(IEnumerable<T> list) where T : struct
{
if (list == null) throw new ArgumentNullException(nameof(list));
return list.Select(x => x.ToString());
} #region 注册事件 /// <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}");
} #endregion 注册事件 /// <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;
}
} #endregion private method }
}
以上代码未全部测试,如若使用请自行验证。如代码有误请帮忙指出,谢谢!