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

        /// 
        /// 构造函数,在其中注册Redis事件
        /// 
        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数据类型操作

        /// 
        /// Redis String类型 新增一条记录
        /// 
        /// generic refrence type
        /// unique key of value
        /// value of key of type T
        /// time span of expiration
        /// true or false
        public bool StringSet(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);
        }

        /// 
        /// Redis String类型 新增一条记录
        /// 
        /// generic refrence type
        /// unique key of value
        /// value of key of type object
        /// time span of expiration
        /// true or false
        public bool StringSet(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);
        }

        /// 
        /// Redis String数据类型 获取指定key中字符串长度
        /// 
        /// 
        /// 
        public long StringLength(string key, CommandFlags commandFlags = CommandFlags.None)
        {
            return _db.StringLength(key, commandFlags);
        }

        /// 
        ///  Redis String数据类型  返回拼接后总长度
        /// 
        /// 
        /// 
        /// 总长度
        public long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None)
        {
            return _db.StringAppend(key, appendVal, commandFlags);
        }

        /// 
        /// 设置新值并且返回旧值
        /// 
        /// 
        /// 
        /// 
        /// OldVal
        public string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None)
        {
            return DeserializeContent(_db.StringGetSet(key, newVal, commandFlags));
        }

        /// 
        /// 更新时应使用此方法,代码更可读。
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool StringUpdate(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);
        }

        /// 
        /// 为数字增长val
        /// 
        /// 
        /// 可以为负
        /// 
        /// 增长后的值
        public double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None)
        {
            return _db.StringIncrement(key, val, commandFlags);
        }

        /// 
        /// Redis String类型  Get
        /// 
        /// 
        /// 
        /// T
        public T StringGet(string key, CommandFlags commandFlags = CommandFlags.None) where T : class
        {
            try
            {
                RedisValue myString = _db.StringGet(key, commandFlags);
                if (myString.HasValue && !myString.IsNullOrEmpty)
                {
                    return DeserializeContent(myString);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                // Log Exception
                return null;
            }
        }

        /// 
        ///  Redis String类型
        /// 类似于模糊查询  key* 查出所有key开头的键
        /// 
        /// 
        /// 
        /// 
        /// 
        /// List
        public List StringGetList(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();
                foreach (var redisValue in keyValues)
                {
                    if (redisValue.HasValue && !redisValue.IsNullOrEmpty)
                    {
                        var item = DeserializeContent(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各数据类型公用

        /// 
        /// Redis中是否存在指定Key
        /// 
        /// 
        /// 
        public bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None)
        {
            return _db.KeyExists(key, commandFlags);
        }

        /// 
        /// Dispose DB connection 释放DB相关链接
        /// 
        public void DbConnectionStop()
        {
            _redis.Dispose();
        }

        /// 
        /// 从Redis中移除键
        /// 
        /// 
        /// 
        public bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None)
        {
            return _db.KeyDelete(key, commandFlags);
        }
        /// 
        /// 从Redis中移除多个键
        /// 
        /// 
        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(RedisValue myString)
        {
            return JsonConvert.DeserializeObject(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日,反思了一下,还是加锁的单例更为合适。

你可能感兴趣的:(StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作))