C#操作Redis

前提在安装Redis的情况下,不在赘述,上代码

NuGet引用Redis、StackExchange.Redis

封装类:

using ServiceStack.Redis;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedisTest
{
    public class RedisCatcheHelper
    {
        private static readonly PooledRedisClientManager pool = null;
        private static readonly string[] redisHosts = null;
        public static int RedisMaxReadPool = 3;
        public static int RedisMaxWritePool = 1;
        static RedisCatcheHelper()
        {
            var redisHostStr = "127.0.0.1:6379";
            if (!string.IsNullOrEmpty(redisHostStr))
            {
                redisHosts = redisHostStr.Split(',');
                if (redisHosts.Length > 0)
                {
                    pool = new PooledRedisClientManager(redisHosts, redisHosts, new RedisClientManagerConfig()
                    {
                        MaxWritePoolSize = RedisMaxWritePool,
                        MaxReadPoolSize = RedisMaxReadPool,
                        AutoStart = true
                    });
                }
            }
        }

        #region Add
        public static void Add(string key, T value, DateTime expiry)
        {
            if (value == null)
                return;
            //if (expiry <= DateTime.Now)
            //{
            //    Remove(key);
            //    return;
            //}
            try
            {
                if (pool != null)
                {
                    using (var r = pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Set(key, value, expiry - DateTime.Now);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"cache:存储发生异常!{key}";
            }
        }

        public static void Add(string key, T value, TimeSpan slidingExpiration)
        {
            if (value == null)
                return;
            if (slidingExpiration.TotalSeconds <= 0)
            {
                Remove(key);
                return;
            }
            try
            {
                if (pool != null)
                {
                    using (var r = pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Set(key, value, slidingExpiration);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"cache:存储发生异常!{key}";
            }

        }

        public static T Get(string key)
        {
            if (string.IsNullOrEmpty(key))
                return default(T);
            T obj = default(T);
            try
            {
                if (pool != null)
                {
                    using (var r = pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            obj = r.Get(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"cache:获取发生异常!{key}";
            }
            return obj;
        }
        #endregion

        public static void Remove(string key)
        {
            try
            {
                if (pool != null)
                {
                    using (var r = pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Remove(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"cache:发生异常!{key}";
            }
        }

        public static bool Exists(string key)
        {
            try
            {
                if (pool != null)
                {
                    using (var r = pool.GetClient())
                    {
                        r.SendTimeout = 1000;
                        bool test = r.ContainsKey(key);
                        return r.ContainsKey(key);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"cache:是否存在发生异常!{key}";
            }
            return false;
        }

    }
}

测试:

RedisCatcheHelper.Add("2", "kk", DateTime.Now);
            bool isExists = RedisCatcheHelper.Exists("2");
            Console.WriteLine($"是否存在当前Key: {isExists}");
            Console.ReadLine();

结果:

C#操作Redis_第1张图片

你可能感兴趣的:(C#)