Login
网站首页 > 文章中心 > 其它

.Net Core之Redis插件对比【CSRedisCore】【ServiceStack.Redis】【StackExchange.Redis】

作者:小编 更新时间:2023-10-01 10:41:00 浏览量:455人看过

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

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

接口


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("12⑦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(string key,T value)
    {
        return _redisClient.Set(key,value);
    }

    public T Get(string key)
    {
        return _redisClient.Get(key);
    }

    public void DeleteKey(string key)
    {
        _redisClient.Del(key);
    }
}

【ServiceStack.Redis】


public class ServiceStackRedisHelper : IRedisHelper
{
    private static readonly RedisClient _redisClient = new RedisClient("12⑦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(string key, T value)
    {
        _redisClient.Set(key,value);
    }

    public T Get(string key)
    {
        return _redisClient.Get(key);
    }

    public void DeleteKey(string key)
    {
        _redisClient.Delete(key);
    }
}

【StackExchange.Redis】


public class StackExchangeRedisHelper : IRedisHelper
{
    private IDatabase _redisClient = ConnectionMultiplexer.Connect("172.30.3⑦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(string key,T t)
    {
        _redisClient.StringSet(key,JsonConvert.SerializeObject(t));
    }

    public T Get(string key)
    {
        return JsonConvert.DeserializeObject(_redisClient.StringGet(key));
    }

    public void DeleteKey(string key)
    {
        _redisClient.KeyDelete(key);
    }
}

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

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

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

以上就是土嘎嘎小编为大家整理的.Net Core之Redis插件对比【CSRedisCore】【ServiceStack.Redis】【StackExchange.Redis】相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章