環境準備 1. Redis集群(Windows集群搭建) 啟動Redis集群,給每個節點加上Title start "Redis - 6379" /min redis-server.exe redis.6379.conf start "Redis - 6380" /min redis-server. ...
環境準備
1. Redis集群(Windows集群搭建)
啟動Redis集群,給每個節點加上Title
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包
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);
這裡只需要提供一個節點就能訪問整個集群
ConnectionMultiplexer 連接Redis可以使用ConfigurationOptions,或者直接字元串初始化(裡面實現也是調用ConfigurationOptions.Parse轉換)
這裡要說一下是他的重連重試機制,在第一次創建連接的時候,StackExchange.Redis會創建一個心跳檢測
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每秒鐘檢查一次。
StackExchange.Redis 實現兩種重試策略,當然我們也可以實現自己的重現策略,實現IReconnectRetryPolicy介面
- 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))); }
轉發請標明出處:https://www.cnblogs.com/WilsonPan/p/12677478.html
示例代碼: https://github.com/WilsonPan/Net.Demos/tree/master/Demo.RedisCluster
參考文章
StackExchange.Redis | General purpose redis client