PhpRedisConnection.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Redis;
  4. use Closure;
  5. use Illuminate\Contracts\Redis\Connection as ConnectionContract;
  6. /**
  7. * @mixin \Redis
  8. */
  9. class PhpRedisConnection extends Connection implements ConnectionContract
  10. {
  11. /**
  12. * Create a new PhpRedis connection.
  13. *
  14. * @param \Redis $client
  15. * @return void
  16. */
  17. public function __construct($client)
  18. {
  19. $this->client = $client;
  20. }
  21. /**
  22. * Returns the value of the given key.
  23. *
  24. * @param string $key
  25. * @return string|null
  26. */
  27. public function get($key)
  28. {
  29. $result = $this->client->get($key);
  30. return $result !== false ? $result : null;
  31. }
  32. /**
  33. * Get the values of all the given keys.
  34. *
  35. * @param array $keys
  36. * @return array
  37. */
  38. public function mget(array $keys)
  39. {
  40. return array_map(function ($value) {
  41. return $value !== false ? $value : null;
  42. }, $this->client->mget($keys));
  43. }
  44. /**
  45. * Determine if the given keys exist.
  46. *
  47. * @param dynamic $keys
  48. * @return int
  49. */
  50. public function exists(...$keys)
  51. {
  52. $keys = collect($keys)->map(function ($key) {
  53. return $this->applyPrefix($key);
  54. })->all();
  55. return $this->executeRaw(array_merge(['exists'], $keys));
  56. }
  57. /**
  58. * Set the string value in argument as value of the key.
  59. *
  60. * @param string $key
  61. * @param mixed $value
  62. * @param string|null $expireResolution
  63. * @param int|null $expireTTL
  64. * @param string|null $flag
  65. * @return bool
  66. */
  67. public function set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
  68. {
  69. return $this->command('set', [
  70. $key,
  71. $value,
  72. $expireResolution ? [$flag, $expireResolution => $expireTTL] : null,
  73. ]);
  74. }
  75. /**
  76. * Set the given key if it doesn't exist.
  77. *
  78. * @param string $key
  79. * @param string $value
  80. * @return int
  81. */
  82. public function setnx($key, $value)
  83. {
  84. return (int) $this->client->setnx($key, $value);
  85. }
  86. /**
  87. * Get the value of the given hash fields.
  88. *
  89. * @param string $key
  90. * @param dynamic $dictionary
  91. * @return int
  92. */
  93. public function hmget($key, ...$dictionary)
  94. {
  95. if (count($dictionary) == 1) {
  96. $dictionary = $dictionary[0];
  97. }
  98. return array_values($this->command('hmget', [$key, $dictionary]));
  99. }
  100. /**
  101. * Set the given hash fields to their respective values.
  102. *
  103. * @param string $key
  104. * @param dynamic $dictionary
  105. * @return int
  106. */
  107. public function hmset($key, ...$dictionary)
  108. {
  109. if (count($dictionary) == 1) {
  110. $dictionary = $dictionary[0];
  111. } else {
  112. $input = collect($dictionary);
  113. $dictionary = $input->nth(2)->combine($input->nth(2, 1))->toArray();
  114. }
  115. return $this->command('hmset', [$key, $dictionary]);
  116. }
  117. /**
  118. * Set the given hash field if it doesn't exist.
  119. *
  120. * @param string $hash
  121. * @param string $key
  122. * @param string $value
  123. * @return int
  124. */
  125. public function hsetnx($hash, $key, $value)
  126. {
  127. return (int) $this->client->hsetnx($hash, $key, $value);
  128. }
  129. /**
  130. * Removes the first count occurrences of the value element from the list.
  131. *
  132. * @param string $key
  133. * @param int $count
  134. * @param $value $value
  135. * @return int|false
  136. */
  137. public function lrem($key, $count, $value)
  138. {
  139. return $this->command('lrem', [$key, $value, $count]);
  140. }
  141. /**
  142. * Removes and returns a random element from the set value at key.
  143. *
  144. * @param string $key
  145. * @param int|null $count
  146. * @return mixed|false
  147. */
  148. public function spop($key, $count = null)
  149. {
  150. return $this->command('spop', [$key]);
  151. }
  152. /**
  153. * Add one or more members to a sorted set or update its score if it already exists.
  154. *
  155. * @param string $key
  156. * @param dynamic $dictionary
  157. * @return int
  158. */
  159. public function zadd($key, ...$dictionary)
  160. {
  161. if (is_array(end($dictionary))) {
  162. foreach (array_pop($dictionary) as $member => $score) {
  163. $dictionary[] = $score;
  164. $dictionary[] = $member;
  165. }
  166. }
  167. $key = $this->applyPrefix($key);
  168. return $this->executeRaw(array_merge(['zadd', $key], $dictionary));
  169. }
  170. /**
  171. * Return elements with score between $min and $max.
  172. *
  173. * @param string $key
  174. * @param mixed $min
  175. * @param mixed $max
  176. * @param array $options
  177. * @return int
  178. */
  179. public function zrangebyscore($key, $min, $max, $options = [])
  180. {
  181. if (isset($options['limit'])) {
  182. $options['limit'] = [
  183. $options['limit']['offset'],
  184. $options['limit']['count'],
  185. ];
  186. }
  187. return $this->command('zRangeByScore', [$key, $min, $max, $options]);
  188. }
  189. /**
  190. * Return elements with score between $min and $max.
  191. *
  192. * @param string $key
  193. * @param mixed $min
  194. * @param mixed $max
  195. * @param array $options
  196. * @return int
  197. */
  198. public function zrevrangebyscore($key, $min, $max, $options = [])
  199. {
  200. if (isset($options['limit'])) {
  201. $options['limit'] = [
  202. $options['limit']['offset'],
  203. $options['limit']['count'],
  204. ];
  205. }
  206. return $this->command('zRevRangeByScore', [$key, $min, $max, $options]);
  207. }
  208. /**
  209. * Find the intersection between sets and store in a new set.
  210. *
  211. * @param string $output
  212. * @param array $keys
  213. * @param array $options
  214. * @return int
  215. */
  216. public function zinterstore($output, $keys, $options = [])
  217. {
  218. return $this->zInter($output, $keys,
  219. $options['weights'] ?? null,
  220. $options['aggregate'] ?? 'sum'
  221. );
  222. }
  223. /**
  224. * Find the union between sets and store in a new set.
  225. *
  226. * @param string $output
  227. * @param array $keys
  228. * @param array $options
  229. * @return int
  230. */
  231. public function zunionstore($output, $keys, $options = [])
  232. {
  233. return $this->zUnion($output, $keys,
  234. $options['weights'] ?? null,
  235. $options['aggregate'] ?? 'sum'
  236. );
  237. }
  238. /**
  239. * Execute commands in a pipeline.
  240. *
  241. * @param callable $callback
  242. * @return \Redis|array
  243. */
  244. public function pipeline(callable $callback = null)
  245. {
  246. $pipeline = $this->client()->pipeline();
  247. return is_null($callback)
  248. ? $pipeline
  249. : tap($pipeline, $callback)->exec();
  250. }
  251. /**
  252. * Execute commands in a transaction.
  253. *
  254. * @param callable $callback
  255. * @return \Redis|array
  256. */
  257. public function transaction(callable $callback = null)
  258. {
  259. $transaction = $this->client()->multi();
  260. return is_null($callback)
  261. ? $transaction
  262. : tap($transaction, $callback)->exec();
  263. }
  264. /**
  265. * Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
  266. *
  267. * @param string $script
  268. * @param int $numkeys
  269. * @param mixed $arguments
  270. * @return mixed
  271. */
  272. public function evalsha($script, $numkeys, ...$arguments)
  273. {
  274. return $this->command('evalsha', [
  275. $this->script('load', $script), $arguments, $numkeys,
  276. ]);
  277. }
  278. /**
  279. * Evaluate a script and return its result.
  280. *
  281. * @param string $script
  282. * @param int $numberOfKeys
  283. * @param dynamic $arguments
  284. * @return mixed
  285. */
  286. public function eval($script, $numberOfKeys, ...$arguments)
  287. {
  288. return $this->client->eval($script, $arguments, $numberOfKeys);
  289. }
  290. /**
  291. * Subscribe to a set of given channels for messages.
  292. *
  293. * @param array|string $channels
  294. * @param \Closure $callback
  295. * @return void
  296. */
  297. public function subscribe($channels, Closure $callback)
  298. {
  299. $this->client->subscribe((array) $channels, function ($redis, $channel, $message) use ($callback) {
  300. $callback($message, $channel);
  301. });
  302. }
  303. /**
  304. * Subscribe to a set of given channels with wildcards.
  305. *
  306. * @param array|string $channels
  307. * @param \Closure $callback
  308. * @return void
  309. */
  310. public function psubscribe($channels, Closure $callback)
  311. {
  312. $this->client->psubscribe((array) $channels, function ($redis, $pattern, $channel, $message) use ($callback) {
  313. $callback($message, $channel);
  314. });
  315. }
  316. /**
  317. * Subscribe to a set of given channels for messages.
  318. *
  319. * @param array|string $channels
  320. * @param \Closure $callback
  321. * @param string $method
  322. * @return void
  323. */
  324. public function createSubscription($channels, Closure $callback, $method = 'subscribe')
  325. {
  326. //
  327. }
  328. /**
  329. * Execute a raw command.
  330. *
  331. * @param array $parameters
  332. * @return mixed
  333. */
  334. public function executeRaw(array $parameters)
  335. {
  336. return $this->command('rawCommand', $parameters);
  337. }
  338. /**
  339. * Disconnects from the Redis instance.
  340. *
  341. * @return void
  342. */
  343. public function disconnect()
  344. {
  345. $this->client->close();
  346. }
  347. /**
  348. * Apply prefix to the given key if necessary.
  349. *
  350. * @param string $key
  351. * @return string
  352. */
  353. private function applyPrefix($key)
  354. {
  355. $prefix = (string) $this->client->getOption(Redis::OPT_PREFIX);
  356. return $prefix.$key;
  357. }
  358. /**
  359. * Pass other method calls down to the underlying client.
  360. *
  361. * @param string $method
  362. * @param array $parameters
  363. * @return mixed
  364. */
  365. public function __call($method, $parameters)
  366. {
  367. $method = strtolower($method);
  368. return parent::__call($method, $parameters);
  369. }
  370. }