/** * {@inheritDoc} */ public function add($source, $type, $unique, array $data, \DateTime $timestamp = null) { $identifier = base64_encode(implode(':', [$source, $type, $unique])); $timestamp = $timestamp ?: new \DateTime(); $this->redis->hset('aggregator:objects', $identifier, json_encode(['source' => $source, 'type' => $type, 'unique' => $unique, 'data' => $data, 'timestamp' => $timestamp->format('U')])); $this->redis->zadd("aggregator:sources:{$source}", $timestamp->format('U'), $identifier); }
/** * {@inheritdoc} */ public function later($delay, $job, $data = '', string $queue = null) { $payload = $this->createPayload($job, $data); $delay = $this->getSeconds($delay); $this->redis->zadd($this->getQueue($queue) . ':delayed', $this->getTime() + $delay, $payload); return Arr::get(json_decode($payload, true), 'id'); }
/** * Creates or modifies keys * * If $key already exists: * * - Strings: its value will be overwritten with $value * - Other types: $value items will be appended to its value * * Examples: * * ``` php * <?php * // Strings: $value must be a scalar * $I->haveInRedis('string', 'Obladi Oblada'); * * // Lists: $value can be a scalar or an array * $I->haveInRedis('list', ['riri', 'fifi', 'loulou']); * * // Sets: $value can be a scalar or an array * $I->haveInRedis('set', ['riri', 'fifi', 'loulou']); * * // ZSets: $value must be an associative array with scores * $I->haveInRedis('set', ['riri' => 1, 'fifi' => 2, 'loulou' => 3]); * * // Hashes: $value must be an associative array * $I->haveInRedis('hash', ['obladi' => 'oblada']); * ``` * * @param string $type The type of the key * @param string $key The key name * @param mixed $value The value * * @throws ModuleException */ public function haveInRedis($type, $key, $value) { switch (strtolower($type)) { case 'string': if (!is_scalar($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "string", ' . 'third argument must be a scalar'); } $this->driver->set($key, $value); break; case 'list': $this->driver->rpush($key, $value); break; case 'set': $this->driver->sadd($key, $value); break; case 'zset': if (!is_array($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "zset", ' . 'third argument must be an (associative) array'); } $this->driver->zadd($key, $value); break; case 'hash': if (!is_array($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "hash", ' . 'third argument must be an array'); } $this->driver->hmset($key, $value); break; default: throw new ModuleException($this, "Unknown type \"{$type}\" for key \"{$key}\". Allowed types are " . '"string", "list", "set", "zset", "hash"'); } }
/** * @inheritdoc */ public function release(array $message, $delay = 0) { if ($delay > 0) { $this->redis->zadd($message['queue'] . ':delayed', [$message['body'] => time() + $delay]); } else { $this->redis->rpush($message['queue'], [$message['body']]); } }
/** * * @param array $data * e.g: * [ * [score, member], * [score1, member1], * ... * ] * @param $key * @param callable $scoreMemberHandle * @return int|number */ protected function addDataListToZSet($key, array $data, callable $scoreMemberHandle) { if (!$data || !$key) { return 0; } // $pip = $this->redis->pipeline(); // $pip = $this->redis->watch($key); $this->redis->multi(); foreach ($data as $row) { list($score, $member) = $scoreMemberHandle($row); $this->redis->zadd($key, [(string) $member => $score]); } $result = $this->redis->exec(); return array_sum($result); }
/** * Push job to redis * * @param string $jobId * @param string $class * @param array $args * @param string $queue * @param bool $retry * @param float|null $doAt * @throws exception Exception */ private function atomicPush($jobId, $class, $args = [], $queue = self::QUEUE, $retry = true, $doAt = null) { if (array_values($args) !== $args) { throw new Exception('Associative arrays in job args are not allowed'); } if (!is_null($doAt) && !is_float($doAt) && is_string($doAt)) { throw new Exception('at argument needs to be in a unix epoch format. Use microtime(true).'); } $job = $this->serializer->serialize($jobId, $class, $args, $retry); if ($doAt === null) { $this->redis->sadd($this->name('queues'), $queue); $this->redis->lpush($this->name('queue', $queue), $job); } else { $this->redis->zadd($this->name('schedule'), [$job => $doAt]); } }
private function zSetSave() { return $this->client->zadd($this->key_header, array($this->message->getJsonFormat() => strtotime($this->message->getCreateAt()))); }