图片 9

Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

Redis缓存服务器是一款key/value数据库,读1一千0次/s,写八一千次/s,因为是内部存款和储蓄器操作所以速度神速,常见用法有存用户令牌、短信验证码等

转自:

官方网址突显Redis自个儿并不曾Windows版本的,微软官方开拓了基于Windows的Redis服务器:MSOpenTech/redis

Redis缓存服务器是壹款key/value数据库,读110000次/s,写八一千次/s,因为是内部存款和储蓄器操作所以速度高速,常见用法有存用户令牌、短信验证码等

一、Redis服务端

率先下载Redis服务器,点击前往下载.msi版本,双击安装Redis服务端就有了,并以服务的花样随系统一起运转:

图片 1

设置好Redis服务器之后第一件事正是安装密码,进入安装目录:C:\Program
Files\Redis – 找到配置文件:redis.windows-service.conf – 找到:#
requirepass foobared – 回车换行加上:requirepass
这里写自个儿的新密码(顶行写,后边不要留空格)

  • 到服务里重启Redis服务,或许重启计算机

不安装密码的弊病,看看携程那哥俩的饱受就知道了:记1次Redis被攻击的风云

官方网址展现Redis本身并从未Windows版本的,微软官方开辟了根据Windows的Redis服务器:MSOpenTech/redis

二、Redis客户端(命令行和可视化学工业具奥迪Q5DM)

命令行格局演示:运营Redis客户端、读写Redis服务器

图片 2

上海体育场地命令解释:

cd C:\Program
Files\Redis:cd命令进入Redis安装目录,约等于Windows系统里双击进入Redis的设置目录

redis-cli.exe:打开redis-cli客户端程序,也便是Windows系统里双击运维一个exe程序(安装了地点的Redis服务端程序,供给1个客户端程序连接那个服务端。连接本机redis服务器直接敲此命令,连接远程的急需加ip和端口,例:redis-cli.exe
-h 11一.1壹.11.111 -p 637玖)

keys
*:查看全部键值对(假使Redis服务器设置了密码,那条命令会报错,供给先输入密码,实行此命令:auth
你的密码)

set blog
oppoic.cnblogs.com:设置二个键值对,键是:blog,值是:oppoic.cnblogs.com(按目录存款和储蓄:set
目录名:键 值)

get blog:获取键为blog对应的值

keys *:查看全部键值对

别的常用命令:

config get dir:获取redis安装目录

ping:再次来到PONG表示redis服务器常规

redis-cli.exe:进入第3个数据库(私下认可),redis壹共0到一伍共拾八个库,进入第陆个库
redis-cli -n 二(已经进入了,select 0~一伍 随便切换)

quit:退出redis程序

exit:退出dos窗口

flushdb:删除当前选用数据库中的全部key

flushall:删除全部数据库中的数据库

越来越多命令:

 

现今,四个运转在本机的Redis缓存服务器已经搭建完毕,并且能够读写了。然则命令行分明对小白用户不和煦,可视化学工业具上场:Redis
Desktop Manager

图片 3

右边树展现已经有一个老是了,点击尾巴部分的Connect to Redis
Server再增多三个一而再:

Name:连接名称,随意起

Host:主机地址,本机正是127.0.0.一,远程的输入对应IP

Port:端口,Redis服务器默许端口637九

Auth:密码,设置了就输,没安装留空

连上Redis服务器就可以知到,暗中同意十五个库(配置文件可改),索引从0开端。常见用法是3个门类一个库,项目下区别作用模块分不一样目录存在那几个库下。

有了可视化学工业具之后的操作就不要说了,双击,右键新建、删除。。。会用Windows系统的都会用这几个工具。相比于命令行,Redis
Desktop
Manager这一个可视化学工业具更要好,调节和测试远程服务器上的多少也更便于,指哪打哪。

注:本机能够这么,连接远程服务器须求到服务器上的Redis安装目录下,找到redis.windows-service.conf文件,找到bind
1二7.0.0.一 后面加”#”注释掉,然后到劳动里右键重启redis服务

一、Redis服务端

