library/Rediska/Command/CompareSortedSets.php
public function create(array $keys, $storeKey, $aggregation = self::SUM)
    {
        if (empty($keys)) {
            throw new Rediska_Command_Exception('You must specify sorted sets');
        }

        // With weights?
        $withWeights = false;
        foreach($keys as $keyOrIndex => $keyOrWeight) {
            if (is_string($keyOrIndex)) {
                $this->_weights = $keys;
                $keys = array_keys($keys);
                $withWeights = true;
                break;
            }
        }

        $connections = array();
        $keysByConnections = array();
        foreach ($keys as $key) {
            $connection = $this->_rediska->getConnectionByKeyName($key);
            $connectionAlias = $connection->getAlias();
            if (!array_key_exists($connectionAlias, $connections)) {
                $connections[$connectionAlias] = $connection;
                $keysByConnections[$connectionAlias] = array();
            }
            $keysByConnections[$connectionAlias][] = $key;
        }

        // If only one connection, compare by redis
        if (count($connections) == 1) {
            $connectionValues = array_values($connections);
            $connection = $connectionValues[0];
            $storeConnection = $this->_rediska->getConnectionByKeyName($storeKey);

            if ($storeConnection === $connection) {
                $command = array($this->_command,
                                 $this->_rediska->getOption('namespace') . $storeKey,
                                 count($keys));

                foreach($keys as $key) {
                    $command[] = $this->_rediska->getOption('namespace') . $key;
                }
    
                if ($withWeights) {
                    $command[] = 'WEIGHTS';
                    $command = array_merge($command, $this->_weights);
                }

                if (strtolower($aggregation) != self::SUM) {
                    $command[] = 'AGGREGATE';
                    $command[] = strtoupper($aggregation);
                }

                return new Rediska_Connection_Exec($connection, $command);
            }
        }

        // Compare by hand
        
        // Set default weights
        if (!$withWeights) {
            $this->_weights = array_fill_keys($keys, 1);
        }

        $this->setAtomic(false);
        $commands = array();
        foreach($keysByConnections as $connectionAlias => $keys) {
            foreach($keys as $key) {
                $this->_keys[] = $key;
                $command = array('ZRANGE',
                                 $this->_rediska->getOption('namespace') . $key,
                                 0,
                                 -1,
                                 'WITHSCORES');
                $commands[] = new Rediska_Connection_Exec($connections[$connectionAlias], $command);
            }
        }

        return $commands;
    }
public function create(array $keys, $storeKey, $aggregation = self::SUM)
    {
        if (empty($keys)) {
            throw new Rediska_Command_Exception('You must specify sorted sets');
        }

        // With weights?
        $withWeights = false;
        foreach($keys as $keyOrIndex => $keyOrWeight) {
            if (is_string($keyOrIndex)) {
                $this->_weights = $keys;
                $keys = array_keys($keys);
                $withWeights = true;
                break;
            }
        }

        $connections = array();
        $keysByConnections = array();
        foreach ($keys as $key) {
            $connection = $this->_rediska->getConnectionByKeyName($key);
            $connectionAlias = $connection->getAlias();
            if (!array_key_exists($connectionAlias, $connections)) {
                $connections[$connectionAlias] = $connection;
                $keysByConnections[$connectionAlias] = array();
            }
            $keysByConnections[$connectionAlias][] = $key;
        }

        // If only one connection, compare by redis
        if (count($connections) == 1) {
            $connectionValues = array_values($connections);
            $connection = $connectionValues[0];
            $storeConnection = $this->_rediska->getConnectionByKeyName($storeKey);

            if ($storeConnection === $connection) {
                $command = array($this->_command,
                                 $this->_rediska->getOption('namespace') . $storeKey,
                                 count($keys));

                foreach($keys as $key) {
                    $command[] = $this->_rediska->getOption('namespace') . $key;
                }
    
                if ($withWeights) {
                    $command[] = 'WEIGHTS';
                    $command = array_merge($command, $this->_weights);
                }

                if (strtolower($aggregation) != self::SUM) {
                    $command[] = 'AGGREGATE';
                    $command[] = strtoupper($aggregation);
                }

                return new Rediska_Connection_Exec($connection, $command);
            }
        }

        // Compare by hand
        
        // Set default weights
        if (!$withWeights) {
            $this->_weights = array_fill_keys($keys, 1);
        }

        $this->setAtomic(false);
        $commands = array();
        foreach($keysByConnections as $connectionAlias => $keys) {
            foreach($keys as $key) {
                $this->_keys[] = $key;
                $command = array('ZRANGE',
                                 $this->_rediska->getOption('namespace') . $key,
                                 0,
                                 -1,
                                 'WITHSCORES');
                $commands[] = new Rediska_Connection_Exec($connections[$connectionAlias], $command);
            }
        }

        return $commands;
    }
Create command

@param array  $keys       Array of key names or associative array with weights
@param string $storeKey   Result sorted set key name
@param string $aggregation Aggregation method: SUM (for default), MIN, MAX.
@return Rediska_Connection_Exec