/// <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.IntersectAndStore(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.ZINTERSTORE, destination, parameters), queueJump));
        }
        internal static Aggregate ToAggregate(this RedisAggregate aggregate)
        {
            switch (aggregate)
            {
            case RedisAggregate.Sum:
                return(Aggregate.Sum);

            case RedisAggregate.Max:
                return(Aggregate.Max);

            case RedisAggregate.Min:
                return(Aggregate.Min);

            default:
                throw new Exception("RedisProxy 非法的聚合运算");
            }
        }
Beispiel #4
0
 public long SortedSetCombineAndStore(RedisSetOperation operation, string desctination, string first, string second, RedisAggregate aggregate = RedisAggregate.Sum)
 {
     return(Db.SortedSetCombineAndStore(operation.ToSetOperation(), desctination, first, second, aggregate.ToAggregate()));
 }
Beispiel #5
0
 public Task <long> SortedSetCombineAndStoreAsync(RedisSetOperation operation, string desctination, string[] keys, double[] weights = null, RedisAggregate aggregate = RedisAggregate.Sum)
 {
     return(Db.SortedSetCombineAndStoreAsync(operation.ToSetOperation(), desctination, keys.ToRedisKeyArray(), weights, aggregate.ToAggregate()));
 }
        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>
 /// 计算给定的一个或多个有序集的权限值并集,并将该并集(结果集)储存到 <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()));
        }
 public static CSRedis.RedisAggregate Convert(this RedisAggregate redisAggregate)
 {
     return((CSRedis.RedisAggregate)(int) redisAggregate);
 }
Beispiel #10
0
        /// <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());
        }
Beispiel #11
0
        /// <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"
        }