This is the 9th day of my participation in Gwen Challenge
About me
The author blog | start
In the past,.net’s most prestigious ServiceStack.Redis has been relegated to commercial use, using top-up only in.netcore. While stackexchange. Redis works, the previous Timeout errors are quite confusing, so it’s not a good choice. After some sorting and recommendation on the Internet, I found an open source library CSReidsCore.
CSRedisCore is a set of Redis operating library which is open source in China. Now the latest version is V3.6.5. There are no major problems after the use of several actual company projects.
Characteristics of the
- CSRedisClient and RedisHelper keep all method names the same as redis- CLI
- Support for geographic type commands (redis-Server 3.2 or later required)
- Support for Redis cluster redis-trib.rb
- Support for Redis Sentinel and master slave
- Support for stream type commands (redis-Server 5.0 and later required)
The official parameters
The scope of | The default | explain |
---|---|---|
password | < empty > | Redis server password |
defaultDatabase | 0 | Redis server database |
asyncPipeline | The wrong | Asynchronous methods use pipes automatically and 10W concurrent time is 450ms (feedback welcome) |
The pool size | 50 | Connection pool size |
Idle timeout | 20000 | The idle time of an element in the connection pool (MS) for connecting to a remote Redis server |
connectTimeout | 5000 | Connection Timeout (MS) |
syncTimeout | 10000 | Send/receive timeout (MS) |
preheating | 5 | Preheating connection, receiving preheating equivalent = 5 preheating 5 connections |
Automatic disposal | really | Track system exit events for automatic release |
ssl | The wrong | Enabling Encrypted Transmission |
Test cluster | really | If you want to try normal mode, Ali cloud, Tencent cloud merge, set this option to false |
Give it a try | 0 | Error execution, retry the attempt |
The name | < empty > | Connection name, viewed using the client list command |
The first word | < empty > | Csredis.Set (prefix + “key”, 111); |
Begin to use
Redis standalone
Redis is the only Db used
According to the Github authors, if you are the only DB usage scenario, it is recommended to use this method for initialization and usage
Using RedisHelper for initialization and invocation is recommended only for Db
var rds = new CSRedis. CSRedisClient (127.0.0.1 ": 6379, password = 123, defaultDatabase = 0, poolsize = 50, SSL = false, writeBuffer = 10240"); RedisHelper.Initialization(rds); // RedisHelper redishelper.set ("test", datetime.now.tostring ()); RedisHelper.Get("test");Copy the code
Redis is used by multiple Db
The instance array is injected as a singleton
Recommended ways:
Consolidate the various RedisDb instances into an array and inject the services singleton
Var connectionString = "127.0.0.1:6379, password = 123, poolsize = 10"; var redis = new CSRedisClient[14]; for (var a = 0; a < redis.Length; a++) redis[a] = new CSRedisClient(connectionString + ",defaultDatabase=" + a); service.AddSingleton(redis);Copy the code
Create multiple ReidSheplers
Each library corresponds to a RedisHelper, which initializes the Client instance of the specified Helper
public abstract class MyHelper1 : RedisHelper<MyHelper1> {} public abstract class MyHelper2 : RedisHelper<MyHelper2> {} MyHelper1.Initialization(new CSRedisClient("...." )); MyHelper2.Initialization(new CSRedisClient("...." ));Copy the code
Redis cluster
var csredis = new CSRedis.CSRedisClient(null, "127.0.0.1:6371, password = 123, defaultDatabase = 11, poolsize = 10, SSL = false, writeBuffer = 10240", "127.0.0.1:6372, password = 123, defaultDatabase = 12, poolsize = 11, SSL = false, writeBuffer = 10240", "127.0.0.1:6373, password = 123, defaultDatabase = 13, poolsize = 12, SSL = false, writeBuffer = 10240", "127.0.0.1:6374, password = 123, defaultDatabase = 14, poolsize = 13, SSL = false, writeBuffer = 10240"); csredis.Set("test", DateTime.Now.ToString()); csredis.Get("test");Copy the code
The guard mode
Var csredis = new csredis.CSRedisClient("mymaster,password=123", new[] {"192.169.1.10:26379", "192.169.1.11:26379", "192.169.1.12:26379"}); csredis.Set("test", DateTime.Now.ToString()); csredis.Get("test");Copy the code
Distributed cache
The use of distributed caching based on the CSRedisCore open source library is also very simple. The general idea is the same as above but the method body has changed
Normal mode
var csredis = new CSRedis. CSRedisClient (127.0.0.1 ": 6379, password = 123, defaultDatabase = 13, SSL = false, writeBuffer = 10240, poolsize = 50, prefix = key Before dropping out "); services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(csredis));Copy the code
Cluster pattern
var csredis = new CSRedis.CSRedisClient(null, "127.0.0.1:6371, password = 123, defaultDatabase = 11, poolsize = 10, SSL = false, writeBuffer = 10240, prefix =" dropping out before the key, "127.0.0.1:6372, password = 123, defaultDatabase = 12, poolsize = 11, SSL = false, writeBuffer = 10240, prefix =" dropping out before the key, "127.0.0.1:6373, password = 123, defaultDatabase = 13, poolsize = 12, SSL = false, writeBuffer = 10240, prefix =" dropping out before the key, "127.0.0.1:6374, password = 123, defaultDatabase = 14, poolsize = 13, SSL = false, writeBuffer = 10240, prefix =" dropping out before key); services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(csredis));Copy the code
If you need to switch between multiple databases, you can refer to the “Redis multiple Db use” TAB to set up
Advanced usage
Advanced usage of CSRedis can be found in this article.NETCore’s simple and advanced library CSRedis v3.0.0 and Github official library. The general situation is not used, until the need to use the time to review can be.
Recommended reading
New open source has emerged after Redis tools were charged
The highest engineer skills chart on GitHub
The last
The end of this article, I hope to help you 😃
If there are any questions or suggestions, you can exchange more original articles, writing is limited, talent and learning is shallow, if there is something wrong in the article, hope to inform.
More wonderful technical articles are summarized in my public number [programmer tools set], continue to update, welcome to pay attention to the subscription collection.