Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册

🦄 FreeRedis


FreeRedis is a redis client based on .NET, supports .NET Core 2.1+, .NET Framework 4.0+, and Xamarin.

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


  1. ``` cs
  2. public static RedisClient cli = new RedisClient("127.0.0.1:6379,password=123,defaultDatabase=13");
  3. cli.Serialize = obj => JsonConvert.SerializeObject(obj);
  4. cli.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
  5. cli.Notice += (s, e) => Console.WriteLine(e.Log); //print command log

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

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

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 <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), 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 <empty> Connection name, use client list command to view
prefix <empty> The prefix of the key, all methods will have this prefix. cli.Set(prefix + "key", 111);

IPv6: [fe80::b164:55b3:4b4f:7ce6%15]:6379


  1. ``` cs
  2. //FreeRedis.DistributedCache
  3. //services.AddSingleton(new FreeRedis.DistributedCache(cli));
  4. ```

🎣 Master-Slave


  1. ``` cs
  2. public static RedisClient cli = new RedisClient(
  3.     "127.0.0.1:6379,password=123,defaultDatabase=13",
  4.     "127.0.0.1:6380,password=123,defaultDatabase=13",
  5.     "127.0.0.1:6381,password=123,defaultDatabase=13"
  6.     );

  7. var value = cli.Get("key1");
  8. ```

Write data at 127.0.0.1:6379; randomly read data from port 6380 or 6381.


⛳ Redis Sentinel


  1. ``` cs
  2. public static RedisClient cli = new RedisClient(
  3.     "mymaster,password=123",
  4.     new [] { "192.169.1.10:26379", "192.169.1.11:26379", "192.169.1.12:26379" },
  5.     true //This variable indicates whether to use the read-write separation mode.
  6.     );
  7. ```

🌌 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:

  1. ``` cs
  2. public static RedisClient cli = new RedisClient(
  3.     new ConnectionStringBuilder[] { "192.168.0.2:7001", "192.168.0.2:7002", "192.168.0.2:7003" }
  4.     );
  5. ```

⚡ Client-side-caching


requires redis-server 6.0 and above


  1. ``` cs
  2. cli.UseClientSideCaching(new ClientSideCachingOptions
  3. {
  4.     //Client cache capacity
  5.     Capacity = 3,
  6.     //Filtering rules, which specify which keys can be cached locally
  7.     KeyFilter = key => key.StartsWith("Interceptor"),
  8.     //Check long-term unused cache
  9.     CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(2)
  10. });
  11. ```

📡 Subscribe


  1. ``` cs
  2. using (cli.Subscribe("abc", ondata)) //wait .Dispose()
  3. {
  4.     Console.ReadKey();
  5. }

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

lpush + blpop:

  1. ``` cs
  2. using (cli.SubscribeList("list_key", ondata)) //wait .Dispose()
  3. {
  4.     Console.ReadKey();
  5. }

  6. void ondata(string listValue) =>
  7.     Console.WriteLine(listValue);
  8. ```

📃 Scripting


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

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

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

💻 Pipeline


  1. ``` cs
  2. using (var pipe = cli.StartPipe())
  3. {
  4.     pipe.IncrBy("key1", 10);
  5.     pipe.Set("key2", Null);
  6.     pipe.Get("key1");

  7.     object[] ret = pipe.EndPipe();
  8.     Console.WriteLine(ret[0] + ", " + ret[2]);
  9. }
  10. ```

📰 Transaction


  1. ``` cs
  2. using (var tran = cli.Multi())
  3. {
  4.     tran.IncrBy("key1", 10);
  5.     tran.Set("key2", Null);
  6.     tran.Get("key1");

  7.     object[] ret = tran.Exec();
  8.     Console.WriteLine(ret[0] + ", " + ret[2]);
  9. }
  10. ```

📯 GetDatabase: switch database


  1. ``` cs
  2. using (var db = cli.GetDatabase(10))
  3. {
  4.     db.Set("key1", 10);
  5.     var val1 = db.Get("key1");
  6. }
  7. ```

🔍 Scan


Support cluster mode


  1. ``` cs
  2. foreach (var keys in cli.Scan("*", 10, null))
  3. {
  4.     Console.WriteLine(string.Join(", ", keys));
  5. }
  6. ```

👯 Contributors


💕 Donation


Thank you for your donation


Alipay

WeChat

🗄 License


MIT
Last Updated: 2023-09-03 19:17:54