C# StackExchange.Redis使用

RedisManage类

管理类,提供Redis管理上下文,此处单例模式实现获取对应的Redis管理器

using StackExchange.Redis;

namespace DataMigrationService
{
    public class RedisManage
    {
        public static readonly ConfigurationOptions ConfigurationOptions = ConfigurationOptions.Parse("127.0.0.1:6379");
        private static readonly object Locker = new object();
        private static ConnectionMultiplexer _redisConn;

        /// <summary>
        /// 单例获取
        /// </summary>
        public static ConnectionMultiplexer RedisConn
        {
            get
            {
                if (_redisConn == null)
                {
                    // 锁定某一代码块,让同一时间只有一个线程访问该代码块
                    lock (Locker)
                    {
                        if (_redisConn == null || !_redisConn.IsConnected)
                        {
                            _redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions);
                        }
                    }
                }
                return _redisConn;
            }
        }
    }
}

RedisBase类

基础操作类,提供数据获取等的相关指令

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

namespace DataMigrationService
{
    public abstract class RedisBase
    {
        public static IDatabase _database;
        public static IServer _server;
        private static int dbIndex = -1;

        public RedisBase()
        {
            _database = RedisManage.RedisConn.GetDatabase(dbIndex);
            _server = RedisManage.RedisConn.GetServer(RedisManage.RedisConn.GetEndPoints().First());
        }

        public RedisBase(int index)
        {
            dbIndex = index;
            _database = RedisManage.RedisConn.GetDatabase(index);
            _server = RedisManage.RedisConn.GetServer(RedisManage.RedisConn.GetEndPoints().First());
        }

        static RedisBase()
        {
            _database = RedisManage.RedisConn.GetDatabase(dbIndex);
            _server = RedisManage.RedisConn.GetServer(RedisManage.RedisConn.GetEndPoints().First());
        }

        /// <summary>
        /// 判断Key值是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exsist(string key)
        {
            return _database.KeyExists(key);
        }

        /// <summary>
        /// key重命名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newKey"></param>
        /// <returns></returns>
        public bool KeyRename(string key, string newKey)
        {
            return _database.KeyRename(key, newKey);
        }

        /// <summary>
        /// 设置缓存过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        public bool SetExpire(string key, DateTime datetime)
        {
            return _database.KeyExpire(key, datetime);
        }

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

        public string GetFirstKey()
        {
            List<string> list = _server.Keys().Select(u => u.ToString()).ToList();
            list.Sort();
            return list.FirstOrDefault();
        }

        /// <summary>
        /// 获取所有的Key值
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllKey()
        {
            List<string> list = _server.Keys().Select(u => u.ToString()).ToList();
            list.Sort();
            return list;
        }

        public string GetFirstKey(string pattern)
        {
            List<string> list = _server.Keys(pattern: pattern).Select(u => u.ToString()).ToList();
            list.Sort();
            return list.FirstOrDefault();
        }

        /// <summary>
        /// 获取所有的Key值(安装pattern匹配获取)
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllKey(string pattern)
        {
            List<string> list = _server.Keys(pattern: pattern).Select(u => u.ToString()).ToList();
            list.Sort();
            return list;
        }
    }
}

RedisList类

List操作类

using DataMigrationService.Extensions;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataMigrationService
{
    public class RedisList : RedisBase
    {
        /// <summary>
        /// 获取并移除Key值上最左边的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string ListLeftPop(string key)
        {
            return _database.ListLeftPop(key).ToString();
        }

        /// <summary>
        /// 移除并返回存储在该键列表的第一个元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public T ListLeftPop<T>(string key)
        {
            return JsonConvert.DeserializeObject<T>(_database.ListLeftPop(key));
        }

        /// <summary>
        /// 获取并移除key值上最右边的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string ListRightPop(string key)
        {
            return _database.ListRightPop(key).ToString();
        }

        /// <summary>
        /// 移除并返回存储在该键列表的最后一个元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public T ListRightPop<T>(string key)
        {
            return JsonConvert.DeserializeObject<T>(_database.ListRightPop(key));
        }

        /// <summary>
        /// 将值插入到key值的最左边
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long ListLeftPush<T>(string key, T value)
        {
            return _database.ListLeftPush(key, JsonConvert.SerializeObject(value));
        }

        /// <summary>
        /// 将值插入到key值的最右边
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        public long ListRightPush<T>(string key, T value)
        {
            return _database.ListRightPush(key, JsonConvert.SerializeObject(value));
        }

        /// <summary>
        /// 返回列表长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListLength(string key)
        {
            return _database.ListLength(key);
        }

        /// <summary>
        /// 获取key值上的第一个数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T ListLeftGetFirst<T>(string key)
        {
            return JsonConvert.DeserializeObject<T>(_database.ListGetByIndex(key, 0));
        }

        /// <summary>
        /// 返回在该列表上键所对应的元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public IEnumerable<RedisValue> ListRange(string key)
        {
            return _database.ListRange(key);
        }

        /// <summary>
        /// 返回在该列表上键所对应的元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public List<T> ListRange<T>(string key)
        {
            List<T> result = new List<T>();
            IEnumerable<RedisValue> list = _database.ListRange(key);
            foreach (RedisValue value in list)
            {
                result.Add(JsonConvert.DeserializeObject<T>(value.ToString()));
            }
            return result;
        }

    }
}

posted @ 2023-09-14 21:08  说不出来  阅读(232)  评论(0编辑  收藏  举报