ASP.NET Core Redis实战

ASP.NET Core 依赖注入  
ASP.NET Core 中的分布式缓存(SQL Server 和 Redis 分布式缓存)  
Redis 模式类型  
Redis 安装、数据类型、模式类型、键(key)命令  
Redis 命令参考(红色)
Redis 命令参考(蓝色)
Redis 数据库学习教程
Redis 官网 

1、服务器安装 Redis 
安装文件及方法,可以参考 Redis 安装、数据类型、模式类型、键(key)命令 文章 

2、Redis 第三方驱动
StackExchange.Redis(免费)
ServiceStack.Redis(商业)
FreeRedis
NewLife.Redis
CSRedisCore

3、Redis 连接

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RedisTestApp
{
    public partial class RedisStringForm : Form
    {
        public static PooledRedisClientManager prcm;
        public static IRedisClientsManager redisClientManager;
        public static IRedisClient redisClient;

        public RedisStringForm()
        {
            InitializeComponent();
        }

        private void RedisStringForm_Load(object sender, EventArgs e)
        {
            //public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[]
            //{
            //    //如果是Redis集群则配置多个{IP地址:端口号}即可
            //    //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
            //    "127.0.0.1:6379"
            //});

            if (prcm == null)
            {
                prcm = CreateManager(
                new string[] { "127.0.0.1:6379" },
                new string[] { "127.0.0.1:6379" });
            }
            prcm.GetClient();
        }

        /// <summary>
        /// 方式一
        /// INCR 增1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.IncrementValue("p");
                client.IncrementValueBy("p", 2);
                label1.Text = client.Get<string>("p");
            }
        }

        /// <summary>
        /// 方式二
        /// 库存操作 INCR 增1,DECR 减1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            using (RedisClient client = new RedisClient("127.0.0.1", 6327))
            {
                //删除“当前”数据库中所有Key,默认删除的是db0
                client.FlushDb();
                //删除“所有”数据库中的Key
                client.FlushAll();

                client.Set<int>("number", 1);
                client.Set<int>("ordernumber", 1);

                var num = client.Get<int>("number");
                if (num>=1)
                {
                    //减一
                    client.Decr("number");
                    //增一
                    client.Incr("number");
                }
            }
        }

        #region redis

        public PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {
            //支持读写分离,均衡负载
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
                new RedisClientManagerConfig
                {
                    MaxWritePoolSize = 5,//“写”链接池链接数
                    MaxReadPoolSize = 5,//“读”链接池链接数
                    AutoStart = true,
                });
        }

        #endregion
    }
}

