.net core 6 redis操作类

2024-01-09 19:08:58
/// <summary>
/// redis操作类
/// </summary>
public class RedisTool
{
    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="configuration"></param>
    public RedisTool(IConfiguration configuration)
    {
        string redis = configuration.GetValue<string>("redis");

        CSRedisClient cSRedis = new CSRedis.CSRedisClient(redis);

        RedisHelper.Initialization(cSRedis);
    }

    /// <summary>
    /// 得到值
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string GetRedisString(string str)
    {
        return RedisHelper.Get(str);
    }

    /// <summary>
    /// 得到值
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public bool SetRedisString(string key, string value)
    {
        return RedisHelper.Set(key, value);
    }

    /// <summary>
    /// 设置列表
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string SetRedisList(string key, string value)
    {
        //列表前后插入
        RedisHelper.RPush<string>(key, value).ToString();
        return RedisHelper.LPush<string>(key, value).ToString();
    }

    /// <summary>
    /// 设置哈希值
    /// </summary>
    /// <param name="key">表名</param>
    /// <param name="field">字段名</param>
    /// <param name="value">值</param>
    /// <returns></returns>
    public bool SetRedisHSet(string key, string field, string value)
    {
        //相同时会更新
        return RedisHelper.HSet(key, field, value);
    }

    /// <summary>
    /// 得到哈希值
    /// </summary>
    /// <param name="key"></param>
    /// <param name="field"></param>
    /// <returns></returns>
    public string GetRedisHSet(string key, string field)
    {
        return RedisHelper.HGet(key, field);
    }

    /// <summary>
    /// 给值增加指定的值
    /// </summary>
    /// <param name="key"></param>
    /// <param name="field"></param>
    /// <returns></returns>
    public long SetRedisIncre(string key, long value)
    {
        return RedisHelper.IncrBy(key, value);
    }

    /// <summary>
    /// 是否包含
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool RedisSIsMember(string key, string value)
    {
        //是否包含
        return RedisHelper.SIsMember(key, value);
    }
}

1、配置appsettings.json:

"redis": "127.0.0.1:6379,password=,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240"

2、用的大神国产包:CSRedisCore

文章来源:https://blog.csdn.net/qq_40934195/article/details/135484785
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。