首先下载Redis服务器,点击前往下载.msi版本,双击安装Redis服务端就有了,并以服务的款式随系统同步运转:

图片 1

安装好Redis服务器之后第2件事正是设置密码,进入安装目录:C:\Program
Files\Redis – 找到配置文件:redis.windows-service.conf – 找到:#
requirepass foobared – 回车换行加上:requirepass
这里写自身的新密码(顶行写,前边不要留空格) –
到劳动里重启Redis服务,大概重启电脑

不安装密码的弊病,看看携程那汉子儿的面对就清楚了:记三回Redis被口诛笔伐的轩然大波

三、C#操作Redis服务器

如上都以命令行和可视化学工业具操作Redis服务器,C#次第操作Redis要求重视StackExchange.Redis,为了统一调用,封装了3个RedisHelper协助类:

201柒.0贰.08:RedisHelper.cs代码有修改,请下载小说最终提供的源码

图片 5图片 6

using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Text;
using System.Linq;

namespace redis_Demo
{
    /// <summary>
    /// Redis 帮助类
    /// </summary>
    public static class RedisHelper
    {
        private static string _conn = ConfigurationManager.AppSettings["redis_connection_string"] ?? "127.0.0.1:6379";
        private static string _pwd = ConfigurationManager.AppSettings["redis_connection_pwd"] ?? "123456";
        private static int _store_db = string.IsNullOrEmpty(ConfigurationManager.AppSettings["redis_store_db"]) ? -1 : Convert.ToInt32(ConfigurationManager.AppSettings["redis_store_db"]);

        static ConnectionMultiplexer _redis;
        static readonly object _locker = new object();

        #region 单例模式
        public static ConnectionMultiplexer Manager
        {
            get
            {
                if (_redis == null)
                {
                    lock (_locker)
                    {
                        if (_redis != null) return _redis;
                        _redis = GetManager();
                        return _redis;
                    }
                }
                return _redis;
            }
        }