4、Redis 字符串
client.Add("id", "1")
client.Get<string>("id")
client.Set("id", "100")
client.Replace("id", "0")
client.Remove("id")
client.Incr("number")
client.DecrementValue("number")
client.DecrementValueBy("number", 2)
client.Decr("number")
client.IncrementValue("number")
client.IncrementValueBy("number", 2)
client.SetAll(dict)

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RedisTestApp
{
    public partial class RedisStringForm : Form
    {
        public static PooledRedisClientManager prcm;
        public static IRedisClientsManager redisClientManager;
        public static IRedisClient redisClient;

        public RedisStringForm()
        {
            InitializeComponent();
        }

        private void RedisStringForm_Load(object sender, EventArgs e)
        {
            //public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[]
            //{
            //    //如果是Redis集群则配置多个{IP地址:端口号}即可
            //    //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
            //    "127.0.0.1:6379"
            //});

            if (prcm == null)
            {
                prcm = CreateManager(
                new string[] { "127.0.0.1:6379" },
                new string[] { "127.0.0.1:6379" });
            }
            prcm.GetClient();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.Add("p", "撼地神牛");
                client.Add("p", "影魔");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                label1.Text = client.Get<string>("p");
                var obj = client.As<string>().GetAll();
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.Set("p", "撼地神牛升级版");
                label1.Text = client.Get<string>("p");
            }
        }

        /// <summary>
        /// 替换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.Replace("p", "撼地神牛超神版");
                label1.Text = client.Get<string>("p");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.Remove("p");
                label1.Text = client.Get<string>("p");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.AppendToValue("p", "hhh");
                label1.Text = client.Get<string>("p");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                if (client.Db != 7)
                {
                    ((RedisClient)client).ChangeDb(7);
                }
            }
        }

        /// <summary>
        /// INCR 增1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.IncrementValue("p");
                client.IncrementValueBy("p", 2);
                label1.Text = client.Get<string>("p");
            }
        }

        /// <summary>
        /// DECR 减1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            using (IRedisClient client = prcm.GetClient())
            {
                client.DecrementValue("p");
                client.DecrementValueBy("p", 2);
                label1.Text = client.Get<string>("p");
            }
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            using (RedisClient client = new RedisClient("127.0.0.1", 6327))
            {
                Dictionary<string,string> dict = new Dictionary<string,string>();
                dict.Add("id", "1");
                dict.Add("name", "zhangsan");
                dict.Add("age", "18");

                client.SetAll(dict);

               var list = client.GetAll<string>(new string[] { "id","name","age"});
                foreach (var item in list)
                {
                    Console.WriteLine(item);
                }
            }
        }

        #region redis

        public PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {
            //支持读写分离,均衡负载
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
                new RedisClientManagerConfig
                {
                    MaxWritePoolSize = 5,//“写”链接池链接数
                    MaxReadPoolSize = 5,//“读”链接池链接数
                    AutoStart = true,
                });
        }

        #endregion
    }
}

5、Redis 哈希(Hash)
client.SetEntryInHash("user", "id", "1")
client.GetValueFromHash("user", "id")
client.SetRangeInHash("user", dict)     批量写入
client.GetAllEntriesFromHash("user")  批量读取
client.SetEntryInHashIfNotExists("user", "id", "100")
client.StoreAsHash<User>(new User { Id = "1", Name = "zhangsan" })   写入对象
var name = client.GetFromHash<User>("1").Name                                  读取对象
client.RemoveEntryFromHash("user", "id")                                               删除
client.IncrementValueInHash("user", "id", 100)                                         Hash自增

6、Redis 列表(List)
client.AddItemToList("中国", "北京")                  顺序添加
client.PushItemToList("中国", "上海")                左侧插入
client.PrependItemToList("中国", "深圳")          右侧插入
client.AddRangeToList("中国", new List<string>() { "1", "2", "3" })  批量插入
client.PopItemFromList("蜀国")            尾部删除
client.RemoveStartFromList("中国")    头部移除,并返回移除的值
client.RemoveEndFromList("中国")      尾部移除,并返回移除的值
client.PopAndPushItemBetweenLists("fromKey", "toKey")    从一个list的尾部移除一个数据,添加到另外一个list的头部,并返回移动的值
client.SetItemInList("中国", 0, "北京北京")     根据集合下标修改内容
client.GetListCount("中国")
client.ExpireEntryAt("中国", DateTime.Now.AddSeconds(2))  过期时间

7、Redis 集合(Set) 自动去重
client.AddItemToSet("中国", "北京")
client.AddItemToSet("user", JsonConvert.SerializeObject(new User { Id = "1", Name = "libai" }));
client.AddRangeToSet("中国", new List<string>() { "北京", "上海", "深圳" })    批量添加
client.GetAllItemsFromSet("中国")                 查询
client.GetRandomItemFromSet("中国")         随机获取
client.PopItemFromSet("中国")                      随机删除,并返回移除的值(先进先出)
client.RemoveItemFromSet("中国", "北京")   指定删除
client.GetIntersectFromSets("集合A", "集合B")         交集
client.GetUnionFromSets("集合A", "集合B")             并集

