• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#进阶(4)——Redis用于消息队列的存储

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、参考的博文

a : http://www.cnblogs.com/lori/archive/2012/04/12/2443708.html —— 主要的实现思路

b:  http://www.cnblogs.com/liqingwen/archive/2017/04/06/6672452.html —— RedisHelper 类

c : https://www.cnblogs.com/stopfalling/p/5375492.html —— 应用场景说明

2、原理说明

    博文a 中的老师,提供了Redis 实现消息队列的整体思路,言简意赅,但部分类库a 老师并未提供,因此我参照了博文b 中老师的RedisHelper 类,主要借鉴的方法为ListLeftPop及ListRightPush,及实现消息队列的核心思想,先进先出。

    博文c 中老师详细介绍了几种消息队列的 业务场景,是我所看的所有业务场景描述中最为详细,清晰的,结合博文a 老师的整体思路,对消息队列的实现上有了较为清晰的认识。

整体思路在博文a 中老师已经介绍,及依靠 mvc 框架,通过web端用户提交事件,实现消息入列,通过定时器(Timer)实现按照时间间隔的消息出列,Redis 作为存储媒介,存储消

息内容。

3、具体实现

3.1 前期准备

(1)开发工具:VS 2017;redis-desktop-manager 客户端;Redis版本:Redis-x64-3.2.100

(2)新建MVC5 框架,配置 web.config 文件,RouteConfig文件

       a、新建MVC5 框架

         b、配置web.config 文件(Default key 主要用于区别不同的key值,此处使用了解决方法名称)

    c、配置路由(使用经典模式即可,路由的详细配置说明可参见我的另一篇博文 )

  

(3)目前.net 框架中支持Redis 的dll包括ServiceStackRedis 及 StackExchange,前者下载地址在Git hub 上,但该类库在6000并发后会抛出license exception 的异常。因此

我此次使用的dll 为基于微软的StackExchage dll,该dll的下载方式是,在NuGet中,找到StackExchage.Redis 类包,下载即可,见下图。

(4)创建消息实体对象,如下图所示

 

(此处userId 为 字符型,int 型在序列化时,会出现异常,请注意。)

 3.2 引入Redis 类库,此部分未做大量修改,代码拷贝自 博文b 

