Redis weapon CSRedis under NetCore

It should be very common for everyone to use redis cache in the project. We used stackexchange before Redis. Inadvertently browsed a post * * net core 2.0 redis driver performance competition * *, it is found that there are two shorter and stronger components: CSRedis and newlife Redis is actually like servicestack Redis .

  • ServiceStack.Redis: it is a commercial version, and the free version is limited;
  • StackExchange.Redis: it is a free version. There was a Timeout Bug in the early stage. The current version can be solved only by using asynchronous methods;
  • NewLife.Redis: free version. It is widely used in real-time computing of Zhongtong big data. It seems to be very good.

First, let's take a look at the official website https://redis.io/ , the following operations are officially recommended:

For example, when the cache data reaches 500G, if you use a redis server server, it will be very difficult to rely on memory storage alone, and the use of hard disk will affect the performance. This function can be used to automatically manage the shared storage of n redis server servers. Each server only needs about (500/N)G of memory, and each server can be configured with the official high availability architecture.

Those marked with an asterisk are officially recommended, but we still want to try CSRedis today to see if they can be used in later projects.

basic function

CSRedisClient is used to instantiate a CSRedisClient object and then initialize RedisHelper. All method names are consistent with redis cli. It is basically the same as using the redis cli command.

Function introduction

1. Get Nuget package (current version 3.5.1)! Its description is: CSRedis is redis IO official recommendation library, which supports redis trib cluster, sentinel, private partition and connection pool management technology, and simple RedisHelper static class.

nuget Install-Package CSRedisCore

2. Introduction of four modes: general mode, sentinel mode, cluster mode and zoning mode

Normal mode

var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=13,prefix=key prefix ");

Parameter description

Sentinel mode

var csredis = new CSRedis.CSRedisClient(
    "mymaster,password=123,prefix=key prefix ", 
    new [] { "192.169.1.10:26379", "192.169.1.11:26379", "192.169.1.12:26379" });

mymaster in the connection string is the name of sentinel listening. Other configuration parameters are consistent with normal mode

Read only mode: new CSRedisClient("mymaster,password=123", new [] {sentinel}, false)

Cluster mode

Suppose you have configured the redis trib cluster and defined a CSRedisClient object in [normal mode], which will automatically add the node attribute according to the MOVED | ASK error record slot returned by the redis server.

Partition mode

This function enables multiple service nodes to share storage (a way implemented by the author), which is different from the official partition, cluster and high availability scheme.

For example, when the cache data reaches 500G, if you use a redis server server, it will be very difficult to rely on memory storage alone, and the use of hard disk will affect the performance. This function can be used to automatically manage the shared storage of n redis server servers. Each server only needs about (500/N)G of memory, and each server can be configured with the official high availability architecture.

example

Let me simply use the normal mode to play with an example:

private static CSRedisClient csredis;
static void Main(string[] args)
{
    csredis = new CSRedisClient("127.0.0.1:6379,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240");
    RedisHelper.Initialization(csredis);
    Test();
    Console.ReadKey();
}

static void Test()
{

    csredis.Set("name", "Blake");//Set the value. Default never expires
    Console.WriteLine(RedisHelper.Get<String>("name"));
    RedisHelper.SetAsync("fullname", "BlakeYu");//Asynchronous operation
    Console.WriteLine(RedisHelper.Get<String>("fullname"));

    csredis.Set("time", DateTime.Now, 1);
    Console.WriteLine(RedisHelper.Get<DateTime>("time"));
    Thread.Sleep(1100);
    Console.WriteLine(RedisHelper.Get<DateTime>("time"));

    // list
    csredis.RPush("list", "First element");
    csredis.RPush("list", "Second element");
    csredis.LInsertBefore("list", "Second element", "I am the second element newly inserted!");
    Console.WriteLine($"list The length of the is:{csredis.LLen("list")}");
    Console.WriteLine($"list The second element of is:{csredis.LIndex("list", 1)}");

    // Hash
    csredis.HSet("person", "name", "Zhang San");
    csredis.HSet("person", "sex", "male");
    csredis.HSet("person", "age", "28");
    csredis.HSet("person", "adress", "wuhan");
    Console.WriteLine($"person In this hash age by:{csredis.HGet<int>("person", "age")}");


    // aggregate
    csredis.SAdd("students", "Zhang San", "Li Si");
    csredis.SAdd("students", "Wang Wu");
    csredis.SAdd("students", "Zhao Niu");
    Console.WriteLine($"students The size of this collection is:{csredis.SCard("students")}individual");
    Console.WriteLine($"students Does this collection contain wagnwu:{csredis.SIsMember("students", "wangwu")}");
}    

The operation results are as follows:

Keywords: Database Redis Cache

Added by dm3 on Sun, 20 Feb 2022 03:51:24 +0200