# FreeRedis **Repository Path**: 5131/FreeRedis ## Basic Information - **Project Name**: FreeRedis - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-12-05 - **Last Updated**: 2023-12-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

πŸ¦„ FreeRedis

FreeRedis is a redis client based on .NET, supports .NET Core 2.1+, .NET Framework 4.0+, Xamarin, and AOT. [![nuget](https://img.shields.io/nuget/v/FreeRedis.svg?style=flat-square)](https://www.nuget.org/packages/FreeRedis) [![stats](https://img.shields.io/nuget/dt/FreeRedis.svg?style=flat-square)](https://www.nuget.org/stats/packages/FreeRedis?groupby=Version) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/2881099/FreeRedis/master/LICENSE.txt)

English | δΈ­ζ–‡

- 🌈 RedisClient Keep all method names consistent with redis-cli - 🌌 Support Redis Cluster (requires redis-server 3.2 and above) - β›³ Support Redis Sentinel - 🎣 Support Redis Master-Slave - πŸ“‘ Support Redis Pub-Sub - πŸ“ƒ Support Redis Lua Scripting - πŸ’» Support Pipeline - πŸ“° Support Transaction - 🌴 Support Geo type commands (requires redis-server 3.2 and above) - 🌲 Support Streams type commands (requires redis-server 5.0 and above) - ⚑ Support Client-side-caching (requires redis-server 6.0 and above) - 🌳 Support Redis 6 RESP3 Protocol QQ Groups:4336577(full)、**8578575(available)**、**52508226(available)** ## πŸš€ Quick start ```csharp public static RedisClient cli = new RedisClient("127.0.0.1:6379,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"); ``` > Supports strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, geo, streams And BloomFilter. | Parameter | Default | Explain | | :---------------- | --------: | :------------------- | | protocol | RESP2 | If you use RESP3, you need redis 6.0 environment | | user | \ | Redis server username, requires redis-server 6.0 | | password | \ | 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), suitable for connecting to remote redis server | | connectTimeout | 10000 | Connection timeout (MS) | | receiveTimeout | 10000 | Receive timeout (MS) | | sendTimeout | 10000 | Send timeout (MS) | | encoding | utf-8 | string charset | | retry | 0 | Protocol error retry execution times | | ssl | false | Enable encrypted transmission | | name | \ | Connection name, use client list command to view | | prefix | \ | The prefix of the key, all methods will have this prefix. cli.Set(prefix + "key", 111); | | exitAutoDisposePool | true | AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress auto disposed | | subscribleReadbytes | false | Subscrible read bytes | > IPv6: [fe80::b164:55b3:4b4f:7ce6%15]:6379 ```csharp //FreeRedis.DistributedCache //services.AddSingleton(new FreeRedis.DistributedCache(cli)); ``` ### 🎣 Master-Slave ```csharp 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"); ``` > Write data at 127.0.0.1:6379; randomly read data from port 6380 or 6381. ### β›³ Redis Sentinel ```csharp 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 //This variable indicates whether to use the read-write separation mode. ); ``` ### 🌌 Redis Cluster Suppose, a Redis cluster has three master nodes (7001-7003) and three slave nodes (7004-7006), then use the following code to connect to the cluster: ```csharp 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 > requires redis-server 6.0 and above ```csharp cli.UseClientSideCaching(new ClientSideCachingOptions { //Client cache capacity Capacity = 3, //Filtering rules, which specify which keys can be cached locally KeyFilter = key => key.StartsWith("Interceptor"), //Check long-term unused cache CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(2) }); ``` ### πŸ“‘ Subscribe ```csharp using (cli.Subscribe("abc", ondata)) //wait .Dispose() { Console.ReadKey(); } void ondata(string channel, string data) => Console.WriteLine($"{channel} -> {data}"); ``` xadd + xreadgroup: ```csharp using (cli.SubscribeStream("stream_key", ondata)) //wait .Dispose() { Console.ReadKey(); } void ondata(Dictionary streamValue) => Console.WriteLine(JsonConvert.SerializeObject(streamValue)); // NoAck xpending cli.XPending("stream_key", "FreeRedis__group", "-", "+", 10); ``` lpush + blpop: ```csharp using (cli.SubscribeList("list_key", ondata)) //wait .Dispose() { Console.ReadKey(); } void ondata(string listValue) => Console.WriteLine(listValue); ``` ### πŸ“ƒ Scripting ```csharp 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 ```csharp 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 ```csharp 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: switch database ```csharp using (var db = cli.GetDatabase(10)) { db.Set("key1", 10); var val1 = db.Get("key1"); } ``` ### πŸ” Scan > Support cluster mode ```csharp foreach (var keys in cli.Scan("*", 10, null)) { Console.WriteLine(string.Join(", ", keys)); } ``` ## πŸ‘― Contributors ## πŸ’• Donation > Thank you for your donation - [Alipay](https://www.cnblogs.com/FreeSql/gallery/image/338860.html) - [WeChat](https://www.cnblogs.com/FreeSql/gallery/image/338859.html) ## πŸ—„ License [MIT](LICENSE)