. NET redis client open source component FreeRedis (rewritten after CSRedisCore)

1 | 0 what is FreeRedis

FreeRedis is a NET redis client open source component, which is open source hosted in github with MIT protocol, is currently supported NET 5,. NETCore 2.1+,. Net framework 4.0 + and Xamarin may already support AOT compilation (currently not tested, but will go in this direction).

FreeRedis will do a good job in unit testing in strict accordance with the open source mode of FreeSql. It is compatible with the platform, easy to use, and has an attitude of answering questions and responding to requests, so as to serve China NET open source cause.

Thank you for your support. The project has won 66 stars before it is made public. At present, the project is still in its infancy. Small partners are welcome to participate. They can contribute tests, code or suggestions.

Current status of the project:

  • Version 0.0.8 (currently not recommended for production environments)
  • 268 unit tests
  • Support cluster, sentry and master-slave (tested)
  • Support connection pool
  • support. NET5/.NETCore 2.1+/.NET4.0+
  • Redis6.0 support 0 all types
  • Redis6.0 support 0 resp3 protocol
  • The API is still consistent with the redis cli command
  • Adopt the loosest open source protocol MIT https://github.com/2881099/FreeRedis

2. Origin of the project

It's a long story. Before 2016, I wrote a nodejs server application for more than a year and used node redis components. It's really easy to use. During this period, some colleagues kept Amway NET can cross platform, advise me to come back soon NET, at first, I resisted being the first person to be a crab. I don't know which afternoon I went to experience it NETCore 1.0-previewXX (I don't remember which version). I tried and was attracted. The experience is very similar to that of expressjs. I can no longer see the shortcomings of webform/mvc in the past.

So I'm ready to go into the pit. The first thing to go into the pit is not only hello world, but also relevant research:

  • Performance OK
  • Design OK
  • Development OK (temporary rating)
  • Related components OK (HttpClient, Redis, Ado.NET, and other basic components)

After the initial research is completed, we will take time to select the framework, and finally choose the one suitable for the team from many frameworks: https://github.com/simplcommerce/SimplCommerce , on the original basis of this project, combined with the requirements of enterprise specifications, customized transformation has been completed in about two months. (the frame does not want to be perfect at the beginning, but to be polished continuously in use and finally become perfect)

The ideal plump and realistic backbone. The next story is the production failure, stackeexchange Redis and HttpClient have talked about these two components before, but they won't talk about them now (I never thought there would be problems with the use of such a large component). If you eat crabs, you will fall into a pit. If you fall into a pit, you have to find a way to solve it, and finally become associated with the csredis component.

Look at the situation at that time NET all redis client components, only csredis source code is the easiest to transform and support NETCore (limited level, please forgive me), csredis stopped updating in 2014, and I supported its transformation in 2016 NETCore, and add connection pool management, cluster, sentry and redis2 8. The above orders are open source after the company's project production environment is used for one and a half years.

CSRedisCore has been open source for so long, and the number of nuget downloads has reached 60W. There are a number of collection requirements and bugs (some have been solved and some have not been reproduced). Based on my familiarity with redis, redis 5.0/6.0 has added many new features, and the idea of writing a new one with fewer bugs and better maintainability came into being.

After several months of ink, the project was finally named FreeRedis

Thanks to the big brother of Nuget transfer package.

3 | 0 how to use

🌈 Single machine redis

public static RedisClient cli = new RedisClient(",password=123,defaultDatabase=13");
//cli.Serialize = obj => JsonConvert.SerializeObject(obj);
//cli.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
cli.Notice += (s, e) => Console.WriteLine(e.Log); //print command log

cli.Set("key1", "value1");
cli.MSet("key1", "value1", "key2", "value2");

string value1 = cli.Get("key1");
string[] vals = cli.MGet("key1", "key2");

The API is still consistent with the redis cli command, so if you want to know how to use each method of FreeRedis, go to Baidu to search "redis command", there are a lot of materials. don't say so much!!!

Redis6.0 support All data types supported by 0: strings, hashes, lists, sets, sorted sets, bitmaps, hyperlogs, geo, streams and bloomfilter

Parameter Default Explain
protocol RESP2 If you use RESP3, you need redis 6.0 environment
user <empty> Redis server username, requires redis-server 6.0
password <empty> Redis server password
defaultDatabase 0 Redis server database
max poolsize 100 Connection max pool size
min poolsize 5 Connection min pool size
idleTimeout 20000 Idle time of elements in the connection pool (MS)
connectTimeout 10000 Connection timeout (MS)
receiveTimeout 10000 Receive timeout (MS)
sendTimeout 10000 Send timeout (MS)
encoding utf-8 string charset
ssl false Enable encrypted transmission
name <empty> Connection name, use client list command to view

