【Redis】 Cluster NetCore combat

Environmental preparation

1. Redis cluster ( Windows cluster build )

Start the Redis cluster and add Title to each node

start "Redis - 6379" /min redis-server.exe redis.6379.conf
start "Redis - 6380" /min redis-server.exe redis.6380.conf
start "Redis - 6381" /min redis-server.exe redis.6381.conf
start "Redis - 6382" /min redis-server.exe redis.6382.conf
start "Redis - 6383" /min redis-server.exe redis.6383.conf
start "Redis - 6384"/ min redis-server.exe redis. 6384 .conf

2. Install the StackExchange.Redis package

dotnet add package StackExchange.Redis

 

Connect with Redis

var configString = "127.0.0.1:6379";
var options = ConfigurationOptions.Parse(configString);
options.ReconnectRetryPolicy = new ExponentialRetry(5000);
var client = ConnectionMultiplexer.Connect(options);

Here only need to provide a node to access the entire cluster

ConnectionMultiplexer to connect to Redis can use ConfigurationOptions, or direct string initialization (the implementation is also called ConfigurationOptions.Parse conversion)

Let me talk about his reconnection retry mechanism. When the connection is first created, StackExchange.Redis will create a heartbeat detection

internal static IDisposable Create(ConnectionMultiplexer connection)
{
    var token = new TimerToken(connection);
    var timer = new Timer(Heartbeat, token, MillisecondsPerHeartbeat, MillisecondsPerHeartbeat);
    token.SetTimer(timer);
    return timer;
}

Use System.Threading.Timer to check every second.

StackExchange.Redis implements two retry strategies, of course we can also implement our own replay strategy and implement the IReconnectRetryPolicy interface

  •  ExponentialRetry (index)

   According to the specified time, continuously increase the range of random numbers according to the number of retries

  • LinearRetry

   Retry at fixed intervals according to the specified time

Common data structure operations

String operation

 

static void RedisStringOperation(ConnectionMultiplexer client)
{
    var db = client.GetDatabase();

    //单个Key操作
    db.StringSet("Key", "Wilson");
    Console.WriteLine($"Get Key : {db.StringGet("Key")}");

    db.StringSet("Nums", 1);
    db.StringIncrement("Nums");
    Console.WriteLine($"Get Nums : {db.StringGet("Nums")}");

    db.StringDecrement("Nums");
    Console.WriteLine($"Get Nums : {db.StringGet("Nums")}");

    //多个Key操作
    db.StringSet(new KeyValuePair<RedisKey, RedisValue>[]
    {
        new KeyValuePair<RedisKey, RedisValue>("{user}Name","Wilson"),
        new KeyValuePair<RedisKey, RedisValue>("{user}Age",30)
    });

    foreach (var value in db.StringGet(new RedisKey[] { "{user}Name", "{user}Age" }))
    {
        Console.WriteLine($"{value}");
    }
}

 

Hash operation

 

static void RedisHashOperation(ConnectionMultiplexer client)
{
    var db = client.GetDatabase();

    db.HashSet("person", "name", "Wilson");
    Console.WriteLine(db.HashGet("person", "name"));

    db.HashSet("person", new HashEntry[]
    {
        new HashEntry("name","Wilson"),
        new HashEntry("sex",1)
    });
    Console.WriteLine(string.Join("\n", db.HashGet("person", new RedisValue[] { "name", "sex" })));

    Console.WriteLine(string.Join("\n", db.HashGetAll("person")));
}

 

List operation

 

static void RedisListOperation(ConnectionMultiplexer client)
{
    var db = client.GetDatabase();

    db.ListLeftPush("list", 100);
    db.ListLeftPush("list", 200);
    db.ListLeftPush("list", 300);
    db.ListRightPush("list", 400);
    db.ListRightPush("list", 500);

    Console.WriteLine(db.ListLeftPop("list"));
    Console.WriteLine(db.ListRightPop("list"));
    Console.WriteLine(string.Join("\t", db.ListRange("list", 0, 2)));
    Console.WriteLine(db.ListLength("list"));
}

 

Set

 

static void RedisSetOperation(ConnectionMultiplexer client)
{
    var db = client.GetDatabase();

    db.SetAdd("user", "Wilson");
    db.SetAdd("user", "Wilson");
    db.SetAdd("user", "Alice");

    Console.WriteLine(db.SetLength("user"));
    Console.WriteLine(db.SetPop("user"));
    Console.WriteLine(string.Join("\n", db.SetMembers("user")));
}

 

Sorted Set

 

static void RedisSortedSetOperation(ConnectionMultiplexer client)
{
    var db = client.GetDatabase();

    db.KeyDelete("user");

    db.SortedSetAdd("user", "Wilson", 90);
    db.SortedSetAdd("user", "Alice", 85);
    db.SortedSetAdd("user", "Trenary", 12);
    db.SortedSetAdd("user", "Nixon", 30);

    Console.WriteLine(db.SortedSetLength("user"));
    Console.WriteLine(db.SortedSetRemove("user", "Wilson"));
    Console.WriteLine(string.Join("\n", db.SortedSetRangeByRank("user", 0, 2)));
}

 

Guess you like

Origin www.cnblogs.com/WilsonPan/p/12677478.html