环境准备
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(" ", db.HashGet("person", new RedisValue[] { "name", "sex" }))); Console.WriteLine(string.Join(" ", 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(" ", 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(" ", 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(" ", db.SortedSetRangeByRank("user", 0, 2))); }
转发请标明出处:https://www.cnblogs.com/WilsonPan/p/12677478.html
示例代码: https://github.com/WilsonPan/Net.Demos/tree/master/Demo.RedisCluster