        private static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = _conn;
            }
            var options = ConfigurationOptions.Parse(connectionString);
            options.Password = _pwd;
            return ConnectionMultiplexer.Connect(options);
        }
        #endregion

        #region 辅助方法
        /// <summary>
        /// 获取要操作的库
        /// </summary>
        /// <param name="db">库,0和-1都是第一个库,1是第二个库...</param>
        /// <returns></returns>
        private static int GetOperationDB(int db)
        {
            if (db == -100)
            {
                return _store_db;
            }
            else
            {
                return db;
            }
        }

        private static string ConvertJson<T>(T value)
        {
            string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
            return result;
        }

        private static T ConvertObj<T>(RedisValue value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return default(T);
            }
            else
            {
                return JsonConvert.DeserializeObject<T>(value);
            }
        }

        private static List<T> ConvetList<T>(RedisValue[] values)
        {
            List<T> result = new List<T>();
            foreach (var item in values)
            {
                var model = ConvertObj<T>(item);
                if (model != null)
                    result.Add(model);
            }
            return result;
        }

        private static RedisKey[] ConvertRedisKeys(List<string> redisKeys, string prefix)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
            }
            else
            {
                return redisKeys.Select(redisKey => (RedisKey)(prefix + ":" + redisKey)).ToArray();
            }
        }

        /// <summary>
        /// 获得枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
        /// <returns>枚举的Description</returns>
        private static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }

            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;
        }
        #endregion

        #region 通用(key)
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static bool KeyExists(CacheFolderEnum folder, string key, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="min">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static bool KeyExpire(CacheFolderEnum folder, string key, int min = 600, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyExpire(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, DateTime.Now.AddMinutes(min));
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 修改键
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="newKey">新键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool KeyRename(CacheFolderEnum folder, string key, string newKey, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyRename(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, string.IsNullOrEmpty(fd) ? newKey : fd + ":" + newKey);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool KeyDelete(CacheFolderEnum folder, string key, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keys">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long KeyDelete(CacheFolderEnum folder, List<string> keys, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyDelete(ConvertRedisKeys(keys, fd));
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion

        #region String
        /// <summary>
        /// 添加单个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expireMinutes">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet(CacheFolderEnum folder, string key, string value, int expireMinutes = 600, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, value, TimeSpan.FromMinutes(expireMinutes));
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keysStr">键</param>
        /// <param name="valuesStr">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet(CacheFolderEnum folder, string[] keysStr, string[] valuesStr, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var count = keysStr.Length;
            var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
            for (int i = 0; i < count; i++)
            {
                keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(string.IsNullOrEmpty(fd) ? keysStr[i] : fd + ":" + keysStr[i], valuesStr[i]);
            }
            return Manager.GetDatabase(db).StringSet(keyValuePair);
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        /// <param name="expireMinutes">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet<T>(CacheFolderEnum folder, string key, T obj, int expireMinutes = 600, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string json = ConvertJson(obj);
            return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, json, TimeSpan.FromMinutes(expireMinutes));
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static string StringGet(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }

        /// <summary>
        /// 获取多个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keys">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static RedisValue[] StringGet(CacheFolderEnum folder, List<string> keys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringGet(ConvertRedisKeys(keys, fd));
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T StringGet<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string value = Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }
        #endregion

        #region List
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">索引开始</param>
        /// <param name="stop">索引结束</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> ListRange<T>(CacheFolderEnum folder, string key, long start = 0, long stop = -1, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListRange(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
            return ConvetList<T>(value);
        }

        /// <summary>
        /// 获取指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="index">索引</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListGetByIndex<T>(CacheFolderEnum folder, string key, long index, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListGetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 替换指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="index">索引</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static void ListSetByIndex<T>(CacheFolderEnum folder, string key, long index, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            Manager.GetDatabase(db).ListSetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index, ConvertJson(value));
        }

        /// <summary>
        /// 删除指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="count">count > 0: Remove elements equal to value moving from head to tail.count 小于 0: Remove elements equal to value moving from tail to head.count = 0: Remove all elements equal to value.</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListRemove<T>(CacheFolderEnum folder, string key, T value, long count = 0, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value), count);
        }

        /// <summary>
        /// 指定位置之后插入
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="pivot">位置</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListInsertAfter<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListInsertAfter(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
        }

        /// <summary>
        /// 指定位置之前插入
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="pivot">位置</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListInsertBefore<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListInsertBefore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
        }

        /// <summary>
        /// 入栈(后插入的在List前面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static long ListLeftPush<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
        }

        /// <summary>
        /// 批量入栈(后插入的在List前面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListLeftPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 出栈(删除最前面的一个元素并返回)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListLeftPop<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListLeftPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 入队(后插入的在List后面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static long ListRightPush<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
        }

        /// <summary>
        /// 批量入队(后插入的在List后面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListRightPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 出队(删除最后面的一个元素并返回)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListRightPop<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListRightPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 获取个数
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListLength(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }
        #endregion

        #region Hash
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="t">实体</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashSet<T>(CacheFolderEnum folder, string key, string dataKey, T t, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey, ConvertJson(t));
        }

        /// <summary>
        /// 获取特定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T HashGet<T>(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 批量获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKeys">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> HashGet<T>(CacheFolderEnum folder, string key, RedisValue[] dataKeys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys);
            return ConvetList<T>(value);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static HashEntry[] HashGetAll<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashGetAll(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }

        /// <summary>
        /// 删除特定
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashDelete(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKeys">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long HashDelete(CacheFolderEnum folder, string key, List<RedisValue> dataKeys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys.ToArray());
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashExists(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
        }
        #endregion

        #region Zset
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="score">排序列</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool SortedSetAdd<T>(CacheFolderEnum folder, string key, T value, double score, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetAdd(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value), score);
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">索引开始</param>
        /// <param name="stop">索引结束</param>
        /// <param name="order">排序方式</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> SortedSetRangeByRank<T>(CacheFolderEnum folder, string key, long start = 0, long stop = -1, Order order = Order.Ascending, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var values = Manager.GetDatabase(db).SortedSetRangeByRank(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop, order);
            return ConvetList<T>(values);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool SortedSetRemove<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value));
        }

        /// <summary>
        /// 批量删除(根据对象)
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">对象</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetRemove<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 批量删除(根据score删除)
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">开始</param>
        /// <param name="stop">结束</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetRemoveRangeByScore(CacheFolderEnum folder, string key, int start, int stop, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetRemoveRangeByScore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetLength(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }
        #endregion

    }
}