8、Redis 有序集合(sorted set) 自动去重,权重分数,自动排序
client.AddItemToSortedSet("中国", "北京", 100)
client.AddItemToSortedSet("中国", "上海", 90);
client.AddRangeToSet("中国", new List<string>() { "北京", "上海", "深圳" });
client.GetAllItemsFromSortedSet("中国")            正序查询
client.GetAllItemsFromSortedSetDesc("中国")    倒叙查询
client.GetRangeFromSortedSet("中国", 0, 1)                     根据下标获取,不返回分数
client.GetRangeWithScoresFromSortedSet("中国", 0, 1)   根据下标获取,返回分数
client.StoreIntersectFromSortedSets("存放交集的集合", "集合A", "集合B")
*
9、appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "Default": "data source=.;initial catalog=dbTest;user id=sa;password=123456;TrustServerCertificate=True;"
  },
  "Redis": {
    "ConnectionString": "127.0.0.1",
    "ConnectionString1": "localhost",
    "ConnectionString2": "47.98.226.195,abortConnect=false",
    "Port": "6379",
    "Password": "123456",
    "InstanceName": "Demo",
    "DefaultDb": 0,
    "Prefix": "NM",
    "Provider": "StackExchange",
    "EnableLogging": true,
    "SerializerName": "binary",
    "Timeout": 30,
    "Enabled": "true"
  }
}

10、ICacheHandler.cs

using System;
using System.Threading.Tasks;

namespace Lib.Cache.Abstractions
{
    /// <summary>
    /// 缓存处理器
    /// </summary>
    public interface ICacheHandler
    {
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        string Get(string key);

        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        Task<string> GetAsync(string key);

        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string key);

        /// <summary>
        /// 尝试获取
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">返回值</param>
        /// <returns></returns>
        bool TryGetValue(string key, out string value);

        /// <summary>
        /// 尝试获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">返回值</param>
        /// <returns></returns>
        bool TryGetValue<T>(string key, out T value);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        bool Set<T>(string key, T value);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expires">有效期(分钟)</param>
        bool Set<T>(string key, T value, int expires);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expiry">时间间隔</param>
        bool Set<T>(string key, T value, TimeSpan expiry);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        Task<bool> SetAsync<T>(string key, T value);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expires">有效期(分钟)</param>
        /// <returns></returns>
        Task<bool> SetAsync<T>(string key, T value, int expires);

        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expiry">时间间隔</param>
        /// <returns></returns>
        Task<bool> SetAsync<T>(string key, T value, TimeSpan expiry);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        bool Remove(string key);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> RemoveAsync(string key);

        /// <summary>
        /// 指定键是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool Exists(string key);

        /// <summary>
        /// 指定键是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> ExistsAsync(string key);

        /// <summary>
        /// 删除指定前缀的缓存
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        Task RemoveByPrefixAsync(string prefix);
    }
}

*、MemoryCacheHandler.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using NetModular.Lib.Cache.Abstractions;

namespace Lib.Cache.MemoryCache
{
    public class MemoryCacheHandler : ICacheHandler
    {
        private readonly IMemoryCache _cache;

        public MemoryCacheHandler(IMemoryCache cache)
        {
            _cache = cache;
        }

        public string Get(string key)
        {
            var value = _cache.Get(key);
            return value?.ToString();
        }

        public T Get<T>(string key)
        {
            return _cache.Get<T>(key);
        }

        public Task<string> GetAsync(string key)
        {
            var value = Get(key);
            return Task.FromResult(value);
        }

        public Task<T> GetAsync<T>(string key)
        {
            return Task.FromResult(_cache.Get<T>(key));
        }

        public bool TryGetValue(string key, out string value)
        {
            return _cache.TryGetValue(key, out value);
        }

        public bool TryGetValue<T>(string key, out T value)
        {
            return _cache.TryGetValue(key, out value);
        }

        public bool Set<T>(string key, T value)
        {
            _cache.Set(key, value);
            return true;
        }

        public bool Set<T>(string key, T value, int expires)
        {
            Set(key, value, new TimeSpan(0, 0, expires, 0));
            return true;
        }

