【Redis】集羣NetCore實戰

環境準備html

1. Redis集羣(Windows集羣搭建git

啓動Redis集羣,給每一個節點加上Titlegithub

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. 安裝StackExchange.Redis包redis

dotnet add package StackExchange.Redis

 

鏈接Redis數據結構

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

這裏只須要提供一個節點就能訪問整個集羣spa

ConnectionMultiplexer 鏈接Redis能夠使用ConfigurationOptions,或者直接字符串初始化(裏面實現也是調用ConfigurationOptions.Parse轉換)code

這裏要說一下是他的重連重試機制,在第一次建立鏈接的時候,StackExchange.Redis會建立一個心跳檢測server

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

使用System.Threading.Timer每秒鐘檢查一次。htm

StackExchange.Redis 實現兩種重試策略,固然咱們也能夠實現本身的重現策略,實現IReconnectRetryPolicy接口blog

  •  ExponentialRetry(指數)

   根據指定時間,根據重試次數不斷增大隨機數範圍

  • LinearRetry(線性)

   根據指定時間固定時間間隔重試

經常使用數據結構操做

字符串(String)操做

 

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)操做

 

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) 操做

 

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

 

相關文章
相關標籤/搜索