• Stars
    star
    903
  • Rank 50,260 (Top 1.0 %)
  • Language
    C#
  • License
    MIT License
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🦄 FreeRedis is .NET40+ redis client. supports cluster, sentinel, master-slave, pub-sub, lua, pipeline, transaction, streams, client-side-caching, and pooling.

🦄 FreeRedis

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

nuget stats GitHub license

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);

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));
}

👯 Contributors

💕 Donation

Thank you for your donation

🗄 License

MIT

More Repositories

1

csredis

.NET Core or .NET Framework 4.0+ client for Redis and Redis Sentinel (2.8) and Cluster. Includes both synchronous and asynchronous clients.
C#
1,907
star
2

FreeIM

.NETCore websocket 实现简易、高性能、集群即时通讯组件,支持点对点通讯、群聊通讯、上线下线事件消息等众多实用性功能.
C#
1,223
star
3

dotnetGen_mysql

.NETCore + Mysql 生成器
C#
242
star
4

FreeSql.Tools

FreeSql 工具包,包括生成器等
C#
212
star
5

FreeSql.AdminLTE

这是一个 .NETCore MVC 中间件,基于 AdminLTE 前端框架动态产生 FreeSql 实体的增删查改界面。
C#
166
star
6

FreeScheduler

轻量化定时任务调度,支持临时的延时任务和重复循环任务(可持久化),可按秒,每天/每周/每月固定时间,自定义间隔执行,支持 .NET Core 2.1+、.NET Framework 4.0+ 运行环境。
C#
164
star
7

dotnetGen_sqlserver

.NETCore + SqlServer 生成器
C#
149
star
8

SafeObjectPool

应用场景:连接池,资源池等等
C#
112
star
9

IdleBus

IdleBus 空闲对象管理容器,有效组织对象重复利用,自动创建、销毁,解决【实例】过多且长时间占用的问题。
C#
107
star
10

dotnetGen_postgresql

.NETCore + PostgreSQL 生成器
C#
94
star
11

NPinyin

拼音汉字转换 .NETCore 版本
C#
80
star
12

FreeSql.Cloud

提供跨数据库访问,分布式事务TCC、SAGA解决方案。
C#
65
star
13

Microsoft.Extensions.Caching.CSRedis

分布式缓存,替代 Microsoft.Extensions.Caching.Redis
C#
44
star
14

FreeSql.DbContext

FreeSql 扩展包,实现真正的 ORM,Repository DbContext UnitOfWork 实现。
C#
41
star
15

FreeSql.DynamicProxy

The dynamic proxy on The .NetCore or .NetFramework4.0+. Support asynchronous method interception, Method parameter interception, Property interception, multiple intercepts, dependency injection and inversion of control
C#
38
star
16

TcpClientHttpRequest

基于 TcpClient 现实的 http请求库,编写于2007年做了几年数据采集工作。
C#
34
star
17

NJob

超级轻便的调度器
C#
30
star
18

FreeSql.Wiki.VuePress

FreeSql wiki 文档采用 vuepress
Dockerfile
30
star
19

MySocket

Socket服务端与客户端的封装(IOCP、EPOLL),支持.NETCore
C#
28
star
20

WorkQueue

超级轻便的线程队列工作器
C#
25
star
21

ojbk

模块化的单体应用项目
JavaScript
21
star
22

dotnetGen

.NET Framework 3.0 + SqlServer 生成器(停止更新)
C#
17
star
23

genms_shop

.NETCore 快速开发做一个简易商城
JavaScript
13
star
24

FreeSql.Connection.Extensions

Mysql, postgresql, sqlserver, Oracle and SQLite connection object extension methods.
C#
10
star
25

dng.Mysql

dotnetgen_mysql生成器所需MySql.Data的基础封装
C#
6
star
26

bmw.js

JavaScript
6
star
27

robot_test

简易任务调度
C#
6
star
28

dotnetGen_demo

dotnetGen 生成后的项目示例
C#
5
star
29

dng.Mssql

dotnetgen_sqlserver生成器所需System.Data.SqlClient的基础封装
C#
4
star
30

oss_signature

阿里云OSS服务端签名后直传,模仿官方get.php代码实现
C#
4
star
31

Spider

爬虫工具,.NET2.0实现
C#
4
star
32

AdminBlazor

AdminBlazor 是一款 Blazor SSR 后台管理项目,支持 RABC 权限菜单/按钮,支持一对一、一对多、多对多代码生成 .razor 界面。
HTML
4
star
33

dng.Pgsql

dotnetgen_postgresql生成器所需npgsql的基础封装
C#
3
star
34

dng.Template

模板引擎,不再使用(作纪念)
C#
3
star
35

cnodejs_netcore

.NETCore + MySql 实现的 cnodejs.org
JavaScript
2
star