PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel_tintuc/vendor/laravel/framework/src/Illuminate/Redis/Connections/PhpRedisConnection.php

https://gitlab.com/nmhieucoder/laravel_tintuc
PHP | 576 lines | 253 code | 63 blank | 260 comment | 19 complexity | f06dae80395f756d9647c9c53d580d26 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Closure;
  4. use Illuminate\Contracts\Redis\Connection as ConnectionContract;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Str;
  7. use Redis;
  8. use RedisCluster;
  9. use RedisException;
  10. /**
  11. * @mixin \Redis
  12. */
  13. class PhpRedisConnection extends Connection implements ConnectionContract
  14. {
  15. /**
  16. * The connection creation callback.
  17. *
  18. * @var callable
  19. */
  20. protected $connector;
  21. /**
  22. * The connection configuration array.
  23. *
  24. * @var array
  25. */
  26. protected $config;
  27. /**
  28. * Create a new PhpRedis connection.
  29. *
  30. * @param \Redis $client
  31. * @param callable|null $connector
  32. * @param array $config
  33. * @return void
  34. */
  35. public function __construct($client, callable $connector = null, array $config = [])
  36. {
  37. $this->client = $client;
  38. $this->config = $config;
  39. $this->connector = $connector;
  40. }
  41. /**
  42. * Returns the value of the given key.
  43. *
  44. * @param string $key
  45. * @return string|null
  46. */
  47. public function get($key)
  48. {
  49. $result = $this->command('get', [$key]);
  50. return $result !== false ? $result : null;
  51. }
  52. /**
  53. * Get the values of all the given keys.
  54. *
  55. * @param array $keys
  56. * @return array
  57. */
  58. public function mget(array $keys)
  59. {
  60. return array_map(function ($value) {
  61. return $value !== false ? $value : null;
  62. }, $this->command('mget', [$keys]));
  63. }
  64. /**
  65. * Set the string value in the argument as the value of the key.
  66. *
  67. * @param string $key
  68. * @param mixed $value
  69. * @param string|null $expireResolution
  70. * @param int|null $expireTTL
  71. * @param string|null $flag
  72. * @return bool
  73. */
  74. public function set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
  75. {
  76. return $this->command('set', [
  77. $key,
  78. $value,
  79. $expireResolution ? [$flag, $expireResolution => $expireTTL] : null,
  80. ]);
  81. }
  82. /**
  83. * Set the given key if it doesn't exist.
  84. *
  85. * @param string $key
  86. * @param string $value
  87. * @return int
  88. */
  89. public function setnx($key, $value)
  90. {
  91. return (int) $this->command('setnx', [$key, $value]);
  92. }
  93. /**
  94. * Get the value of the given hash fields.
  95. *
  96. * @param string $key
  97. * @param mixed $dictionary
  98. * @return array
  99. */
  100. public function hmget($key, ...$dictionary)
  101. {
  102. if (count($dictionary) === 1) {
  103. $dictionary = $dictionary[0];
  104. }
  105. return array_values($this->command('hmget', [$key, $dictionary]));
  106. }
  107. /**
  108. * Set the given hash fields to their respective values.
  109. *
  110. * @param string $key
  111. * @param mixed $dictionary
  112. * @return int
  113. */
  114. public function hmset($key, ...$dictionary)
  115. {
  116. if (count($dictionary) === 1) {
  117. $dictionary = $dictionary[0];
  118. } else {
  119. $input = collect($dictionary);
  120. $dictionary = $input->nth(2)->combine($input->nth(2, 1))->toArray();
  121. }
  122. return $this->command('hmset', [$key, $dictionary]);
  123. }
  124. /**
  125. * Set the given hash field if it doesn't exist.
  126. *
  127. * @param string $hash
  128. * @param string $key
  129. * @param string $value
  130. * @return int
  131. */
  132. public function hsetnx($hash, $key, $value)
  133. {
  134. return (int) $this->command('hsetnx', [$hash, $key, $value]);
  135. }
  136. /**
  137. * Removes the first count occurrences of the value element from the list.
  138. *
  139. * @param string $key
  140. * @param int $count
  141. * @param mixed $value
  142. * @return int|false
  143. */
  144. public function lrem($key, $count, $value)
  145. {
  146. return $this->command('lrem', [$key, $value, $count]);
  147. }
  148. /**
  149. * Removes and returns the first element of the list stored at key.
  150. *
  151. * @param mixed $arguments
  152. * @return array|null
  153. */
  154. public function blpop(...$arguments)
  155. {
  156. $result = $this->command('blpop', $arguments);
  157. return empty($result) ? null : $result;
  158. }
  159. /**
  160. * Removes and returns the last element of the list stored at key.
  161. *
  162. * @param mixed $arguments
  163. * @return array|null
  164. */
  165. public function brpop(...$arguments)
  166. {
  167. $result = $this->command('brpop', $arguments);
  168. return empty($result) ? null : $result;
  169. }
  170. /**
  171. * Removes and returns a random element from the set value at key.
  172. *
  173. * @param string $key
  174. * @param int|null $count
  175. * @return mixed|false
  176. */
  177. public function spop($key, $count = 1)
  178. {
  179. return $this->command('spop', func_get_args());
  180. }
  181. /**
  182. * Add one or more members to a sorted set or update its score if it already exists.
  183. *
  184. * @param string $key
  185. * @param mixed $dictionary
  186. * @return int
  187. */
  188. public function zadd($key, ...$dictionary)
  189. {
  190. if (is_array(end($dictionary))) {
  191. foreach (array_pop($dictionary) as $member => $score) {
  192. $dictionary[] = $score;
  193. $dictionary[] = $member;
  194. }
  195. }
  196. $options = [];
  197. foreach (array_slice($dictionary, 0, 3) as $i => $value) {
  198. if (in_array($value, ['nx', 'xx', 'ch', 'incr', 'NX', 'XX', 'CH', 'INCR'], true)) {
  199. $options[] = $value;
  200. unset($dictionary[$i]);
  201. }
  202. }
  203. return $this->command('zadd', array_merge([$key], [$options], array_values($dictionary)));
  204. }
  205. /**
  206. * Return elements with score between $min and $max.
  207. *
  208. * @param string $key
  209. * @param mixed $min
  210. * @param mixed $max
  211. * @param array $options
  212. * @return array
  213. */
  214. public function zrangebyscore($key, $min, $max, $options = [])
  215. {
  216. if (isset($options['limit']) && Arr::isAssoc($options['limit'])) {
  217. $options['limit'] = [
  218. $options['limit']['offset'],
  219. $options['limit']['count'],
  220. ];
  221. }
  222. return $this->command('zRangeByScore', [$key, $min, $max, $options]);
  223. }
  224. /**
  225. * Return elements with score between $min and $max.
  226. *
  227. * @param string $key
  228. * @param mixed $min
  229. * @param mixed $max
  230. * @param array $options
  231. * @return array
  232. */
  233. public function zrevrangebyscore($key, $min, $max, $options = [])
  234. {
  235. if (isset($options['limit']) && Arr::isAssoc($options['limit'])) {
  236. $options['limit'] = [
  237. $options['limit']['offset'],
  238. $options['limit']['count'],
  239. ];
  240. }
  241. return $this->command('zRevRangeByScore', [$key, $min, $max, $options]);
  242. }
  243. /**
  244. * Find the intersection between sets and store in a new set.
  245. *
  246. * @param string $output
  247. * @param array $keys
  248. * @param array $options
  249. * @return int
  250. */
  251. public function zinterstore($output, $keys, $options = [])
  252. {
  253. return $this->command('zinterstore', [$output, $keys,
  254. $options['weights'] ?? null,
  255. $options['aggregate'] ?? 'sum',
  256. ]);
  257. }
  258. /**
  259. * Find the union between sets and store in a new set.
  260. *
  261. * @param string $output
  262. * @param array $keys
  263. * @param array $options
  264. * @return int
  265. */
  266. public function zunionstore($output, $keys, $options = [])
  267. {
  268. return $this->command('zunionstore', [$output, $keys,
  269. $options['weights'] ?? null,
  270. $options['aggregate'] ?? 'sum',
  271. ]);
  272. }
  273. /**
  274. * Scans all keys based on options.
  275. *
  276. * @param mixed $cursor
  277. * @param array $options
  278. * @return mixed
  279. */
  280. public function scan($cursor, $options = [])
  281. {
  282. $result = $this->client->scan($cursor,
  283. $options['match'] ?? '*',
  284. $options['count'] ?? 10
  285. );
  286. if ($result === false) {
  287. $result = [];
  288. }
  289. return $cursor === 0 && empty($result) ? false : [$cursor, $result];
  290. }
  291. /**
  292. * Scans the given set for all values based on options.
  293. *
  294. * @param string $key
  295. * @param mixed $cursor
  296. * @param array $options
  297. * @return mixed
  298. */
  299. public function zscan($key, $cursor, $options = [])
  300. {
  301. $result = $this->client->zscan($key, $cursor,
  302. $options['match'] ?? '*',
  303. $options['count'] ?? 10
  304. );
  305. if ($result === false) {
  306. $result = [];
  307. }
  308. return $cursor === 0 && empty($result) ? false : [$cursor, $result];
  309. }
  310. /**
  311. * Scans the given hash for all values based on options.
  312. *
  313. * @param string $key
  314. * @param mixed $cursor
  315. * @param array $options
  316. * @return mixed
  317. */
  318. public function hscan($key, $cursor, $options = [])
  319. {
  320. $result = $this->client->hscan($key, $cursor,
  321. $options['match'] ?? '*',
  322. $options['count'] ?? 10
  323. );
  324. if ($result === false) {
  325. $result = [];
  326. }
  327. return $cursor === 0 && empty($result) ? false : [$cursor, $result];
  328. }
  329. /**
  330. * Scans the given set for all values based on options.
  331. *
  332. * @param string $key
  333. * @param mixed $cursor
  334. * @param array $options
  335. * @return mixed
  336. */
  337. public function sscan($key, $cursor, $options = [])
  338. {
  339. $result = $this->client->sscan($key, $cursor,
  340. $options['match'] ?? '*',
  341. $options['count'] ?? 10
  342. );
  343. if ($result === false) {
  344. $result = [];
  345. }
  346. return $cursor === 0 && empty($result) ? false : [$cursor, $result];
  347. }
  348. /**
  349. * Execute commands in a pipeline.
  350. *
  351. * @param callable|null $callback
  352. * @return \Redis|array
  353. */
  354. public function pipeline(callable $callback = null)
  355. {
  356. $pipeline = $this->client()->pipeline();
  357. return is_null($callback)
  358. ? $pipeline
  359. : tap($pipeline, $callback)->exec();
  360. }
  361. /**
  362. * Execute commands in a transaction.
  363. *
  364. * @param callable|null $callback
  365. * @return \Redis|array
  366. */
  367. public function transaction(callable $callback = null)
  368. {
  369. $transaction = $this->client()->multi();
  370. return is_null($callback)
  371. ? $transaction
  372. : tap($transaction, $callback)->exec();
  373. }
  374. /**
  375. * Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
  376. *
  377. * @param string $script
  378. * @param int $numkeys
  379. * @param mixed $arguments
  380. * @return mixed
  381. */
  382. public function evalsha($script, $numkeys, ...$arguments)
  383. {
  384. return $this->command('evalsha', [
  385. $this->script('load', $script), $arguments, $numkeys,
  386. ]);
  387. }
  388. /**
  389. * Evaluate a script and return its result.
  390. *
  391. * @param string $script
  392. * @param int $numberOfKeys
  393. * @param dynamic $arguments
  394. * @return mixed
  395. */
  396. public function eval($script, $numberOfKeys, ...$arguments)
  397. {
  398. return $this->command('eval', [$script, $arguments, $numberOfKeys]);
  399. }
  400. /**
  401. * Subscribe to a set of given channels for messages.
  402. *
  403. * @param array|string $channels
  404. * @param \Closure $callback
  405. * @return void
  406. */
  407. public function subscribe($channels, Closure $callback)
  408. {
  409. $this->client->subscribe((array) $channels, function ($redis, $channel, $message) use ($callback) {
  410. $callback($message, $channel);
  411. });
  412. }
  413. /**
  414. * Subscribe to a set of given channels with wildcards.
  415. *
  416. * @param array|string $channels
  417. * @param \Closure $callback
  418. * @return void
  419. */
  420. public function psubscribe($channels, Closure $callback)
  421. {
  422. $this->client->psubscribe((array) $channels, function ($redis, $pattern, $channel, $message) use ($callback) {
  423. $callback($message, $channel);
  424. });
  425. }
  426. /**
  427. * Subscribe to a set of given channels for messages.
  428. *
  429. * @param array|string $channels
  430. * @param \Closure $callback
  431. * @param string $method
  432. * @return void
  433. */
  434. public function createSubscription($channels, Closure $callback, $method = 'subscribe')
  435. {
  436. //
  437. }
  438. /**
  439. * Flush the selected Redis database.
  440. *
  441. * @return void
  442. */
  443. public function flushdb()
  444. {
  445. if (! $this->client instanceof RedisCluster) {
  446. return $this->command('flushdb');
  447. }
  448. foreach ($this->client->_masters() as $master) {
  449. $this->client->flushDb($master);
  450. }
  451. }
  452. /**
  453. * Execute a raw command.
  454. *
  455. * @param array $parameters
  456. * @return mixed
  457. */
  458. public function executeRaw(array $parameters)
  459. {
  460. return $this->command('rawCommand', $parameters);
  461. }
  462. /**
  463. * Run a command against the Redis database.
  464. *
  465. * @param string $method
  466. * @param array $parameters
  467. * @return mixed
  468. *
  469. * @throws \RedisException
  470. */
  471. public function command($method, array $parameters = [])
  472. {
  473. try {
  474. return parent::command($method, $parameters);
  475. } catch (RedisException $e) {
  476. if (Str::contains($e->getMessage(), 'went away')) {
  477. $this->client = $this->connector ? call_user_func($this->connector) : $this->client;
  478. }
  479. throw $e;
  480. }
  481. }
  482. /**
  483. * Disconnects from the Redis instance.
  484. *
  485. * @return void
  486. */
  487. public function disconnect()
  488. {
  489. $this->client->close();
  490. }
  491. /**
  492. * Apply a prefix to the given key if necessary.
  493. *
  494. * @param string $key
  495. * @return string
  496. */
  497. private function applyPrefix($key)
  498. {
  499. $prefix = (string) $this->client->getOption(Redis::OPT_PREFIX);
  500. return $prefix.$key;
  501. }
  502. /**
  503. * Pass other method calls down to the underlying client.
  504. *
  505. * @param string $method
  506. * @param array $parameters
  507. * @return mixed
  508. */
  509. public function __call($method, $parameters)
  510. {
  511. return parent::__call(strtolower($method), $parameters);
  512. }
  513. }