        public bool Set<T>(string key, T value, TimeSpan expiry)
        {
            _cache.Set(key, value, expiry);
            return true;
        }

        public Task<bool> SetAsync<T>(string key, T value)
        {
            Set(key, value);
            return Task.FromResult(true);
        }

        public Task<bool> SetAsync<T>(string key, T value, int expires)
        {
            Set(key, value, expires);
            return Task.FromResult(true);
        }

        public Task<bool> SetAsync<T>(string key, T value, TimeSpan expiry)
        {
            Set(key, value, expiry);
            return Task.FromResult(true);
        }

        public bool Remove(string key)
        {
            _cache.Remove(key);
            return true;
        }

        public Task<bool> RemoveAsync(string key)
        {
            _cache.Remove(key);
            return Task.FromResult(true);
        }

        public bool Exists(string key)
        {
            return _cache.TryGetValue(key, out _);
        }

        public Task<bool> ExistsAsync(string key)
        {
            return Task.FromResult(_cache.TryGetValue(key, out _));
        }

        public async Task RemoveByPrefixAsync(string prefix)
        {
            if (prefix.IsNull())
                return;

            var keys = GetAllKeys().Where(m => m.StartsWith(prefix));
            foreach (var key in keys)
            {
                await RemoveAsync(key);
            }
        }

        private List<string> GetAllKeys()
        {
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var entries = _cache.GetType().GetField("_entries", flags).GetValue(_cache);
            var cacheItems = entries as IDictionary;
            var keys = new List<string>();
            if (cacheItems == null) return keys;
            foreach (DictionaryEntry cacheItem in cacheItems)
            {
                keys.Add(cacheItem.Key.ToString());
            }
            return keys;
        }
    }
}

*、RedisCacheHandler.cs

using System;
using System.Threading.Tasks;
using NetModular.Lib.Cache.Abstractions;

namespace Lib.Cache.Redis
{
    public class RedisCacheHandler : ICacheHandler
    {
        private readonly RedisHelper _helper;

        public RedisCacheHandler(RedisHelper helper)
        {
            _helper = helper;
        }

        public string Get(string key)
        {
            return _helper.StringGetAsync<string>(key).GetAwaiter().GetResult();
        }

        public T Get<T>(string key)
        {
            return _helper.StringGetAsync<T>(key).GetAwaiter().GetResult();
        }

        public Task<string> GetAsync(string key)
        {
            return _helper.StringGetAsync<string>(key);
        }

        public Task<T> GetAsync<T>(string key)
        {
            return _helper.StringGetAsync<T>(key);
        }

        public bool TryGetValue(string key, out string value)
        {
            value = null;
            if (Exists(key))
            {
                value = Get(key);
                return true;
            }

            return false;
        }

        public bool TryGetValue<T>(string key, out T value)
        {
            value = default;
            if (Exists(key))
            {
                value = Get<T>(key);
                return true;
            }

            return false;
        }

        public bool Set<T>(string key, T value)
        {
            return _helper.StringSetAsync(key, value).GetAwaiter().GetResult();
        }

        public bool Set<T>(string key, T value, int expires)
        {
            return SetAsync(key, value, expires).GetAwaiter().GetResult();
        }

        public bool Set<T>(string key, T value, TimeSpan expiry)
        {
            return SetAsync(key, value, expiry).GetAwaiter().GetResult();
        }

        public Task<bool> SetAsync<T>(string key, T value)
        {
            return _helper.StringSetAsync(key, value);
        }

        public Task<bool> SetAsync<T>(string key, T value, int expires)
        {
            return SetAsync(key, value, new TimeSpan(0, 0, expires, 0));
        }

        public Task<bool> SetAsync<T>(string key, T value, TimeSpan expiry)
        {
            return _helper.StringSetAsync(key, value, expiry);
        }

        public bool Remove(string key)
        {
            return _helper.KeyDelete(key);
        }

