FreeRedis 1.3.0
π¦ FreeRedis
FreeRedis is a redis client based on .NET, supports .NET Core 2.1+, .NET Framework 4.0+, Xamarin, and AOT.
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
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 | <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); |
| exitAutoDisposePool | true | AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress auto disposed |
| subscribeReadbytes | false | Subscribe read bytes |
IPv6: [fe80::b164:55b3:4b4f:7ce6%15]:6379
//FreeRedis.DistributedCache
//services.AddSingleton<IDistributedCache>(new FreeRedis.DistributedCache(cli));
π£ Master-Slave
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
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:
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
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
using (cli.Subscribe("abc", ondata)) //wait .Dispose()
{
Console.ReadKey();
}
void ondata(string channel, string data) =>
Console.WriteLine($"{channel} -> {data}");
xadd + xreadgroup:
using (cli.SubscribeStream("stream_key", ondata)) //wait .Dispose()
{
Console.ReadKey();
}
void ondata(Dictionary<string, string> streamValue) =>
Console.WriteLine(JsonConvert.SerializeObject(streamValue));
// NoAck xpending
cli.XPending("stream_key", "FreeRedis__group", "-", "+", 10);
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: switch database
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));
}
π‘DelayQueue
var delayQueue = cli.DelayQueue("TestDelayQueue");
//Add queue
delayQueue.Enqueue($"Execute in 5 seconds.", TimeSpan.FromSeconds(5));
delayQueue.Enqueue($"Execute in 10 seconds.", DateTime.Now.AddSeconds(10));
delayQueue.Enqueue($"Execute in 15 seconds.", DateTime.Now.AddSeconds(15));
delayQueue.Enqueue($"Execute in 20 seconds.", TimeSpan.FromSeconds(20));
delayQueue.Enqueue($"Execute in 25 seconds.", DateTime.Now.AddSeconds(25));
delayQueue.Enqueue($"Execute in 2024-07-02 14:30:15", DateTime.Parse("2024-07-02 14:30:15"));
//Consumption queue
await delayQueue.DequeueAsync(s =>
{
output.WriteLine($"{DateTime.Now}οΌ{s}");
return Task.CompletedTask;
});
π― Contributors
π Donation
Thank you for your donation
π License
Showing the top 20 packages that depend on FreeRedis.
| Packages | Downloads |
|---|---|
|
QC.Redis
redis服务
|
340 |
.NET Framework 4.0
- No dependencies.
.NET Framework 4.5.1
- No dependencies.
.NET Standard 2.0
- No dependencies.
| Version | Downloads | Last updated |
|---|---|---|
| 1.5.5 | 10 | 01/13/2026 |
| 1.5.3 | 2 | 11/28/2025 |
| 1.5.2 | 2 | 11/15/2025 |
| 1.5.1 | 2 | 11/14/2025 |
| 1.5.0 | 2 | 11/15/2025 |
| 1.4.2 | 2 | 11/15/2025 |
| 1.4.1 | 3 | 08/29/2025 |
| 1.4.0 | 3 | 06/18/2025 |
| 1.3.9 | 4 | 06/24/2025 |
| 1.3.8 | 4 | 06/24/2025 |
| 1.3.7 | 4 | 06/24/2025 |
| 1.3.6 | 4 | 06/24/2025 |
| 1.3.5 | 4 | 06/24/2025 |
| 1.3.4 | 4 | 06/24/2025 |
| 1.3.3 | 4 | 06/24/2025 |
| 1.3.2 | 4 | 06/24/2025 |
| 1.3.1 | 4 | 06/24/2025 |
| 1.3.0 | 4 | 06/24/2025 |
| 1.2.15 | 3 | 06/23/2025 |
| 1.2.14 | 2 | 11/28/2025 |
| 1.2.13 | 3 | 06/23/2025 |
| 1.2.12 | 3 | 06/23/2025 |
| 1.2.11 | 3 | 06/23/2025 |
| 1.2.10 | 3 | 06/23/2025 |
| 1.2.9 | 4 | 05/20/2025 |
| 1.2.7 | 3 | 05/20/2025 |
| 1.2.6 | 3 | 05/20/2025 |
| 1.2.5 | 3 | 05/20/2025 |
| 1.2.2 | 4 | 06/24/2025 |
| 1.1.12 | 4 | 05/21/2025 |
| 1.1.10 | 4 | 05/21/2025 |
| 1.1.9 | 4 | 06/24/2025 |
| 1.1.8 | 4 | 06/24/2025 |
| 1.1.7 | 4 | 06/24/2025 |
| 1.1.6 | 4 | 06/24/2025 |
| 1.1.5 | 4 | 06/24/2025 |
| 1.1.3 | 4 | 06/24/2025 |
| 1.1.2 | 4 | 06/24/2025 |
| 1.1.1 | 5 | 05/20/2025 |
| 1.1.0 | 4 | 06/24/2025 |
| 1.0.11 | 2 | 11/28/2025 |
| 1.0.10 | 2 | 11/28/2025 |
| 1.0.8 | 4 | 05/20/2025 |
| 1.0.7 | 3 | 05/20/2025 |
| 1.0.5 | 4 | 05/20/2025 |
| 1.0.4 | 4 | 05/20/2025 |
| 1.0.3 | 4 | 06/24/2025 |
| 1.0.2 | 3 | 05/20/2025 |
| 1.0.2-preview20220915 | 4 | 06/25/2025 |
| 1.0.1 | 4 | 06/24/2025 |
| 0.6.6 | 4 | 06/24/2025 |
| 0.6.5 | 4 | 05/20/2025 |
| 0.6.3 | 4 | 06/24/2025 |
| 0.6.2 | 4 | 05/20/2025 |
| 0.6.1 | 3 | 05/20/2025 |
| 0.5.9 | 3 | 05/20/2025 |
| 0.5.8 | 4 | 05/20/2025 |
| 0.5.5 | 4 | 06/24/2025 |
| 0.5.4 | 3 | 05/20/2025 |
| 0.5.3 | 4 | 06/24/2025 |
| 0.5.2 | 3 | 05/20/2025 |
| 0.5.1 | 4 | 02/26/2025 |
| 0.5.0 | 4 | 06/24/2025 |
| 0.3.7 | 56 | 12/10/2023 |
| 0.3.5 | 4 | 06/24/2025 |
| 0.3.0 | 4 | 06/24/2025 |
| 0.2.8 | 3 | 05/20/2025 |
| 0.2.7 | 4 | 05/20/2025 |
| 0.2.6 | 4 | 05/20/2025 |
| 0.2.5 | 3 | 05/20/2025 |
| 0.2.4 | 5 | 05/20/2025 |
| 0.2.1 | 4 | 05/20/2025 |
| 0.1.9 | 4 | 05/20/2025 |
| 0.1.8 | 3 | 05/20/2025 |
| 0.1.7 | 4 | 05/20/2025 |
| 0.1.6 | 4 | 05/20/2025 |
| 0.1.5 | 4 | 05/20/2025 |
| 0.1.3 | 4 | 06/24/2025 |
| 0.1.2 | 3 | 05/20/2025 |
| 0.1.0 | 4 | 06/24/2025 |
| 0.0.9 | 4 | 06/24/2025 |
| 0.0.8 | 4 | 06/24/2025 |
| 0.0.6 | 4 | 06/24/2025 |
| 0.0.5 | 4 | 06/24/2025 |