/// <summary> /// 鍙栨湁搴忛泦鍚堢殑骞堕泦 /// </summary> /// <param name="desKey"></param> /// <param name="zSetKeys"></param> /// <param name="aggregate"></param> /// <param name="poolType"></param> /// <returns></returns> public static long ZUnionStore(string desKey, string[] zSetKeys, RedisAggregate aggregate, RedisPoolType poolType) { long rtn = 0; PooledRedisClientManager pool = GetRedisPool(poolType); IRedisClient redis = pool.GetReadOnlyClient(); try { if (zSetKeys == null || zSetKeys.Length == 0) { redis.Remove(desKey); redis.AddItemToSortedSet(desKey, "-1", -1); rtn = 0; } else { string[] args = new string[] { "AGGREGATE", aggregate.ToString() }; rtn = redis.StoreUnionFromSortedSets(desKey, zSetKeys, args); } } catch { throw; } finally { if (redis != null) { redis.Dispose(); } } return(rtn); }
Task <long> ISortedSetCommands.UnionAndStore(int db, string destination, string[] keys, RedisAggregate aggregate, bool queueJump) { string[] parameters = new string[keys.Length + 3]; //prepend the number of keys and append the aggregate keyword and the aggregation type parameters[0] = keys.Length.ToString(); keys.CopyTo(parameters, 1); parameters[keys.Length + 1] = "AGGREGATE"; parameters[keys.Length + 2] = aggregate.ToString(); return(ExecuteInt64(RedisMessage.Create(db, RedisLiteral.ZUNIONSTORE, destination, parameters), queueJump)); }
/// <summary> /// 计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。 /// 默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之和. /// 关于 WEIGHTS 和 AGGREGATE 选项的描述,参见 ZUNIONSTORE 命令 /// </summary> /// <param name="destinationKey">目标键</param> /// <param name="keyWeights">键与乘法因子</param> /// <param name="aggregate">聚合方式</param> /// <returns>保存到 destination 的结果集的基数</returns> public int ZINTERSTORE(string destinationKey, IDictionary <string, double> keyWeights, RedisAggregate aggregate) { //ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] //Integer reply: the number of elements in the resulting sorted set at destination. if (string.IsNullOrEmpty(destinationKey)) { throw new ArgumentNullException("destinationKey"); } if (keyWeights == null || keyWeights.Count == 0) { throw new ArgumentNullException("keyWeights"); } // var keys = new string[keyWeights.Count]; var weights = new double[keyWeights.Count]; var i = 0; var e = keyWeights.GetEnumerator(); while (e.MoveNext()) { keys[i] = e.Current.Key; weights[i] = e.Current.Value; i++; } //ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] using (var w = new RedisWriter(this.client, 6 + keyWeights.Count * 2, "ZINTERSTORE")) { w.WriteArgument(destinationKey); w.WriteArgument(keyWeights.Count.ToString()); foreach (var key in keys) { w.WriteArgument(key); } w.WriteArgument("WEIGHTS"); foreach (var weight in weights) { w.WriteArgument(weight.ToString()); } w.WriteArgument("AGGREGATE"); w.WriteArgument(aggregate.ToString()); this.connection.SendCommand(w); } return(this.connection.ExpectInt()); }
Task<long> ISortedSetCommands.UnionAndStore(int db, string destination, string[] keys, RedisAggregate aggregate, bool queueJump) { var parameters = new string[keys.Length + 3]; //prepend the number of keys and append the aggregate keyword and the aggregation type parameters[0] = keys.Length.ToString(); keys.CopyTo(parameters, 1); parameters[keys.Length + 1] = "AGGREGATE"; parameters[keys.Length + 2] = aggregate.ToString(); return ExecuteInt64(RedisMessage.Create(db, RedisLiteral.ZUNIONSTORE, destination, parameters), queueJump); }
/// <summary> /// 计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。 /// 默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。 /// </summary> /// <param name="destinationKey">目标键</param> /// <param name="keyWeights">键与乘法因子</param> /// <param name="aggregate">聚合方式</param> /// <returns>保存到 destination 的结果集的基数</returns> public int ZUNIONSTORE(string destinationKey, IDictionary <string, double> keyWeights, RedisAggregate aggregate) { //ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] //Integer reply: the number of elements in the resulting sorted set at destination. if (string.IsNullOrEmpty(destinationKey)) { throw new ArgumentNullException("destinationKey"); } if (keyWeights == null || keyWeights.Count == 0) { throw new ArgumentNullException("keyWeights"); } // var keys = new string[keyWeights.Count]; var weights = new double[keyWeights.Count]; var i = 0; var e = keyWeights.GetEnumerator(); while (e.MoveNext()) { keys[i] = e.Current.Key; weights[i] = e.Current.Value; i++; } //ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] using (var w = new RedisWriter(this.client, 6 + keyWeights.Count * 2, "ZUNIONSTORE")) { w.WriteArgument(destinationKey); w.WriteArgument(keyWeights.Count.ToString()); foreach (var key in keys) { w.WriteArgument(key); } w.WriteArgument("WEIGHTS"); foreach (var weight in weights) { w.WriteArgument(weight.ToString()); } w.WriteArgument("AGGREGATE"); w.WriteArgument(aggregate.ToString()); this.connection.SendCommand(w); } return(this.connection.ExpectInt()); //示例 // redis> ZRANGE programmer 0 -1 WITHSCORES //1) "peter" //2) "2000" //3) "jack" //4) "3500" //5) "tom" //6) "5000" //redis> ZRANGE manager 0 -1 WITHSCORES //1) "herry" //2) "2000" //3) "mary" //4) "3500" //5) "bob" //6) "4000" //redis> ZUNIONSTORE salary 2 programmer manager WEIGHTS 1 3 //(integer) 6 //redis> ZRANGE salary 0 -1 WITHSCORES //1) "peter" //2) "2000" //3) "jack" //4) "3500" //5) "tom" //6) "5000" //7) "herry" //8) "6000" //9) "mary" //10) "10500" //11) "bob" //12) "12000" }