        public Task<bool> RemoveAsync(string key)
        {
            return _helper.KeyDeleteAsync(key);
        }

        public bool Exists(string key)
        {
            return _helper.KeyExists(key);
        }

        public Task<bool> ExistsAsync(string key)
        {
            return _helper.KeyExistsAsync(key);
        }

        public Task RemoveByPrefixAsync(string prefix)
        {
            return _helper.DeleteByPrefix(prefix);
        }
    }
}

*、RedisHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NetModular.Lib.Cache.Abstractions;
using StackExchange.Redis;
using NetModular.Lib.Utils.Core.Attributes;

namespace NetModular.Lib.Cache.Redis
{
    [Singleton]
    public class RedisHelper : IDisposable
    {
        internal ConnectionMultiplexer _redis;
        internal string _prefix;
        internal readonly RedisConfig _config;
        internal readonly IRedisSerializer _redisSerializer;
        public IDatabase Db;
        public RedisDatabase Database;

        public RedisHelper(CacheConfig config, IRedisSerializer redisSerializer)
        {
            _redisSerializer = redisSerializer;
            _config = config.Redis;
            CreateConnection();
        }

        /// <summary>
        /// 获取Redis原生的IDatabase
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public IDatabase GetDb(int db = -1)
        {
            if (db == -1)
                db = _config.DefaultDb;

            return _redis.GetDatabase(db);
        }

        /// <summary>
        /// 获取自定义的RedisDatabase
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public RedisDatabase GetDatabase(int db = -1)
        {
            if (db == -1)
                db = _config.DefaultDb;

            return new RedisDatabase(db, this);
        }

        /// <summary>
        /// 获取键(附加前缀)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetKey(string key)
        {
            return $"{_prefix}{key}";
        }

        /// <summary>
        /// 创建连接
        /// </summary>
        internal void CreateConnection()
        {
            _prefix = _config.Prefix;
            _redis = ConnectionMultiplexer.Connect(_config.ConnectionString);
            Db = GetDb();
            Database = GetDatabase();
        }

        /// <summary>
        /// 获取Redis连接对象
        /// </summary>
        public ConnectionMultiplexer Conn
        {
            get
            {
                if (_redis == null)
                {
                    CreateConnection();
                }

                return _redis;
            }
        }

        #region ==String==

        /// <summary>
        /// 写入字符串类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public Task<bool> StringSetAsync<T>(string key, T obj, TimeSpan? expiry = null)
        {
            return Database.StringSetAsync(key, obj, expiry);
        }

        /// <summary>
        /// 获取字符串类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task<T> StringGetAsync<T>(string key)
        {
            return Database.StringGetAsync<T>(key);
        }

        /// <summary>
        /// 字符串减去数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<long> StringDecrementAsync(string key, long value = 1)
        {
            return Database.StringDecrementAsync(key, value);
        }

        /// <summary>
        /// 字符串增加数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<long> StringIncrementAsync(string key, long value = 1)
        {
            return Database.StringIncrementAsync(key, value);
        }

        #endregion

        #region ==Hash==

