.net core 6 redis操作类

cs 复制代码
/// <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

追加消息发布和订阅示例(mq直连和广播模式):

cs 复制代码
/// <summary>
/// 消息发布
/// </summary>
void Publish()
{
    //RedisHelper.Publish("subscribe", "12");
    RedisHelper.LPush("subscribe", "12");
}

/// <summary>
/// 消息订阅
/// </summary>
void Subscribe()
{
    //SubscribeObject subscribeObject
    //    = RedisHelper.Subscribe(("subscribe", msg => SubscribeAction(msg.Body)));

    //取消订阅
    //subscribeObject.Dispose();

    RedisHelper.SubscribeListBroadcast("subscribe", "1", msg => SubscribeAction(msg));
    RedisHelper.SubscribeListBroadcast("subscribe", "2", msg => SubscribeAction(msg));
    RedisHelper.SubscribeListBroadcast("subscribe", "3", msg => SubscribeAction(msg));
}

/// <summary>
/// 订阅到消息
/// </summary>
/// <param name="str"></param>
void SubscribeAction(string str)
{
    Console.WriteLine(str);
}
相关推荐
miss writer1 分钟前
Redis分布式锁释放锁是否必须用lua脚本?
redis·分布式·lua
_oP_i1 小时前
.NET Core 项目配置到 Jenkins
运维·jenkins·.netcore
亽仒凣凣1 小时前
Windows安装Redis图文教程
数据库·windows·redis
希忘auto3 小时前
详解Redis的常用命令
redis·1024程序员节
岁月变迁呀9 小时前
Redis梳理
数据库·redis·缓存
Code apprenticeship11 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站11 小时前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
装不满的克莱因瓶12 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
黄名富15 小时前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
G_whang16 小时前
centos7下docker 容器实现redis主从同步
redis·docker·容器