YokoRedis 2.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package YokoRedis --version 2.1.1                
NuGet\Install-Package YokoRedis -Version 2.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YokoRedis" Version="2.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add YokoRedis --version 2.1.1                
#r "nuget: YokoRedis, 2.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install YokoRedis as a Cake Addin
#addin nuget:?package=YokoRedis&version=2.1.1

// Install YokoRedis as a Cake Tool
#tool nuget:?package=YokoRedis&version=2.1.1                

[TOC]

YokoRedis

由于免费的 StackExchange.Redis 不可靠,ServiceStack.Redis又付费,故使用YokoRedis

单机 Redis

var myredis = new YokoRedis.YokoRedisClient("127.0.0.1:6379,password=12345678,defaultDatabase=0,prefix=test:");
范围 默认 解释
user <Empty> Redis 服务器用户(redis 6.0+)
password <Empty> Redis 服务器密码
defaultDatabase 0 Redis 服务器数据库
asyncPipeline false 异步方式自动使用流水线
poolsize 50 连接池大小
idleTimeout 20000 连接池中元素的空闲时间(MS),适合连接远程redis服务器
connectTimeout 5000 连接超时 (MS)
syncTimeout 10000 发送/接收超时 (MS)
preheat 5 预热连接,接收值如 preheat = 5 , 预热 5 个连接
autoDispose true 跟随系统退出事件自动释放
ssl false 启用加密传输
testcluster true 是否尝试集群模式,阿里云、腾讯云集群需要设置此选项为 false
tryit 0 执行错误,重试尝试
name <Empty> 连接名称,使用client list命令查看
prefix <Empty> key前辍,设置后,所有方法都会附带此前辍,yokoredis.Set(prefix + "key", 111);

Net462/Net472/Net48

//初始化
RedisHelper.Initialization(new YokoRedis.YokoRedisClient("127.0.0.1:6379,defaultDatabase=3,prefix=test:"));

RedisHelper.Set("666", "12345678", 60);
//var t1 = RedisHelper.CacheShell("666", 60, () => "缓存壳12345678");
//Console.WriteLine(t1);

var t2 = RedisHelper.Get("666");
Console.WriteLine(t2);

Net Core/Net5/Net6

在分布式缓存 IDistributedCache 接口用使用Redis

添加包 YokoRedis.Caching

注:YokoRedisClient 为单例,推荐使用 RedisHelper 静态类,方法名与redis cli的命令相同

//初始化
RedisHelper.Initialization(new YokoRedis.YokoRedisClient("127.0.0.1:6379,defaultDatabase=1,prefix=test:"));
//普通模式
services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.YokoRedisCache(RedisHelper.Instance));
//也可以使用一般用法
// var myredis = new YokoRedis.YokoRedisClient("198.98.98.10:6371,defaultDatabase=11,prefix=key前辍");
// services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.YokoRedisCache(myredis));

//集群模式:初始化的时候写多几个就行了
var myredis = new YokoRedis.YOkoRedisClient(null, "198.98.98.10:6371,defaultDatabase=11,prefix=key前辍", "198.98.98.11:6372,defaultDatabase=12,prefix=key前辍", "198.98.98.12:6373,defaultDatabase=13,prefix=key前辍", "198.98.98.13:6374,defaultDatabase=14,prefix=key前辍");
RedisHelper.Set("001", "66666666", 60);
RedisHelper.Get("001");
  • 缓存对象扩展方法
IDistributedCache cache = xxxx;

object obj1 = new xxxx();
cache.SetObject("key1", obj1);

object obj2 = cache.GetObject("key1");
T obj3 = cache.GetObject<T>("key1");

//批量删除
cache.Remove("key1|key2");

缓存壳

//不加缓存的时候,要从数据库查询
var data = db.Queryable<Order>().ToList();

//一般的缓存代码,如果不封装还需要自己判断
var cacheValue = RedisHelper.Get("test1");
if (string.IsNullOrEmpty(cacheValue)) {
    var data = db.Queryable<Order>().ToList();;
	RedisHelper.Set("test1", data, 60); //缓存60秒
	return data;
}else{
	try {
		return cacheValue;
	} catch {
		RedisHelper.Remove("test1");//出错时删除key
		throw;
	}
}

//使用缓存壳效果同上,以下示例使用 string 和 hash 缓存数据
var t1 = RedisHelper.CacheShell("test1", 10, () => db.Queryable<Order>().ToList());
var t2 = RedisHelper.CacheShell("test", "1", 10, () => db.Queryable<Order>().ToList());
var t3 = RedisHelper.CacheShell("test", new [] { "1", "2" }, 10, notCacheFields => new [] {
  ("1", db.Queryable<Order>().ToList()),
  ("2", db.Queryable<Order2>().ToList())
});

操作多个数据库

var connectionString = "127.0.0.1:6379";
var redis = new YokoRedisClient[14]; 
for (var a = 0; a< redis.Length; a++) {
  redis[a] = new YokoRedisClient(connectionString + ",defaultDatabase=" + a);
}
redis[1].Get("001");

多个 RedisHelper

public abstract class MyHelper1 : RedisHelper<MyHelper1> {}
public abstract class MyHelper2 : RedisHelper<MyHelper2> {}

MyHelper1.Initialization(new YokoRedisClient("...."));
MyHelper2.Initialization(new YokoRedisClient("...."));

订阅/发布

//原生订阅
RedisHelper.Subscribe(
  ("chan1", msg => Console.WriteLine(msg.Body)),
  ("chan2", msg => Console.WriteLine(msg.Body)));

RedisHelper.PSubscribe(new[] { "test*", "*test001", "test*002" }, msg => {
  Console.WriteLine($"PSUB   {msg.MessageId}:{msg.Body}    {msg.Pattern}: chan:{msg.Channel}");
});

//模式订阅:
//1、分区的节点匹配规则,导致通配符最大可能匹配全部节点,所以全部节点都要订阅
//2、本组 "test*", "*test001", "test*002" 订阅全部节点时,需要解决同一条消息不可执行多次

RedisHelper.Publish("chan1", "123123123");

Redis 哨兵模式

var myredis = new YokoRedis.YokoRedisClient("mymaster,password=12345678,prefix=test:", 
  new [] { "198.98.98.10:6379", "198.98.98.11:6379", "198.98.98.12:6379" });

只读:new YokoRedisClient("mymaster,password=123", new [] { Sentinels }, false)

管道模式

使用管道模式,打包多条命令一起执行,从而提高性能。

var ret1 = RedisHelper.StartPipe(p => p.Set("a", "1").Get("a"));
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.1 461 9/27/2022
2.2.0 399 9/27/2022
2.1.1 421 9/10/2022