惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
GbyAI
GbyAI
WordPress大学
WordPress大学
NISL@THU
NISL@THU
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
D
DataBreaches.Net
F
Full Disclosure
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
Visual Studio Blog
Last Week in AI
Last Week in AI
L
LangChain Blog
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
V
V2EX
The Hacker News
The Hacker News
Scott Helme
Scott Helme
T
Troy Hunt's Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric
C
Cyber Attacks, Cyber Crime and Cyber Security
O
OpenAI News
月光博客
月光博客
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
IT之家
IT之家
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
T
Tenable Blog
Jina AI
Jina AI
T
Tor Project blog
The Cloudflare Blog
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
Cyberwarzone
Cyberwarzone
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
A
Arctic Wolf

博客园 - 邢帅杰

.net core使用SharpZipLib压缩zip文件并设置密码 Android 常用数据目录(内部 / 外部、缓存、文件) 的 获取方法对照表 安卓把assets中的文件copy到app目录中 oracle执行sql语句前清除缓存 安卓开发使用interface自定义回调函数 安装DockerDesktop并启用 oracle中decode用法 vue使用import.meta编译报错,import.meta.env报:类型“ImportMeta”上不存在属性“env”。必须配置module。 oracle游标使用详解 oracle存储过程中声明一个行变量,接收游标中的行数据。variable_name table_name%ROWTYPE oracle NVL和NVL2 C#获取文件md5码 oracle查询存储过程和函数中是否包含某个字符串 Android清除WebView缓存 C#获取当前日期是星期几 切换项目git地址,项目迁移到新git地址 C#线程同步、跨进程同步Mutex详解、C#只允许运行一个实例 Android Stack说明 安卓打开第三方app并传入参数 安卓如何唤醒深度睡眠的设备并执行任务 java两个日期相差秒数
CSRedisCore用法
邢帅杰 · 2026-05-21 · via 博客园 - 邢帅杰

安装

Install-Package CSRedisCore
# 或 .NET Core CLI
dotnet add package CSRedisCore

注册

using CSRedis;

builder.Services.AddSingleton<CSRedisClient>(sp =>
{
    return new CSRedisClient("127.0.0.1:6379,password=123456,ConnectTimeout=3000,defaultDatabase=0,abortConnect=false,poolsize=5000");
});

// 或直接初始化 RedisHelper(全局静态)
RedisHelper.Initialization(new CSRedisClient("127.0.0.1:6379"));

公共类,也可以使用RedisHelper替换

using CSRedis;
using Newtonsoft.Json;

namespace XCGWebApp.Common
{
    /// <summary>
    /// CSRedis公共类
    /// </summary>
    public interface IRedisRepository
    {
        Task SetAsync<T>(string key, T value, TimeSpan? expiry = null);
        Task<T> GetAsync<T>(string key);
        Task<T> HGetAsync<T>(string key, string field);
        Task<bool> HSetAsync(string key, string field, string value);
        Task ReleaseLockAsync(string key);
        Task<bool> AcquireLockAsync(string key, TimeSpan expiry);
        Task<long> HDelAsync(string key, params string[] fields);
    }

    public class RedisRepository : IRedisRepository
    {
        private readonly CSRedisClient _redis;

        public RedisRepository(CSRedisClient redis) => _redis = redis;

        /// <summary>
        /// 使用管道批量设置Key-Value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <param name="expiry"></param>
        public void BatchSetAsync<T>(Dictionary<string, T> dic, TimeSpan? expiry = null)
        {
            var clientPipe =  _redis.StartPipe();
            foreach (var key in dic.Keys)
            {
                var value = dic[key];
                var json = JsonConvert.SerializeObject(value);

                if (expiry.HasValue)
                    clientPipe.Set(key, json, expiry.Value);
                else
                    clientPipe.Set(key, json);
            }
            clientPipe.EndPipe();
        }

        /// <summary>
        /// 获取Hash类型单字段值
        /// </summary>
        /// <typeparam name="T">自动反序列化类型</typeparam>
        /// <param name="key">总键</param>
        /// <param name="field">具体字段名</param>
        /// <returns></returns>
        public async Task<T> HGetAsync<T>(string key, string field)
        {
            var res = await _redis.HGetAsync<T>(key, field);
            return res;
        }
        /// <summary>
        /// 设置Hash类型单字段值
        /// </summary>
        /// <param name="key">总键</param>
        /// <param name="field">具体字段名</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public async Task<bool> HSetAsync(string key, string field, string value)
        {
            return await _redis.HSetAsync(key, field, value);
        }
        /// <summary>
        /// 删除Hash类型某些单字段值
        /// </summary>
        /// <param name="key">总键</param>
        /// <param name="fields">具体字段名 数组</param>
        /// <returns></returns>
        public async Task<long> HDelAsync(string key, params string[] fields)
        {
            return await _redis.HDelAsync(key, fields);
        }

        public async Task<T> GetAsync<T>(string key)
        {
            //var res = await RedisHelper.GetAsync<T>(key);
            var res = await _redis.GetAsync<T>(key);
            return res;
        }

        public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null)
        {
            var json = JsonConvert.SerializeObject(value);
            if (expiry.HasValue)
                await _redis.SetAsync(key, json, expiry.Value);
            else
                await _redis.SetAsync(key, json);
        }

        /// <summary>
        /// 可靠的分布式锁,对防止并发问题至关重要。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public async Task<bool> AcquireLockAsync(string key, TimeSpan expiry)
        {
            return await _redis.SetAsync(key, "locked", expiry, RedisExistence.Nx);
        }

        /// <summary>
        /// 删除一个key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task ReleaseLockAsync(string key)
        {
            await _redis.DelAsync(key);
        }

    }
}

 注册RepositoryRedis
builder.Services.AddScoped<IRedisRepository, RedisRepository>();//CSRedisCore