If you need to connect to IPv6, please use the connection string: [fe80::b164:55b3:4b4f:7ce6%15]:6379

🎣 Master slave

public static RedisClient cli = new RedisClient(

var value = cli.Get("key1");

For the cli created in this way, all write commands will be executed by connecting, and all read commands will only be executed by connecting or randomly. (each command has been internally marked for reading and writing)

⛳ Redis Sentinel (sentinel high availability)

public static RedisClient cli = new RedisClient(
    new [] { "", "", "" },
    true //Whether to separate reading and writing

Sentry is a distributed system that provides high availability solutions for Redis. When the host master goes down, a new master will be available immediately to ensure the normal operation of the service.

Sentinel mode can also set read-write separation to relieve the pressure of frequent data reading by the master. Disadvantages: it is possible that the data read is not up-to-date, because there is a delay in the synchronization of redis from the master to the slave.

🌌 Redis Cluster

If you have a Redis Cluster with three master nodes (7001-7003) and three slave nodes (7004-7006), the code for connecting this cluster:

public static RedisClient cli = new RedisClient(
    new ConnectionStringBuilder[] { "", "", "" }

📡 Subscribe

using (cli.Subscribe("abc", ondata)) //wait .Dispose()

void ondata(string channel, string data) => Console.WriteLine($"{channel} -> {data}");

📃 Scripting

var r1 = cli.Eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", new[] { "key1", "key2" }, "first", "second") as object[];

var r2 = cli.Eval("return {1,2,{3,'Hello World!'}}") as object[];

cli.Eval("return redis.call('set',KEYS[1],'bar')", new[] { Guid.NewGuid().ToString() })

💻 Pipeline

using (var pipe = cli.StartPipe())
    pipe.IncrBy("key1", 10);
    pipe.Set("key2", Null);

    object[] ret = pipe.EndPipe();
    Console.WriteLine(ret[0] + ", " + ret[2]);

// or Async Callback

using (var pipe = cli.StartPipe())
    var tasks = new List<Task>();
    long t0 = 0;
    task.Add(pipe.IncrByAsync("key1", 10).ContinueWith(t => t0 = t.Result)); //callback

    pipe.SetAsync("key2", Null);

    string t2 = null;
    task.Add(pipe.GetAsync("key1").ContinueWith(t => t2 = t.Result)); //callback

    Task.WaitAll(tasks.ToArray()); //wait all callback
    Console.WriteLine(t0 + ", " + t2);

📰 Transaction

using (var tran = cli.Multi())
    tran.IncrBy("key1", 10);
    tran.Set("key2", Null);

    object[] ret = tran.Exec();
    Console.WriteLine(ret[0] + ", " + ret[2]);

// or Async Callback

using (var tran = cli.Multi())
    var tasks = new List<Task>();
    long t0 = 0;
    task.Add(tran.IncrByAsync("key1", 10).ContinueWith(t => t0 = t.Result)); //callback

    tran.SetAsync("key2", Null);

    string t2 = null;
    task.Add(tran.GetAsync("key1").ContinueWith(t => t2 = t.Result)); //callback

    Task.WaitAll(tasks.ToArray()); //wait all callback
    Console.WriteLine(t0 + ", " + t2);

📯 GetDatabase

using (var db = cli.GetDatabase(10))
    db.Set("key1", 10);
    var val1 = db.Get("key1");

4 | 0 conclusion

At present, the project is still in its infancy. Small partners are welcome to participate. They can contribute tests, code or suggestions.

FreeRedis uses MIT, the loosest open source protocol https://github.com/2881099/FreeRedis

If you have a good idea of redis implementation, please leave a message to the author for discussion. Thank you for watching!

Author: FreeSql & CSRedis 
Link to this article: https://www.cnblogs.com/kellynic/p/13943313.html
About bloggers: comments and private messages will be replied at the first time. perhaps Direct private letter I.
Copyright notice: all articles on this blog are in English unless otherwise stated BY-NC-SA License agreement. Reprint please indicate the source!
Support blogger: if you think the article is helpful to you, you can click [] in the lower right corner of the article. Your encouragement is the biggest motivation of bloggers!

Tags: Redis .NET

Posted by mizz key_me on Sun, 01 May 2022 19:37:35 +0300