        /// <summary>
        /// 设置值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Task<bool> HashSetAsync<T>(string key, string field, T obj)
        {
            return Database.HashSetAsync(key, field, obj);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public Task<T> HashGetAsync<T>(string key, string field)
        {
            return Database.HashGetAsync<T>(key, field);
        }

        /// <summary>
        /// 获取所有值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task<IList<T>> HashValuesAsync<T>(string key)
        {
            return Database.HashValuesAsync<T>(key);
        }

        /// <summary>
        /// 删除值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public Task<bool> HashDeleteAsync(string key, string field)
        {
            return Database.HashDeleteAsync(key, field);
        }

        /// <summary>
        /// 获取所有键值集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task<IList<KeyValuePair<string, T>>> HashGetAllAsync<T>(string key)
        {
            return Database.HashGetAllAsync<T>(key);
        }

        /// <summary>
        /// 数值减
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<long> HashDecrementAsync(string key, string field, long value = 1)
        {
            return Database.HashDecrementAsync(key, field, value);
        }

        /// <summary>
        /// 数值加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<long> HashIncrementAsync(string key, string field, long value = 1)
        {
            return Database.HashIncrementAsync(key, field, value);
        }

        /// <summary>
        /// 判断字段是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public Task<bool> HashExistsAsync(string key, string field)
        {
            return Database.HashExistsAsync(key, field);
        }

        #endregion

        #region ==Set==

        public Task<bool> SetAddAsync<T>(string key, T obj)
        {
            return Database.SetAddAsync(key, obj);
        }

        public Task<bool> SetRemoveAsync<T>(string key, T obj)
        {
            return Database.SetRemoveAsync(key, obj);
        }

        public Task<bool> SetContainsAsync<T>(string key, T obj)
        {
            return Database.SetContainsAsync(key, obj);
        }

        public Task<long> SetLengthAsync(string key)
        {
            return Database.SetLengthAsync(key);
        }

        public Task<T> SetPopAsync<T>(string key)
        {
            return Database.SetPopAsync<T>(key);
        }

        public Task<IList<T>> SetMembersAsync<T>(string key)
        {
            return Database.SetMembersAsync<T>(key);
        }

        #endregion

        #region ==Sorted Set==

        /// <summary>
        /// 添加有序集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public Task<bool> SortedSetAddAsync<T>(string key, T member, double score)
        {
            return Database.SortedSetAddAsync(key, member, score);
        }

        /// <summary>
        /// 减去值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<double> SortedSetDecrementAsync<T>(string key, T member, double value)
        {
            return Database.SortedSetDecrementAsync(key, member, value);
        }

        /// <summary>
        /// 增加值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<double> SortedSetIncrementAsync<T>(string key, T member, double value)
        {
            return Database.SortedSetIncrementAsync(key, member, value);
        }

        /// <summary>
        /// 获取排名的成员
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public Task<IList<T>> SortedSetRangeByRankAsync<T>(string key, long start = 0, long stop = -1, Order order = Order.Ascending)
        {
            return Database.SortedSetRangeByRankAsync<T>(key, start, stop, order);
        }

        /// <summary>
        /// 获取排名的成员和值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public Task<IList<KeyValuePair<T, double>>> SortedSetRangeByRankWithScoresAsync<T>(string key, long start = 0, long stop = -1, Order order = Order.Ascending)
        {
            return Database.SortedSetRangeByRankWithScoresAsync<T>(key, start, stop, order);
        }

        /// <summary>
        /// 返回指定分数区间的成员数量
        /// </summary>
        /// <param name="key"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public Task<long> SortedSetLengthAsync(string key, double min = double.NegativeInfinity, double max = double.PositiveInfinity)
        {
            return Database.SortedSetLengthAsync(key, min, max);
        }

        /// <summary>
        /// 删除并返回第一个元素,默认情况下,分数是从低到高排序的。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public Task<KeyValuePair<T, double>> SortedSetPopAsync<T>(string key, Order order = Order.Ascending)
        {
            return Database.SortedSetPopAsync<T>(key, order);
        }

        /// <summary>
        /// 删除指定成员
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public Task<bool> SortedSetRemoveAsync<T>(string key, T member)
        {
            return Database.SortedSetRemoveAsync(key, member);
        }

        /// <summary>
        /// 根据分数区间删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns>删除数量</returns>
        public Task<long> SortedSetRemoveRangeByScoreAsync(string key, long start = 0, long stop = -1)
        {
            return Database.SortedSetRemoveRangeByScoreAsync(key, start, stop);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns>删除数量</returns>
        public Task<long> SortedSetRemoveRangeByRankAsync(string key, long start = 0, long stop = -1)
        {
            return Database.SortedSetRemoveRangeByRankAsync(key, start, stop);
        }

        #endregion

        #region ==KeyDelete==

        /// <summary>
        /// 删除键
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyDelete(string key)
        {
            return Database.KeyDelete(key);
        }

        /// <summary>
        /// 删除键
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task<bool> KeyDeleteAsync(string key)
        {
            return Database.KeyDeleteAsync(key);
        }

        #endregion

        #region ==KeyExists==

        /// <summary>
        /// 是否存在键
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {
            return Database.KeyExists(key);
        }

        /// <summary>
        /// 是否存在键
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Task<bool> KeyExistsAsync(string key)
        {
            return Database.KeyExistsAsync(key);
        }

        #endregion

        #region ==KeyExpire==

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public bool KeyExpire(string key, DateTime? expiry)
        {
            return Database.KeyExpire(key, expiry);
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public Task<bool> KeyExpireAsync(string key, DateTime? expiry)
        {
            return Database.KeyExpireAsync(key, expiry);
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public bool KeyExpire(string key, TimeSpan? expiry)
        {
            return Database.KeyExpire(key, expiry);
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public Task<bool> KeyExpireAsync(string key, TimeSpan? expiry)
        {
            return Database.KeyExpireAsync(key, expiry);
        }

        #endregion

        #region ==Other==

        /// <summary>
        /// 分页获取所有Keys
        /// </summary>
        /// <param name="database"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageOffset"></param>
        /// <returns></returns>
        public IList<RedisKey> GetAllKeys(int database = 0, int pageSize = 10, int pageOffset = 0)
        {
            return _redis.GetServer(_redis.GetEndPoints()[0]).Keys(database, pageSize: pageSize, pageOffset: pageOffset).ToList();
        }

        /// <summary>
        /// 分页获取指定前缀的Keys列表
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="database"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageOffset"></param>
        /// <returns></returns>
        public IList<RedisKey> GetKeysByPrefix(string prefix, int database = 0, int pageSize = 10, int pageOffset = 0)
        {
            if (prefix.IsNull())
                return null;

            return _redis.GetServer(_redis.GetEndPoints()[0]).Keys(database, $"{GetKey(prefix)}*", pageSize, pageOffset).ToList();
        }

        /// <summary>
        /// 删除指定前缀的Keys
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public Task DeleteByPrefix(string prefix)
        {
            return Database.DeleteByPrefix(prefix);
        }

        public void Dispose()
        {
            _redis?.Dispose();
        }

        #endregion
    }
}

*、RedisConfig.cs

namespace Lib.Cache.Abstractions
{
    /// <summary>
    /// 缓存配置
    /// </summary>
    public class CacheConfig
    {
        /// <summary>
        /// 缓存提供器
        /// </summary>
        public CacheProvider Provider { get; set; } = CacheProvider.MemoryCache;

        /// <summary>
        /// Redis配置
        /// </summary>
        public RedisConfig Redis { get; set; } = new RedisConfig();
    }

    public class RedisConfig
    {
        private int _defaultDb;

        /// <summary>
        /// 默认数据库
        /// </summary>
        public int DefaultDb
        {
            get => _defaultDb < 0 ? 0 : _defaultDb;
            set => _defaultDb = value;
        }

        private string _prefix;

        /// <summary>
        /// 前缀,用于区分不用的项目
        /// </summary>
        public string Prefix
        {
            get => _prefix.NotNull() ? $"{_prefix}:" : string.Empty;
            set => _prefix = value;
        }

        private string _connnectionString = "127.0.0.1";
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString
        {
            get => _connnectionString.NotNull() ? _connnectionString : "127.0.0.1";
            set => _connnectionString = value;
        }

    }
}

*、RedisOptions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lib.Cache.Abstractions
{
    public class ServiceStackRedisOptions
    {
        /// <summary>
        /// 单机的地址,例如:127.0.0.1:6379(默认值)。如果你只用到一个Redis服务端,那么配置此项即可。
        /// </summary>
        public string SingleServer { get; set; } = "127.0.0.1:6379";

        /// <summary>
        /// 读写的地址,例如:{ "192.168.1.1:6379","123456@192.168.1.2:6379","123456@192.168.1.3:6379","123456@192.168.1.4:6379" }
        /// </summary>
        public string[] ReadWriteServers { get; set; }

        /// <summary>
        /// 只读地址,例如:{ "192.168.1.1:6379","123456@192.168.1.3:6379" }
        /// </summary>
        public string[] ReadOnlyServers { get; set; }

        /// <summary>
        /// MaxWritePoolSize写的频率比读低。默认值 8
        /// </summary>
        public int MaxWritePoolSize { get; set; } = 8;

        /// <summary>
        /// MaxReadPoolSize读的频繁比较多。默认值 12,Redis官方声明最大连接数为1W,但是连接数要控制。
        /// </summary>
        public int MaxReadPoolSize { get; set; } = 12;

        /// <summary>
        /// 连接最大的空闲时间。默认值 60,Redis官方默认是240
        /// </summary>
        public int IdleTimeOutSecs { get; set; } = 60;

        /// <summary>
        /// 连接超时时间,毫秒。默认值 6000
        /// </summary>
        public int ConnectTimeout { get; set; } = 6000;

        /// <summary>
        /// 数据发送超时时间,毫秒。默认值 6000
        /// </summary>
        public int SendTimeout { get; set; } = 6000;

        /// <summary>
        /// 数据接收超时时间,毫秒。默认值 6000
        /// </summary>
        public int ReceiveTimeout { get; set; } = 6000;

        /// <summary>
        /// 连接池取链接的超时时间,毫秒。默认值 6000
        /// </summary>
        public int PoolTimeout { get; set; } = 6000;

        /// <summary>
        /// 默认的数据库。默认值 0,Redis官方默认也是0
        /// </summary>
        public long DefaultDb { get; set; } = 0;
    }
}

、缓存相关常量【前缀】

namespace Admin.NET.Core;

/// <summary>
/// 缓存相关常量
/// </summary>
public class CacheConst
{
    /// <summary>
    /// 用户缓存
    /// </summary>
    public const string KeyUser = "sys_user:";

    / <summary>
    / 用户菜单缓存
    / </summary>
    //public const string KeyUserMenu = "sys_user_menu:";

    /// <summary>
    /// 用户权限缓存(按钮集合)
    /// </summary>
    public const string KeyUserButton = "sys_user_button:";

    /// <summary>
    /// 用户机构缓存
    /// </summary>
    public const string KeyUserOrg = "sys_user_org:";

    /// <summary>
    /// 角色最大数据范围缓存
    /// </summary>
    public const string KeyRoleMaxDataScope = "sys_role_maxDataScope:";

    /// <summary>
    /// 在线用户缓存
    /// </summary>
    public const string KeyUserOnline = "sys_user_online:";

    /// <summary>
    /// 图形验证码缓存
    /// </summary>
    public const string KeyVerCode = "sys_verCode:";

    // 手机验证码缓存
    public const string KeyPhoneVerCode = "sys_phoneVerCode:";

    /// <summary>
    /// 租户缓存
    /// </summary>
    public const string KeyTenant = "sys_tenant";

    /// <summary>
    /// 常量下拉框
    /// </summary>
    public const string KeyConst = "sys_const:";

    /// <summary>
    /// 所有缓存关键字集合
    /// </summary>
    public const string KeyAll = "sys_keys";

    /// <summary>
    /// SqlSugar二级缓存
    /// </summary>
    public const string SqlSugar = "sys_sqlSugar:";

    /// <summary>
    /// 开放接口身份缓存
    /// </summary>
    public const string KeyOpenAccess = "sys_open_access:";

    /// <summary>
    /// 开放接口身份随机数缓存
    /// </summary>
    public const string KeyOpenAccessNonce = "sys_open_access_nonce:";
}

*
*
*
*
*

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值