View Code

二、Redis客户端(命令行和可视化学工业具陆风X8DM)

命令行方式演示:运行Redis客户端、读写Redis服务器

图片 2

上航海用体育场面命令解释:

cd C:\Program
Files\Redis:cd命令进入Redis安装目录,也正是Windows系统里双击进入Redis的设置目录

redis-cli.exe:打开redis-cli客户端程序,也正是Windows系统里双击运转二个exe程序(安装了地点的Redis服务端程序,须要3个客户端程序连接那几个服务端。连接本机redis服务器直接敲此命令,连接远程的急需加ip和端口,例:redis-cli.exe
-h 111.1一.11.11① -p 6379)

keys
*:查看全部键值对(假如Redis服务器设置了密码,那条命令会报错,供给先输入密码,实行此命令:auth
你的密码)

set blog
oppoic.cnblogs.com:设置二个键值对,键是:blog,值是:oppoic.cnblogs.com(按目录存款和储蓄:set
目录名:键 值)

get blog:获取键为blog对应的值

keys *:查看全数键值对

任何常用命令:

config get dir:获取redis安装目录

ping:重返PONG表示redis服务器常规

redis-cli.exe:进入第3个数据库(默许),redis壹共0到1五共17个库,进入第陆个库
redis-cli -n 2(已经进入了,select 0~一5 随便切换)

quit:退出redis程序

exit:退出dos窗口

flushdb:删除当前选择数据库中的全数key

flushall:删除全数数据库中的数据库

越多命令:

 

由来,一个运营在本机的Redis缓存服务器已经搭建完结,并且能够读写了。不过命令行显明对小白用户不团结,可视化工具进场:Redis
Desktop Manager

图片 3

左侧树展现已经有一个连接了,点击尾巴部分的Connect to Redis
Server再增添三个连连:

Name:连接名称,随意起

Host:主机地址,本机正是1二⑦.0.0.一,远程的输入对应IP

Port:端口,Redis服务器暗许端口637九

Auth:密码,设置了就输,没设置留空

连上Redis服务器就足以看到,默许十八个库(配置文件可改),索引从0初步。常见用法是三个类型2个库,项目下分化效用模块分不一致目录存在这几个库下。

有了可视化学工业具之后的操作就不要说了,双击,右键新建、删除。。。会用Windows系统的都会用这一个工具。相比较于命令行,Redis
Desktop
Manager那么些可视化学工业具更温馨,调节和测试远程服务器上的数量也更有益,指哪打哪。

注:本机能够如此,连接远程服务器必要到服务器上的Redis安装目录下,找到redis.windows-service.conf文件,找到bind
127.0.0.一 后边加”#”注释掉,然后到劳动里右键重启redis服务

String

RedisHelper.StringSet(CacheFolderEnum.Folder1, "string", "hello world", 60);

效果:

图片 9

三、C#操作Redis服务器

以上都以命令行和可视化学工业具操作Redis服务器,C#次第操作Redis须要依赖StackExchange.Redis,为了统一调用,封装了一个RedisHelper扶助类:

20一七.02.08:RedisHelper.cs代码有涂改,请下载作品最后提供的源码

图片 10图片 11

using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Text;
using System.Linq;

namespace redis_Demo
{
    /// <summary>
    /// Redis 帮助类
    /// </summary>
    public static class RedisHelper
    {
        private static string _conn = ConfigurationManager.AppSettings["redis_connection_string"] ?? "127.0.0.1:6379";
        private static string _pwd = ConfigurationManager.AppSettings["redis_connection_pwd"] ?? "123456";
        private static int _store_db = string.IsNullOrEmpty(ConfigurationManager.AppSettings["redis_store_db"]) ? -1 : Convert.ToInt32(ConfigurationManager.AppSettings["redis_store_db"]);

