Coder Social home page Coder Social logo

elderjames / hiredis-dotnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 2881099/freeredis

0.0 1.0 0.0 2.38 MB

hiredis is .NET40+ redis client. supports redis cluster, redis sentinel, redis master-slave, redis pipeline, redis transaction, and redis pool.

License: MIT License

C# 100.00%

hiredis-dotnet's Introduction

🦄  hiredis-dotnet

nuget stats GitHub license

hiredis-dotnet is .NET redis client, supports .NETCore 2.1+, .NETFramework 4.0+, And Xamarin

  • RedisClient Keep all method names consistent with redis-cli
  • Support geo type commands (redis-server 3.2 or above is required)
  • Support Redis Cluster
  • Support Redis Sentinel
  • Support Redis Master-Slave
  • Supports stream type commands (requires redis-server 5.0 and above)
  • Supports Redis 6 RESP3 Protocol

QQ群:4336577(已满)、8578575(在线)、52508226(在线)

Single machine redis (单机)

public static RedisClient cli = new RedisClient("127.0.0.1:6379,password=123,defaultDatabase=13");

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
ssl false Enable encrypted transmission
name <empty> Connection name, use client list command to view
prefix <empty> key前辍,所有方法都会附带此前辍,cli.Set(prefix + "key", 111);

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

Master-Slave (读写分离)

public static 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");

写入时连接 127.0.0.1:6379,读取时随机连接 6380 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 //是否读写分离
    );

Redis Cluster (集群)

假如你有一个 Redis Cluster 集群,其中有三个主节点(7001-7003)、三个从节点(7004-7006),则连接此集群的代码:

var r = new RedisClient(new ConnectionStringBuilder[] { "192.168.0.2:7001", "192.168.0.2:7001", "192.168.0.2:7003" });

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

// or Async Callback

using (var pipe = cli.StartPipe())
{
    var tasks = new List<Task>();
    long t0 = 0;
    task.Add(pipe.IncrByAsync("key1", 10).ContinueWith(t => t0 = t.Result)); //callback

    pipe.SetAsync("key2", Null);

    string t2 = null;
    task.Add(pipe.GetAsync("key1").ContinueWith(t => t2 = t.Result)); //callback

    pipe.EndPipe();
    Task.WaitAll(tasks.ToArray()); //wait all callback
    Console.WriteLine(t0 + ", " + t2);
}

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

// or Async Callback

using (var tran = cli.Multi())
{
    var tasks = new List<Task>();
    long t0 = 0;
    task.Add(tran.IncrByAsync("key1", 10).ContinueWith(t => t0 = t.Result)); //callback

    tran.SetAsync("key2", Null);

    string t2 = null;
    task.Add(tran.GetAsync("key1").ContinueWith(t => t2 = t.Result)); //callback

    tran.Exec();
    Task.WaitAll(tasks.ToArray()); //wait all callback
    Console.WriteLine(t0 + ", " + t2);
}

💕  Donation

Thank you for your donation

hiredis-dotnet's People

Contributors

2881099 avatar whuanle avatar nmsazulx avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.