• 3种不同Redis插件对比


    先说结论:推荐使用 【CSRedisCore】

    原因:①号称Redis官方推荐的插件 ②功能应该是最全的 ③注释完美

    ServiceStack.Redis:

    已经商业化,对请求有上限

    StackExchange.Redis:

    有时会出现连接TimeOut的错误,找不到原因

    ------------------------------------------------------分割线来了----------------------------------------------------------

    接口

    复制代码
    public interface IRedisHelper
        {
            bool SetString(string key,string value);
    
            string GetString(string key);
    
            void DeleteKey(string key);
    
            void LPush(string key, string value);
    
            void RPush(string key, string value);
    
            string LPop(string key);
    
            string RPop(string key);
    
            void SAdd(string key, string value);
    
            string SPop(string key);
        }
    复制代码

    【CSRedisCore】

    复制代码
    public class CsRedisHelper : IRedisHelper
        {
            private static CSRedisClient _redisClient = new CSRedisClient("127.0.0.1:6379,password=jiang123");
    
            public bool SetString(string key, string value)
            {
                return _redisClient.Set(key,value);
            }
    
            public string GetString(string key)
            {
                return _redisClient.Get(key);
            }
    
            public void LPush(string key, string value)
            {
                _redisClient.LPush(key,value);
            }
    
            public void RPush(string key, string value)
            {
                _redisClient.RPush(key,value);
            }
    
            public string LPop(string key)
            {
                return _redisClient.LPop(key);
            }
    
            public string RPop(string key)
            {
                return _redisClient.RPop(key);
            }
    
            public void SAdd(string key, string value)
            {
                _redisClient.SAdd(key,value);
            }
    
            public string SPop(string key)
            {
                return _redisClient.SPop(key);
            }
    
            public bool Set<T>(string key,T value)
            {
                return _redisClient.Set(key,value);
            }
    
            public T Get<T>(string key)
            {
                return _redisClient.Get<T>(key);
            }
    
            public void DeleteKey(string key)
            {
                _redisClient.Del(key);
            }
        }
    复制代码

    【ServiceStack.Redis】

    复制代码
    public class ServiceStackRedisHelper : IRedisHelper
        {
            private static readonly RedisClient _redisClient = new RedisClient("127.0.0.1", 6379, "jiang123",0);
    
            public bool SetString(string key, string value)
            {
                return _redisClient.Set(key,value);
            }
    
            public string GetString(string key)
            {
                return _redisClient.Get<string>(key);
            }
    
            public void LPush(string key, string value)
            {
                _redisClient.LPush(key, System.Text.Encoding.Default.GetBytes(value));
            }
    
            public void RPush(string key, string value)
            {
                _redisClient.RPush(key, System.Text.Encoding.Default.GetBytes(value));
            }
    
            public string LPop(string key)
            {
                return System.Text.Encoding.Default.GetString(_redisClient.LPop(key));
            }
    
            public string RPop(string key)
            {
                return System.Text.Encoding.Default.GetString(_redisClient.RPop(key));
            }
    
            public void SAdd(string key, string value)
            {
                _redisClient.SAdd(key, System.Text.Encoding.Default.GetBytes(value));
            }
    
            public string SPop(string key)
            {
                return System.Text.Encoding.Default.GetString(_redisClient.SPop(key));
            }
    
            public void Set<T>(string key, T value)
            {
                _redisClient.Set(key,value);
            }
    
            public T Get<T>(string key)
            {
                return _redisClient.Get<T>(key);
            }
    
            public void DeleteKey(string key)
            {
                _redisClient.Delete(key);
            }
        }
    复制代码

    【StackExchange.Redis】

    复制代码
    public class StackExchangeRedisHelper : IRedisHelper
        {
            private IDatabase _redisClient = ConnectionMultiplexer.Connect("172.30.37.23:6379,password=jiang123").GetDatabase(0);
    
            public bool SetString(string key, string value)
            {
                return _redisClient.StringSet(key, value);
            }
    
            public string GetString(string key)
            {
                return _redisClient.StringGet(key);
            }
    
            public void LPush(string key,string value)
            {
                _redisClient.ListLeftPush(key, value);
            }
    
            public void RPush(string key, string value)
            {
                _redisClient.ListRightPush(key,value);
            }
    
            public string LPop(string key)
            {
                return _redisClient.ListLeftPop(key);
            }
    
            public string RPop(string key)
            {
                return _redisClient.ListRightPop(key);
            }
    
            public void SAdd(string key, string value)
            {
                _redisClient.SetAdd(key,value);
            }
    
            public string SPop(string key)
            {
                return _redisClient.SetPop(key);
            }
    
            public void Set<T>(string key,T t)
            {
                _redisClient.StringSet(key,JsonConvert.SerializeObject(t));
            }
    
            public T Get<T>(string key)
            {
                return JsonConvert.DeserializeObject<T>(_redisClient.StringGet(key));
            }
    
            public void DeleteKey(string key)
            {
                _redisClient.KeyDelete(key);
            }
        }
    复制代码

    【CSRedisCore】缺点:貌似无明显缺点

    【ServiceStack.Redis】缺点:感觉字节数组与字符串的转化不必要

    【StackExchange.Redis】缺点:对泛型支持不够

  • 相关阅读:
    项目包结构
    准备dbcp2-2.1.1和pool2-2.4.2 、commons-dbcp-1.4jar包
    导入javax.servlet。伺服登记无法解决:The import javax.servlet.MultipartConfigElement cannot be resolved
    准备mysql-connector-java
    准备mybatis-spring
    准备spring
    准备MyBatis
    vim编辑器使用
    jquery怎样做出分页效果
    快速入门系列--WCF--02消息、会话与服务寄宿
  • 原文地址:https://www.cnblogs.com/nnnnnn/p/14131729.html
Copyright © 2020-2023  润新知