public void TestZInterStore() { string reply = ":2\r\n"; using(var mock = new MockConnector("localhost", 9999, reply, reply, reply, reply, reply)) using(var redis = new RedisClient(mock)) { Assert.Equal(2, redis.ZInterStore("destination", new RedisWeightDictionary("key1", "key2"))); Assert.Equal("*5\r\n$11\r\nZINTERSTORE\r\n$11\r\ndestination\r\n$1\r\n2\r\n$4\r\nkey1\r\n$4\r\nkey2\r\n", mock.GetMessage()); var dict = new RedisWeightDictionary(new[] { "key1", "key2" }, new[] { 1D, 2D }); Assert.Equal(2, redis.ZInterStore("destination", dict)); Assert.Equal("*8\r\n$11\r\nZINTERSTORE\r\n$11\r\ndestination\r\n$1\r\n2\r\n$4\r\nkey1\r\n$4\r\nkey2\r\n$7\r\nWEIGHTS\r\n$1\r\n1\r\n$1\r\n2\r\n", mock.GetMessage()); Assert.Equal(2, redis.ZInterStore("destination", dict, RedisAggregate.Max)); Assert.Equal("*10\r\n$11\r\nZINTERSTORE\r\n$11\r\ndestination\r\n$1\r\n2\r\n$4\r\nkey1\r\n$4\r\nkey2\r\n$7\r\nWEIGHTS\r\n$1\r\n1\r\n$1\r\n2\r\n$9\r\nAGGREGATE\r\n$3\r\nMAX\r\n", mock.GetMessage()); Assert.Equal(2, redis.ZInterStore("destination", dict, RedisAggregate.Min)); Assert.Equal("*10\r\n$11\r\nZINTERSTORE\r\n$11\r\ndestination\r\n$1\r\n2\r\n$4\r\nkey1\r\n$4\r\nkey2\r\n$7\r\nWEIGHTS\r\n$1\r\n1\r\n$1\r\n2\r\n$9\r\nAGGREGATE\r\n$3\r\nMIN\r\n", mock.GetMessage()); Assert.Equal(2, redis.ZInterStore("destination", dict, RedisAggregate.Sum)); Assert.Equal("*10\r\n$11\r\nZINTERSTORE\r\n$11\r\ndestination\r\n$1\r\n2\r\n$4\r\nkey1\r\n$4\r\nkey2\r\n$7\r\nWEIGHTS\r\n$1\r\n1\r\n$1\r\n2\r\n$9\r\nAGGREGATE\r\n$3\r\nSUM\r\n", mock.GetMessage()); } }
/// <summary> /// 计算给定的一个或多个有序集的权限值并集,并将该并集(结果集)储存到 <paramref name="destination"/> 。 /// </summary> /// <param name="client">Redis 客户端。</param> /// <param name="destination">目标有序集的键名。如果有序集已存在,则会覆盖。</param> /// <param name="keyWeights">有序集键名和乘法因子的字典。</param> /// <param name="aggregate">聚合的方式。</param> /// <returns>保存到 <paramref name="destination"/> 的结果集的基数。</returns> public static long ZUnionStore(this IRedisClient client, string destination, RedisWeightDictionary keyWeights, RedisAggregate? aggregate = null) { return ZStore("ZUNIONSTORE", client, destination, keyWeights, aggregate); }
private static long ZStore(string command, IRedisClient client, string destination, RedisWeightDictionary keyWeights, RedisAggregate? aggregate) { if(client == null) throw new ArgumentNullException(nameof(client)); if(string.IsNullOrWhiteSpace(destination)) throw new ArgumentNullException(nameof(destination)); if(keyWeights == null) throw new ArgumentNullException(nameof(keyWeights)); if(keyWeights.Count == 0) return 0L; IEnumerable<object> args = new object[] { destination, keyWeights.Count }; args = RedisArgs.ConcatAll(args, keyWeights.Keys); if(keyWeights.Values.Where(weight => weight != 1.0).Count() > 0) { args = RedisArgs.ConcatAll(RedisArgs.ConcatLast(args, "WEIGHTS"), keyWeights.Values.Cast<object>()); } if(aggregate.HasValue) { args = RedisArgs.ConcatLasts(args, "AGGREGATE", aggregate.Value.ToString().ToUpperInvariant()); } return client.Execute(new RedisInteger(command, args.ToArray())); }
/// <summary> /// 计算给定的一个或多个有序集的权限值并集,并将该并集(结果集)储存到 <paramref name="destination"/> 。 /// </summary> /// <param name="client">Redis 客户端。</param> /// <param name="destination">目标有序集的键名。如果有序集已存在,则会覆盖。</param> /// <param name="keyWeights">有序集键名和乘法因子的字典。</param> /// <param name="aggregate">聚合的方式。</param> /// <returns>保存到 <paramref name="destination"/> 的结果集的基数。</returns> public static long ZUnionStore(this IRedisClient client, string destination, RedisWeightDictionary keyWeights, RedisAggregate?aggregate = null) { return(ZStore("ZUNIONSTORE", client, destination, keyWeights, aggregate)); }
private static long ZStore(string command, IRedisClient client, string destination, RedisWeightDictionary keyWeights, RedisAggregate?aggregate) { if (client == null) { throw new ArgumentNullException("client"); } if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException("destination"); } if (keyWeights == null) { throw new ArgumentNullException("keyWeights"); } if (keyWeights.Count == 0) { return(0L); } IEnumerable <object> args = new object[] { destination, keyWeights.Count }; args = RedisArgs.ConcatAll(args, keyWeights.Keys); if (keyWeights.Values.Where(weight => weight != 1.0).Count() > 0) { args = RedisArgs.ConcatAll(RedisArgs.ConcatLast(args, "WEIGHTS"), keyWeights.Values.Cast <object>()); } if (aggregate.HasValue) { args = RedisArgs.ConcatLasts(args, "AGGREGATE", aggregate.Value.ToString().ToUpperInvariant()); } return(client.Execute(new RedisInteger(command, args.ToArray()))); }