💻 Preface
There are quite a lot of choices for RedisClient SDK under .NET. Chinese people often use StackExchange.Redis/CSRedis/Newlife.Redis for free, and ServiceStack.Redis for a fee.
If you are a CSRedis or ServiceStack.Redis fan, you must not miss to pay attention to FreeRedis. Their API s are very similar, and the method names and parameters are consistent with the official command documentation of redis.io, which avoids the understanding cost of secondary conversion. There are nearly 300 redis commands~~~
Tip: CSRedisCore and FreeRedis are the same author. The latter is rebuilt based on redis6.0 features, which solves some old problems of CSRedisCore and is more scalable.
🌳 Open source concept
FreeRedis is named after Free, Free, and its name and FreeSql are concepts that are easy to follow in the same direction as FreeSql, supporting at least the.NET Framework 4.0 runtime environment and Redis-server 8.0 (Hyperspace Version).
Open source address: https://github.com/2881099/FreeRedis
FreeRedis is the easiest open source protocol MIT open source. It has been 22 months since the first version of v0.0.1 was released. It has been time to verify its reliability. It is time to officially publish v1.0 for everyone to do.neter Not Easy, one more choice, one more path.
Since the optimization of asynchronous methods has not been opened before, v1.0 officially opens asynchronous methods
The entire source code of FreeRedis has zero dependencies. Using it will only generate a FreeRedis.dll in the bin directory, which is very lightweight and powerful:
<h1> 🦄 FreeRedis </h1>
.NET based Redis client supporting .NET Core 2.1+, .NET Framework 4.0+ and Xamarin.
- 🌈 All method names are consistent with redis-cli
- 🌌 Support Redis cluster (server requires version 3.2 and above)
- ⛳ Support Redis Sentinel Mode
- 🎣 Support master-slave separation (Master-Slave)
- 📡 Support Publish-Subscribe (Pub-Sub)
- 📃 Support Redis Lua scripts
- 💻 Support Pipeline
- 📰 Support transactions, command interception, log events
- 🌴 Support GEO commands (server requires 3.2 and above)
- 🌲 Support STREAM type commands (server requires version 5.0 and above)
- ⚡ Support local caching (Client-side-caching, server requires 6.0 and above)
- 🌳 Support RESP3 protocol for Redis 6
QQ group: 4336577 (full), 8578575 (online), 52508226 (online)
🚀 Quick start
public static RedisClient cli = new RedisClient("127.0.0.1:6379,password=123,defaultDatabase=13"); cli.Serialize = obj => JsonConvert.SerializeObject(obj); //serialize, store objects 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");
Supports STRING, HASH, LIST, SET, ZSET, BITMAP, HyperLogLog, GEO, Stream, RedisJSON, and Bloom filters.
parameter | Defaults | illustrate |
---|---|---|
protocol | RESP2 | To use RESP3 protocol, you need Redis 6.0 environment |
user | \<empty\> | Redis server username, requires Redis 6.0 environment |
password | \<empty\> | Redis server password |
defaultDatabase | 0 | Redis server database |
max poolsize | 100 | The maximum number of connections in the connection pool |
min poolsize | 5 | Connection pool minimum number of connections |
idleTimeout | 20000 | The idle time (in milliseconds) of elements in the connection pool, suitable for connecting to a remote server |
connectTimeout | 10000 | Connection timeout, in milliseconds (ms) |
receiveTimeout | 10000 | Receive timeout, in milliseconds (ms) |
sendTimeout | 10000 | Send timeout, in milliseconds (ms) |
encoding | utf-8 | String character set |
retry | 0 | When an error occurs in the protocol, the number of times to retry the execution |
ssl | false | Enable encrypted transmission |
name | \<empty\> | Connection name, use the CLIENT LIST command to view |
prefix | \<empty\> | key prefix, all methods will come with this prefix, cli.Set(prefix + "key", 111); |
IPv6: [fe80::b164:55b3:4b4f:7ce6%15]:6379
🎣 Master-Slave (separation of read and write)
public static RedisClient cli = new RedisClient( "127.0.0.1:6379,password=123,defaultDatabase=13", "127.0.0.1:6380,password=123,defaultDatabase=13", "127.0.0.1:6381,password=123,defaultDatabase=13" ); var value = cli.Get("key1");
Connect 127.0.0.1:6379 when writing, randomly connect 6380 6381 when reading
⛳ Redis Sentinel
public static RedisClient cli = new RedisClient( "mymaster,password=123", new [] { "192.169.1.10:26379", "192.169.1.11:26379", "192.169.1.12:26379" }, true //Whether to separate read and write );
🌌 Redis Cluster
If you have a Redis Cluster cluster with three master nodes (7001-7003) and three slave nodes (7004-7006), the code to connect to this cluster:
public static RedisClient cli = new RedisClient( new ConnectionStringBuilder[] { "192.168.0.2:7001", "192.168.0.2:7002", "192.168.0.2:7003" } );
⚡ Client-side-caching (local cache)
Server requires version 6.0 and above
cli.UseClientSideCaching(new ClientSideCachingOptions { //The capacity of the local cache Capacity = 3, //Filter which keys can be cached locally KeyFilter = key => key.StartsWith("Interceptor"), //Check for long-unused caches CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(2) });
Important features for details: https://www.cnblogs.com/kelly...
📡 Subscribe
using (cli.Subscribe("abc", ondata)) //wait .Dispose() { Console.ReadKey(); } void ondata(string channel, string data) => Console.WriteLine($"{channel} -> {data}");
lpush + blpop:
using (cli.SubscribeList("list_key", ondata)) //wait .Dispose() { Console.ReadKey(); } void ondata(string listValue) => Console.WriteLine(listValue);
📃 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); pipe.Get("key1"); object[] ret = pipe.EndPipe(); Console.WriteLine(ret[0] + ", " + ret[2]); }
📰 Transaction
using (var tran = cli.Multi()) { tran.IncrBy("key1", 10); tran.Set("key2", Null); tran.Get("key1"); object[] ret = tran.Exec(); Console.WriteLine(ret[0] + ", " + ret[2]); }
📯 GetDatabase (cut library)
using (var db = cli.GetDatabase(10)) { db.Set("key1", 10); var val1 = db.Get("key1"); }
🔍 Scan
Support cluster mode
foreach (var keys in cli.Scan("*", 10, null)) { Console.WriteLine(string.Join(", ", keys)); }
🗄 License
⛳ Conclusion
If you have problems with StackExchange.Redis Timeout, try FreeRedis, it's lightweight, powerful, and obedient.
If you are still using the ServiceStack.Redis cracked version, you might as well try the free FreeRedis, which is free, open source, and well-behaved.
Open source address: https://github.com/2881099/FreeRedis
Who is the author?
The author is a veteran who has been in the industry for 18 years. The .net open source projects he currently writes include:
open source project | describe | open source address | open source protocol |
---|---|---|---|
FreeIM | Chat system architecture | https://github.com/2881099/Fr... | MIT |
FreeRedis | Redis SDK | https://github.com/2881099/Fr... | MIT |
csredis | https://github.com/2881099/cs... | MIT | |
FightLandlord | Dou DI Master Online Edition | https://github.com/2881099/Fi... | learning use |
FreeScheduler | timed task | https://github.com/2881099/Fr... | MIT |
IdleBus | free container | https://github.com/2881099/Id... | MIT |
FreeSql | ORM | https://github.com/dotnetcore... | MIT |
FreeSql.Cloud | Distributed tcc/saga | https://github.com/2881099/Fr... | MIT |
FreeSql.AdminLTE | Low-code background generation | https://github.com/2881099/Fr... | MIT |
FreeSql.DynamicProxy | Dynamic proxy | https://github.com/2881099/Fr... | learning use |
If you need it, please take it away. These are all open source works in recent years, and those written earlier will not be posted.