123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
-
- namespace Illuminate\Redis\Connections;
-
- use Redis;
- use Closure;
- use Illuminate\Contracts\Redis\Connection as ConnectionContract;
-
- /**
- * @mixin \Redis
- */
- class PhpRedisConnection extends Connection implements ConnectionContract
- {
- /**
- * Create a new PhpRedis connection.
- *
- * @param \Redis $client
- * @return void
- */
- public function __construct($client)
- {
- $this->client = $client;
- }
-
- /**
- * Returns the value of the given key.
- *
- * @param string $key
- * @return string|null
- */
- public function get($key)
- {
- $result = $this->client->get($key);
-
- return $result !== false ? $result : null;
- }
-
- /**
- * Get the values of all the given keys.
- *
- * @param array $keys
- * @return array
- */
- public function mget(array $keys)
- {
- return array_map(function ($value) {
- return $value !== false ? $value : null;
- }, $this->client->mget($keys));
- }
-
- /**
- * Determine if the given keys exist.
- *
- * @param dynamic $keys
- * @return int
- */
- public function exists(...$keys)
- {
- $keys = collect($keys)->map(function ($key) {
- return $this->applyPrefix($key);
- })->all();
-
- return $this->executeRaw(array_merge(['exists'], $keys));
- }
-
- /**
- * Set the string value in argument as value of the key.
- *
- * @param string $key
- * @param mixed $value
- * @param string|null $expireResolution
- * @param int|null $expireTTL
- * @param string|null $flag
- * @return bool
- */
- public function set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
- {
- return $this->command('set', [
- $key,
- $value,
- $expireResolution ? [$flag, $expireResolution => $expireTTL] : null,
- ]);
- }
-
- /**
- * Set the given key if it doesn't exist.
- *
- * @param string $key
- * @param string $value
- * @return int
- */
- public function setnx($key, $value)
- {
- return (int) $this->client->setnx($key, $value);
- }
-
- /**
- * Get the value of the given hash fields.
- *
- * @param string $key
- * @param dynamic $dictionary
- * @return int
- */
- public function hmget($key, ...$dictionary)
- {
- if (count($dictionary) == 1) {
- $dictionary = $dictionary[0];
- }
-
- return array_values($this->command('hmget', [$key, $dictionary]));
- }
-
- /**
- * Set the given hash fields to their respective values.
- *
- * @param string $key
- * @param dynamic $dictionary
- * @return int
- */
- public function hmset($key, ...$dictionary)
- {
- if (count($dictionary) == 1) {
- $dictionary = $dictionary[0];
- } else {
- $input = collect($dictionary);
-
- $dictionary = $input->nth(2)->combine($input->nth(2, 1))->toArray();
- }
-
- return $this->command('hmset', [$key, $dictionary]);
- }
-
- /**
- * Set the given hash field if it doesn't exist.
- *
- * @param string $hash
- * @param string $key
- * @param string $value
- * @return int
- */
- public function hsetnx($hash, $key, $value)
- {
- return (int) $this->client->hsetnx($hash, $key, $value);
- }
-
- /**
- * Removes the first count occurrences of the value element from the list.
- *
- * @param string $key
- * @param int $count
- * @param $value $value
- * @return int|false
- */
- public function lrem($key, $count, $value)
- {
- return $this->command('lrem', [$key, $value, $count]);
- }
-
- /**
- * Removes and returns a random element from the set value at key.
- *
- * @param string $key
- * @param int|null $count
- * @return mixed|false
- */
- public function spop($key, $count = null)
- {
- return $this->command('spop', [$key]);
- }
-
- /**
- * Add one or more members to a sorted set or update its score if it already exists.
- *
- * @param string $key
- * @param dynamic $dictionary
- * @return int
- */
- public function zadd($key, ...$dictionary)
- {
- if (is_array(end($dictionary))) {
- foreach (array_pop($dictionary) as $member => $score) {
- $dictionary[] = $score;
- $dictionary[] = $member;
- }
- }
-
- $key = $this->applyPrefix($key);
-
- return $this->executeRaw(array_merge(['zadd', $key], $dictionary));
- }
-
- /**
- * Return elements with score between $min and $max.
- *
- * @param string $key
- * @param mixed $min
- * @param mixed $max
- * @param array $options
- * @return int
- */
- public function zrangebyscore($key, $min, $max, $options = [])
- {
- if (isset($options['limit'])) {
- $options['limit'] = [
- $options['limit']['offset'],
- $options['limit']['count'],
- ];
- }
-
- return $this->command('zRangeByScore', [$key, $min, $max, $options]);
- }
-
- /**
- * Return elements with score between $min and $max.
- *
- * @param string $key
- * @param mixed $min
- * @param mixed $max
- * @param array $options
- * @return int
- */
- public function zrevrangebyscore($key, $min, $max, $options = [])
- {
- if (isset($options['limit'])) {
- $options['limit'] = [
- $options['limit']['offset'],
- $options['limit']['count'],
- ];
- }
-
- return $this->command('zRevRangeByScore', [$key, $min, $max, $options]);
- }
-
- /**
- * Find the intersection between sets and store in a new set.
- *
- * @param string $output
- * @param array $keys
- * @param array $options
- * @return int
- */
- public function zinterstore($output, $keys, $options = [])
- {
- return $this->zInter($output, $keys,
- $options['weights'] ?? null,
- $options['aggregate'] ?? 'sum'
- );
- }
-
- /**
- * Find the union between sets and store in a new set.
- *
- * @param string $output
- * @param array $keys
- * @param array $options
- * @return int
- */
- public function zunionstore($output, $keys, $options = [])
- {
- return $this->zUnion($output, $keys,
- $options['weights'] ?? null,
- $options['aggregate'] ?? 'sum'
- );
- }
-
- /**
- * Execute commands in a pipeline.
- *
- * @param callable $callback
- * @return \Redis|array
- */
- public function pipeline(callable $callback = null)
- {
- $pipeline = $this->client()->pipeline();
-
- return is_null($callback)
- ? $pipeline
- : tap($pipeline, $callback)->exec();
- }
-
- /**
- * Execute commands in a transaction.
- *
- * @param callable $callback
- * @return \Redis|array
- */
- public function transaction(callable $callback = null)
- {
- $transaction = $this->client()->multi();
-
- return is_null($callback)
- ? $transaction
- : tap($transaction, $callback)->exec();
- }
-
- /**
- * Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
- *
- * @param string $script
- * @param int $numkeys
- * @param mixed $arguments
- * @return mixed
- */
- public function evalsha($script, $numkeys, ...$arguments)
- {
- return $this->command('evalsha', [
- $this->script('load', $script), $arguments, $numkeys,
- ]);
- }
-
- /**
- * Evaluate a script and return its result.
- *
- * @param string $script
- * @param int $numberOfKeys
- * @param dynamic $arguments
- * @return mixed
- */
- public function eval($script, $numberOfKeys, ...$arguments)
- {
- return $this->client->eval($script, $arguments, $numberOfKeys);
- }
-
- /**
- * Subscribe to a set of given channels for messages.
- *
- * @param array|string $channels
- * @param \Closure $callback
- * @return void
- */
- public function subscribe($channels, Closure $callback)
- {
- $this->client->subscribe((array) $channels, function ($redis, $channel, $message) use ($callback) {
- $callback($message, $channel);
- });
- }
-
- /**
- * Subscribe to a set of given channels with wildcards.
- *
- * @param array|string $channels
- * @param \Closure $callback
- * @return void
- */
- public function psubscribe($channels, Closure $callback)
- {
- $this->client->psubscribe((array) $channels, function ($redis, $pattern, $channel, $message) use ($callback) {
- $callback($message, $channel);
- });
- }
-
- /**
- * Subscribe to a set of given channels for messages.
- *
- * @param array|string $channels
- * @param \Closure $callback
- * @param string $method
- * @return void
- */
- public function createSubscription($channels, Closure $callback, $method = 'subscribe')
- {
- //
- }
-
- /**
- * Execute a raw command.
- *
- * @param array $parameters
- * @return mixed
- */
- public function executeRaw(array $parameters)
- {
- return $this->command('rawCommand', $parameters);
- }
-
- /**
- * Disconnects from the Redis instance.
- *
- * @return void
- */
- public function disconnect()
- {
- $this->client->close();
- }
-
- /**
- * Apply prefix to the given key if necessary.
- *
- * @param string $key
- * @return string
- */
- private function applyPrefix($key)
- {
- $prefix = (string) $this->client->getOption(Redis::OPT_PREFIX);
-
- return $prefix.$key;
- }
-
- /**
- * Pass other method calls down to the underlying client.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- $method = strtolower($method);
-
- return parent::__call($method, $parameters);
- }
- }
|