        static ConnectionMultiplexer _redis;
        static readonly object _locker = new object();

        #region 单例模式
        public static ConnectionMultiplexer Manager
        {
            get
            {
                if (_redis == null)
                {
                    lock (_locker)
                    {
                        if (_redis != null) return _redis;
                        _redis = GetManager();
                        return _redis;
                    }
                }
                return _redis;
            }
        }

        private static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = _conn;
            }
            var options = ConfigurationOptions.Parse(connectionString);
            options.Password = _pwd;
            return ConnectionMultiplexer.Connect(options);
        }
        #endregion

        #region 辅助方法
        /// <summary>
        /// 获取要操作的库
        /// </summary>
        /// <param name="db">库,0和-1都是第一个库,1是第二个库...</param>
        /// <returns></returns>
        private static int GetOperationDB(int db)
        {
            if (db == -100)
            {
                return _store_db;
            }
            else
            {
                return db;
            }
        }

        private static string ConvertJson<T>(T value)
        {
            string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
            return result;
        }

        private static T ConvertObj<T>(RedisValue value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return default(T);
            }
            else
            {
                return JsonConvert.DeserializeObject<T>(value);
            }
        }

        private static List<T> ConvetList<T>(RedisValue[] values)
        {
            List<T> result = new List<T>();
            foreach (var item in values)
            {
                var model = ConvertObj<T>(item);
                if (model != null)
                    result.Add(model);
            }
            return result;
        }

        private static RedisKey[] ConvertRedisKeys(List<string> redisKeys, string prefix)
        {
            if (string.IsNullOrEmpty(prefix))
            {
                return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
            }
            else
            {
                return redisKeys.Select(redisKey => (RedisKey)(prefix + ":" + redisKey)).ToArray();
            }
        }

        /// <summary>
        /// 获得枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
        /// <returns>枚举的Description</returns>
        private static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }

            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;
        }
        #endregion

        #region 通用(key)
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static bool KeyExists(CacheFolderEnum folder, string key, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="min">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static bool KeyExpire(CacheFolderEnum folder, string key, int min = 600, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyExpire(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, DateTime.Now.AddMinutes(min));
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 修改键
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="newKey">新键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool KeyRename(CacheFolderEnum folder, string key, string newKey, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyRename(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, string.IsNullOrEmpty(fd) ? newKey : fd + ":" + newKey);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool KeyDelete(CacheFolderEnum folder, string key, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keys">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long KeyDelete(CacheFolderEnum folder, List<string> keys, int db = -100)
        {
            try
            {
                db = GetOperationDB(db);
                string fd = GetDescription(folder);
                return Manager.GetDatabase(db).KeyDelete(ConvertRedisKeys(keys, fd));
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion

        #region String
        /// <summary>
        /// 添加单个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expireMinutes">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet(CacheFolderEnum folder, string key, string value, int expireMinutes = 600, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, value, TimeSpan.FromMinutes(expireMinutes));
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keysStr">键</param>
        /// <param name="valuesStr">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet(CacheFolderEnum folder, string[] keysStr, string[] valuesStr, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var count = keysStr.Length;
            var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
            for (int i = 0; i < count; i++)
            {
                keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(string.IsNullOrEmpty(fd) ? keysStr[i] : fd + ":" + keysStr[i], valuesStr[i]);
            }
            return Manager.GetDatabase(db).StringSet(keyValuePair);
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        /// <param name="expireMinutes">过期时间,单位:分钟</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool StringSet<T>(CacheFolderEnum folder, string key, T obj, int expireMinutes = 600, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string json = ConvertJson(obj);
            return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, json, TimeSpan.FromMinutes(expireMinutes));
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static string StringGet(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }

        /// <summary>
        /// 获取多个
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="keys">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static RedisValue[] StringGet(CacheFolderEnum folder, List<string> keys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).StringGet(ConvertRedisKeys(keys, fd));
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T StringGet<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string value = Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }
        #endregion

        #region List
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">索引开始</param>
        /// <param name="stop">索引结束</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> ListRange<T>(CacheFolderEnum folder, string key, long start = 0, long stop = -1, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListRange(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
            return ConvetList<T>(value);
        }

        /// <summary>
        /// 获取指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="index">索引</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListGetByIndex<T>(CacheFolderEnum folder, string key, long index, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListGetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 替换指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="index">索引</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static void ListSetByIndex<T>(CacheFolderEnum folder, string key, long index, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            Manager.GetDatabase(db).ListSetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index, ConvertJson(value));
        }

        /// <summary>
        /// 删除指定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="count">count > 0: Remove elements equal to value moving from head to tail.count 小于 0: Remove elements equal to value moving from tail to head.count = 0: Remove all elements equal to value.</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListRemove<T>(CacheFolderEnum folder, string key, T value, long count = 0, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value), count);
        }

        /// <summary>
        /// 指定位置之后插入
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="pivot">位置</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListInsertAfter<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListInsertAfter(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
        }

        /// <summary>
        /// 指定位置之前插入
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="pivot">位置</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListInsertBefore<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListInsertBefore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
        }

        /// <summary>
        /// 入栈(后插入的在List前面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static long ListLeftPush<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
        }

        /// <summary>
        /// 批量入栈(后插入的在List前面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListLeftPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 出栈(删除最前面的一个元素并返回)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListLeftPop<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListLeftPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 入队(后插入的在List后面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        public static long ListRightPush<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
        }

        /// <summary>
        /// 批量入队(后插入的在List后面)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListRightPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 出队(删除最后面的一个元素并返回)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T ListRightPop<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).ListRightPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 获取个数
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long ListLength(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).ListLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }
        #endregion

        #region Hash
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="t">实体</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashSet<T>(CacheFolderEnum folder, string key, string dataKey, T t, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey, ConvertJson(t));
        }

        /// <summary>
        /// 获取特定
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static T HashGet<T>(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            string value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
            return ConvertObj<T>(value);
        }

        /// <summary>
        /// 批量获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKeys">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> HashGet<T>(CacheFolderEnum folder, string key, RedisValue[] dataKeys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys);
            return ConvetList<T>(value);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static HashEntry[] HashGetAll<T>(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashGetAll(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }

        /// <summary>
        /// 删除特定
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashDelete(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKeys">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long HashDelete(CacheFolderEnum folder, string key, List<RedisValue> dataKeys, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys.ToArray());
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="dataKey">元素的键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool HashExists(CacheFolderEnum folder, string key, string dataKey, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).HashExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
        }
        #endregion

        #region Zset
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="score">排序列</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool SortedSetAdd<T>(CacheFolderEnum folder, string key, T value, double score, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetAdd(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value), score);
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">索引开始</param>
        /// <param name="stop">索引结束</param>
        /// <param name="order">排序方式</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static List<T> SortedSetRangeByRank<T>(CacheFolderEnum folder, string key, long start = 0, long stop = -1, Order order = Order.Ascending, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var values = Manager.GetDatabase(db).SortedSetRangeByRank(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop, order);
            return ConvetList<T>(values);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static bool SortedSetRemove<T>(CacheFolderEnum folder, string key, T value, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value));
        }

        /// <summary>
        /// 批量删除(根据对象)
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="values">对象</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetRemove<T>(CacheFolderEnum folder, string key, List<T> values, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
            return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
        }

        /// <summary>
        /// 批量删除(根据score删除)
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="start">开始</param>
        /// <param name="stop">结束</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetRemoveRangeByScore(CacheFolderEnum folder, string key, int start, int stop, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetRemoveRangeByScore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="folder">目录</param>
        /// <param name="key">键</param>
        /// <param name="db">库,默认读取配置文件</param>
        /// <returns></returns>
        public static long SortedSetLength(CacheFolderEnum folder, string key, int db = -100)
        {
            db = GetOperationDB(db);
            string fd = GetDescription(folder);
            return Manager.GetDatabase(db).SortedSetLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
        }
        #endregion

    }
}

View Code