using System;
using System.Linq;
using StackExchange.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace MVC5Project.Redis.BaseOnStackExchage
{
    public class RedisHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static readonly string ConnectionString;
        /// <summary>
        /// redis 连接对象
        /// </summary>
        private static IConnectionMultiplexer _connMultiplexer;
        /// <summary>
        /// 默认的key值(用来当作RedisKey的前缀)【此部分为自行修改的,无意义】
        /// </summary>
        public static string DefaultKey { get; private set; }
        /// <summary>
        ////// </summary>
        private static readonly object Locker = new object();
        /// <summary>
        /// 数据库访问对象
        /// </summary>
        private readonly IDatabase _db;
        /// <summary>
        /// 采用双重锁单例模式,保证数据访问对象有且仅有一个
        /// </summary>
        /// <returns></returns>
        public IConnectionMultiplexer GetConnectionRedisMultiplexer()
        {
            if ((_connMultiplexer == null || !_connMultiplexer.IsConnected))
            {
                lock (Locker)
                {
                    if ((_connMultiplexer == null || !_connMultiplexer.IsConnected))
                    {
                        _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);
                    }
                }
            }
            return _connMultiplexer;
        }
        /// <summary>
        /// 添加事务处理
        /// </summary>
        /// <returns></returns>
        public ITransaction GetTransaction()
        {
            //创建事务
            return _db.CreateTransaction();
        }
        /// <summary>
        /// 静态的构造函数,
        /// 构造函数是属于类的,而不是属于实例的
        /// 就是说这个构造函数只会被执行一次。也就是在创建第一个实例或引用任何静态成员之前,由.NET自动调用。
        /// </summary>
        static RedisHelper()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;
            _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);
            DefaultKey = ConfigurationManager.AppSettings["Redis.DefaultKey"];
            RegisterEvent();
        }
        /// <summary>
        /// 重载构造器
        /// </summary>
        /// <param name="db"></param>
        public RedisHelper(int db = -1)
        {
            _db = _connMultiplexer.GetDatabase(db);
        }

        #region private method
        /// <summary>
        /// 添加 key 的前缀
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string AddKeyPrefix(string key)
        {
            return $"{DefaultKey}:{key}";
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static byte[] Serialize(object obj)
        {
            try
            {
                if (obj == null)
                    return null;
                var binaryFormatter = new BinaryFormatter();
                using (var memoryStream = new MemoryStream())
                {
                    binaryFormatter.Serialize(memoryStream, obj);
                    var data = memoryStream.ToArray();
                    return data;
                }
            }
            catch (SerializationException ex) 
            {
                throw ex;
            }
        }
        /// <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 

        #region stringGet 
        /// <summary>
        /// 设置key,并保存字符串(如果key 已存在,则覆盖)
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expried"></param>
        /// <returns></returns>
        public bool StringSet(string redisKey, string redisValue, TimeSpan? expried = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.StringSet(redisKey, redisValue, expried);
        }
        /// <summary>
        /// 保存多个key-value
        /// </summary>
        /// <param name="keyValuePairs"></param>
        /// <returns></returns>
        public bool StringSet(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs)
        {
            keyValuePairs =
                keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value));
            return _db.StringSet(keyValuePairs.ToArray());
        }
        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public string StringGet(string redisKey, TimeSpan? expired = null)
        {
            try
            {
                redisKey = AddKeyPrefix(redisKey);
                return _db.StringGet(redisKey);
            }
            catch (TypeAccessException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 存储一个对象,该对象会被序列化存储
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public bool StringSet<T>(string redisKey, T redisValue, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            var json = Serialize(redisKey);
            return _db.StringSet(redisKey, json, expired);
        }
        /// <summary>
        /// 获取一个对象(会进行反序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public T StringSet<T>(string redisKey, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            return Deserialize<T>(_db.StringGet(redisKey));
        }

        /// <summary>
        /// 保存一个字符串值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public async Task<bool> StringSetAsync(string redisKey, string redisValue, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.StringSetAsync(redisKey, redisValue, expired);
        }
        /// <summary>
        /// 保存一个字符串值
        /// </summary>
        /// <param name="keyValuePairs"></param>
        /// <returns></returns>
        public async Task<bool> StringSetAsync(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs)
        {
            keyValuePairs
                = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value));
            return await _db.StringSetAsync(keyValuePairs.ToArray());
        }
        /// <summary>
        /// 获取单个值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public async Task<string> StringGetAsync(string redisKey, string redisValue, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.StringGetAsync(redisKey);
        }
        /// <summary>
        /// 存储一个对象(该对象会被序列化保存)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public async Task<bool> StringSetAsync<T>(string redisKey, string redisValue, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            var json = Serialize(redisValue);
            return await _db.StringSetAsync(redisKey, json, expired);
        }
        /// <summary>
        /// 获取一个对象(反序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="expired"></param>
        /// <returns></returns>
        public async Task<T> StringGetAsync<T>(string redisKey, string redisValue, TimeSpan? expired = null)
        {
            redisKey = AddKeyPrefix(redisKey);
            return Deserialize<T>(await _db.StringGetAsync(redisKey));
        }
        #endregion

        #region  string operation
        /// <summary>
        /// 判断字段是否在hash中
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        public bool HashExist(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="hashField"></param>
        /// <returns></returns>
        public long HashDelete(string redisKey, IEnumerable<RedisValue> hashField)
        {
            redisKey = AddKeyPrefix(redisKey);
            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)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.HashSet(redisKey, hashField, value);
        }
        /// <summary>
        /// 从Hash 中获取值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public RedisValue HashGet(string redisKey, string hashField)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.HashGet(redisKey, hashField);
        }
        /// <summary>
        /// 从Hash 中获取值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public RedisValue[] HashGet(string redisKey, RedisValue[] hashField)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.HashGet(redisKey, hashField);
        }
        /// <summary>
        /// 从hash 返回所有的key值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public IEnumerable<RedisValue> HashKeys(string redisKey)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.HashKeys(redisKey);
        }
        /// <summary>
        /// 根据key返回hash中的值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public RedisValue[] HashValues(string redisKey)
        {
            redisKey = AddKeyPrefix(redisKey);
            return _db.HashValues(redisKey);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HashSet<T>(string redisKey, string hashField, T value)
        {
            redisKey = AddKeyPrefix(redisKey);
            var json = Serialize(value);
            return _db.HashSet(redisKey, hashField, json);
        }
        /// <summary>
        /// 在hash 中获取值 (反序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <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));
        }
        /// <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="hashField"></param>
        /// <returns></returns>
        public async Task<long> HashDeleteAsync(string redisKey, IEnumerable<RedisValue> hashField)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.HashDeleteAsync(redisKey, hashField.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>
        /// <returns></returns>
        public async Task HashSetAsync(string redisKey, IEnumerable<HashEntry> hashFields)
        {
            redisKey = AddKeyPrefix(redisKey);
            await _db.HashSetAsync(redisKey, hashFields.ToArray());
        }
        /// <summary>
        /// 在hash 中设定值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public async Task<RedisValue> HashGetAsync(string redisKey, string hashField)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.HashGetAsync(redisKey, hashField);
        }
        /// <summary>
        /// 在hash 中获取值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<IEnumerable<RedisValue>> HashGetAsync(string redisKey, RedisValue[] hashField, string value)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.HashGetAsync(redisKey, hashField);
        }
        /// <summary>
        /// 从hash返回所有的字段值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<IEnumerable<RedisValue>> HashKeysAsync(string redisKey)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.HashKeysAsync(redisKey);
        }
        /// <summary>
        /// 返回hash中所有的值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public async Task<IEnumerable<RedisValue>> HashValuesAsync(string redisKey)
        {
            redisKey = AddKeyPrefix(redisKey);
            return await _db.HashValuesAsync(redisKey);
        }
        /// <summary>
        /// 在hash 中设定值(序列化)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#类型获取实例发布时间:2022-07-10
下一篇:
c#——树的深度,广度优先遍历与迭代器(IEnumerableT)的结合使用 ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap