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

本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 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吧

  1 #region 程序集 RedisRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  2 // Author:吴双  2016.8.28   联系邮箱 wscoder@outlook.com
  3 #endregion
  4 using System;
  5 using System.Collections.Generic;
  6 using StackExchange.Redis;
  7 
  8 namespace RedisRepository
  9 {
 10     public interface IRedisClient
 11     {
 12         #region Redis String类型操作
 13         /// <summary>
 14         /// Redis String类型 新增一条记录
 15         /// </summary>
 16         /// <typeparam name="T">generic refrence type</typeparam>
 17         /// <param name="key">unique key of value</param>
 18         /// <param name="value">value of key of type object</param>
 19         /// <param name="expiresAt">time span of expiration</param>
 20         /// <param name= "when">枚举类型</param>
 21         /// <param name="commandFlags"></param>
 22         /// <returns>true or false</returns>
 23         bool StringSet<T>(string key, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;
 24 
 25         /// <summary>
 26         /// Redis String类型 新增一条记录
 27         /// </summary>
 28         /// <typeparam name="T">generic refrence type</typeparam>
 29         /// <param name="key">unique key of value</param>
 30         /// <param name="value">value of key of type object</param>
 31         /// <param name="expiresAt">time span of expiration</param>
 32         /// <param name= "when">枚举类型</param>
 33         /// <param name="commandFlags"></param>
 34         /// <returns>true or false</returns>
 35         bool StringSet<T>(string key, T value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;
 36 
 37         /// <summary>
 38         /// 更新时应使用此方法,代码更可读。
 39         /// </summary>
 40         /// <typeparam name="T"></typeparam>
 41         /// <param name="key"></param>
 42         /// <param name="value"></param>
 43         /// <param name="expiresAt"></param>
 44         /// <param name="when"></param>
 45         /// <param name="commandFlags"></param>
 46         /// <returns></returns>
 47         bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;
 48 
 49         /// <summary>
 50         /// Redis String类型  Get
 51         /// </summary>
 52         /// <typeparam name="T"></typeparam>
 53         /// <param name="key"></param>
 54         /// <param name="commandFlags"></param>
 55         /// <returns>T</returns>
 56         T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class;
 57 
 58         /// <summary>
 59         /// Redis String数据类型 获取指定key中字符串长度
 60         /// </summary>
 61         /// <param name="key"></param>
 62         /// <param name="commandFlags"></param>
 63         /// <returns></returns>
 64         long StringLength(string key, CommandFlags commandFlags = CommandFlags.None);
 65 
 66         /// <summary>
 67         ///  Redis String数据类型  返回拼接后总长度
 68         /// </summary>
 69         /// <param name="key"></param>
 70         /// <param name="appendVal"></param>
 71         /// <param name="commandFlags"></param>
 72         /// <returns>总长度</returns>
 73         long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None);
 74 
 75         /// <summary>
 76         /// 设置新值并且返回旧值
 77         /// </summary>
 78         /// <param name="key"></param>
 79         /// <param name="newVal"></param>
 80         /// <param name="commandFlags"></param>
 81         /// <returns>OldVal</returns>
 82         string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None);
 83 
 84         /// <summary>
 85         /// 为数字增长val
 86         /// </summary>
 87         /// <param name="key"></param>
 88         /// <param name="val"></param>
 89         /// <param name="commandFlags"></param>
 90         /// <returns>增长后的值</returns>
 91         double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None);
 92 
 93         /// <summary>
 94         /// Redis String数据类型
 95         /// 类似于模糊查询  key* 查出所有key开头的键
 96         /// </summary>
 97         /// <typeparam name="T"></typeparam>
 98         /// <param name="key"></param>
 99         /// <param name="pageSize"></param>
100         /// <param name="commandFlags"></param>
101         /// <returns>返回List<T></returns>
102         List<T> StringGetList<T>(string key, int pageSize = 1000, CommandFlags commandFlags = CommandFlags.None) where T : class;
103         #endregion
104 
105 
106         #region Redis各数据类型公用
107 
108         /// <summary>
109         /// Redis中是否存在指定Key
110         /// </summary>
111         /// <param name="key"></param>
112         /// <param name="commandFlags"></param>
113         /// <returns></returns>
114         bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None);
115 
116         /// <summary>
117         /// 从Redis中移除键
118         /// </summary>
119         /// <param name="key"></param>
120         /// <param name="commandFlags"></param>
121         /// <returns></returns>
122         bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None);
123 
124         /// <summary>
125         /// 从Redis中移除多个键
126         /// </summary>
127         /// <param name="keys"></param>
128         /// <param name="commandFlags"></param>
129         /// <returns></returns>
130         void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None);
131 
132         /// <summary>
133         /// Dispose DB connection 释放DB相关链接
134         /// </summary>
135         void DbConnectionStop();
136         #endregion
137     }
138 }

在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 = 1000, 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日,反思了一下,还是加锁的单例更为合适。

转载于:https://www.cnblogs.com/tdws/p/5815880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值