Redis.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <?php
  2. namespace Mall\Framework\Cache;
  3. use Redis as RedisSource;
  4. use Mall\Framework\Cache\AbstractStorage as Storage;
  5. class Redis extends Storage
  6. {
  7. /**
  8. * @var RedisSource|array
  9. */
  10. protected $resource;
  11. public function __construct($options)
  12. {
  13. parent::__construct($options);
  14. //判断redis扩展是否已经安装
  15. if(!extension_loaded('redis')){
  16. throw new \Exception('Redis extension is not found');
  17. }
  18. $this->resource = new RedisSource();
  19. $port = $this->options['port'] ?: 6379;
  20. if (!$this->resource->connect($this->options['host'], $port, 3)) {
  21. throw new \Exception(sprintf(
  22. 'Cannot connect to redis server on %s:%d',
  23. $this->options['host'], $this->options['port']
  24. ));
  25. }
  26. if ($this->options['auth'] && !$this->resource->auth($this->options['auth'])) {
  27. throw new \Exception(sprintf(
  28. 'Auth failed on %s:%d, auth: %s',
  29. $this->options['host'], $this->options['port'], $this->options['auth']
  30. ));
  31. }
  32. if (isset($this->options['database']) && !$this->resource->select($this->options['database'])) {
  33. throw new \Exception(sprintf(
  34. 'Select Database failed on %s:%d, auth: %s, database:%d',
  35. $this->options['host'], $this->options['port'], $this->options['auth'], $this->options['database']
  36. ));
  37. }
  38. $this->resource->setOption(RedisSource::OPT_PREFIX, 'Mall_');
  39. }
  40. public function set($key, $value = null, $ttl = null)
  41. {
  42. $key = $this->prefix . $key;
  43. $ttl = $ttl ?: 0;
  44. $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
  45. if ($ttl > 0) {
  46. $ret = $this->resource->setex($key, $ttl, $value);
  47. } else {
  48. $ret = $this->resource->set($key, $value);
  49. }
  50. return $ret;
  51. }
  52. public function get($key)
  53. {
  54. $key = $this->prefix . $key;
  55. $value = $this->resource->get($key);
  56. $jsonData = json_decode($value, true);
  57. return ($jsonData === NULL) ? $value : $jsonData;
  58. }
  59. public function incr($key)
  60. {
  61. $key = $this->prefix . $key;
  62. return $this->resource->incr($key);
  63. }
  64. /**
  65. * 将 key 所储存的值加上增量 increment
  66. * @param $key
  67. * @return int
  68. */
  69. public function incrby($key, $increment)
  70. {
  71. $key = $this->prefix . $key;
  72. return $this->resource->incrBy($key, $increment);
  73. }
  74. public function del($key)
  75. {
  76. if(is_array($key)){
  77. foreach ($key as $k => $v){
  78. $key[$k] = $this->prefix . $v;
  79. }
  80. }else{
  81. $key = $this->prefix . $key;
  82. }
  83. $ret = $this->resource->del($key);
  84. return $ret;
  85. }
  86. public function has($key)
  87. {
  88. $key = $this->prefix . $key;
  89. $ret = $this->resource->exists($key);
  90. return $ret;
  91. }
  92. public function delete($key)
  93. {
  94. $key = $this->prefix . $key;
  95. $ret = $this->resource->delete($key) > 0;
  96. return $ret;
  97. }
  98. public function push($key, $value, $type = 'end')
  99. {
  100. $key = $this->prefix . $key;
  101. $ret = FALSE;
  102. if ($type == 'end') {
  103. $ret = $this->resource->rpush($key, $value);
  104. } else if ($type == 'start') {
  105. $ret = $this->resource->lpush($key, $value);
  106. }
  107. return $ret;
  108. }
  109. public function pop($key, $type = 'start')
  110. {
  111. $key = $this->prefix . $key;
  112. $ret =FALSE;
  113. if ($type == 'end') {
  114. $ret = $this->resource->rpop($key);
  115. } else if ($type == 'start') {
  116. $ret = $this->resource->lpop($key);
  117. }
  118. return $ret;
  119. }
  120. /**
  121. * 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
  122. * @param $key
  123. * @param $start
  124. * @param $stop
  125. * @return array
  126. */
  127. public function lrange($key, $start, $stop)
  128. {
  129. $key = $this->prefix . $key;
  130. return $this->resource->lRange($key, $start, $stop);
  131. }
  132. /**
  133. * 返回列表 key 中,下标为 index 的元素
  134. * @param $key
  135. * @param $start
  136. * @param $stop
  137. * @return array
  138. */
  139. public function lindex($key, $index)
  140. {
  141. $key = $this->prefix . $key;
  142. return $this->resource->lIndex($key, $index);
  143. }
  144. /**
  145. * 根据参数 count 的值,移除列表中与参数 value 相等的元素
  146. */
  147. public function lrem($key, $value, $count = 0)
  148. {
  149. $key = $this->prefix . $key;
  150. return $this->resource->lRem($key, $value, $count);
  151. }
  152. /**
  153. * @param $key
  154. * @param array['field1'=>'value', 'field2'=>'value'] $values
  155. * @return int
  156. */
  157. public function hmset($key, $values){
  158. $key = $this->prefix . $key;
  159. return $this->resource->hMSet($key, $values);
  160. }
  161. /**
  162. * @param $key
  163. * @param array['field1', 'field2'] $value
  164. *
  165. * @return int
  166. */
  167. public function hmget($key, $fields){
  168. $key = $this->prefix . $key;
  169. return $this->resource->hMGet($key, $fields);
  170. }
  171. public function hset($key, $field, $value){
  172. $key = $this->prefix . $key;
  173. return $this->resource->hSet($key, $field, $value);
  174. }
  175. public function hget($key, $field){
  176. $key = $this->prefix . $key;
  177. return $this->resource->hGet($key, $field);
  178. }
  179. public function hdel($key, $field){
  180. $key = $this->prefix . $key;
  181. return $this->resource->hDel($key, $field);
  182. }
  183. public function hkeys($key){
  184. $key = $this->prefix . $key;
  185. return $this->resource->hKeys($key);
  186. }
  187. public function hvalues($key){
  188. $key = $this->prefix . $key;
  189. return $this->resource->hVals($key);
  190. }
  191. /**
  192. * @param $key
  193. * @param $field
  194. * @return bool
  195. */
  196. public function hExists($key, $field)
  197. {
  198. $key = $this->prefix . $key;
  199. return $this->resource->hExists($key, $field);
  200. }
  201. public function hIncrBy($key, $field,$value)
  202. {
  203. $key = $this->prefix . $key;
  204. return $this->resource->hIncrBy($key, $field,$value);
  205. }
  206. public function hIncrByFloat($key, $field,$value)
  207. {
  208. $key = $this->prefix . $key;
  209. return $this->resource->hIncrByFloat($key, $field,$value);
  210. }
  211. /**
  212. * 集合
  213. *
  214. * @param $key
  215. * @param $value
  216. *
  217. * @return bool
  218. */
  219. public function sadd($key, $value)
  220. {
  221. $key = $this->prefix . $key;
  222. return $this->resource->sAdd($key, $value);
  223. }
  224. /**
  225. * 集合sadd统计
  226. *
  227. * @param $key
  228. *
  229. * @return int
  230. */
  231. public function scard($key)
  232. {
  233. $key = $this->prefix . $key;
  234. return $this->resource->sCard($key);
  235. }
  236. /**
  237. * 移除集 key 中的一个或多个成员,不存在的成员将被忽略。
  238. */
  239. public function srem($key, $value){
  240. $key = $this->prefix . $key;
  241. return $this->resource->sRem($key, $value);
  242. }
  243. /**
  244. * 随机取集 key 中的一个或多个成员,不存在的成员将被忽略。
  245. * @param $value 获取的条数,默认1
  246. */
  247. public function srandmember($key, $value=1){
  248. $key = $this->prefix . $key;
  249. return $this->resource->sRandMember($key,$value);
  250. }
  251. /**
  252. * 设置客户端
  253. */
  254. public function setOption($option, $value)
  255. {
  256. return $this->resource->setOption($option, $value);
  257. }
  258. /**
  259. * 迭代查询
  260. * @params string $key 集合名
  261. * @params string $keyword 要查询的关键词
  262. */
  263. public function scan($key, $keyword)
  264. {
  265. $key = $this->prefix . $key;
  266. $iterator = null;
  267. while ($members = $this->resource->sScan($key, $iterator, $keyword)) {
  268. foreach ($members as $member) {
  269. echo $member . PHP_EOL;
  270. }
  271. }
  272. }
  273. /**
  274. * 有序集合
  275. *
  276. * @param $key
  277. * @param $value
  278. *
  279. * @return bool
  280. */
  281. public function zadd($key, $score , $value)
  282. {
  283. $key = $this->prefix . $key;
  284. return $this->resource->zAdd($key, $score, $value);
  285. }
  286. /**
  287. * 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员
  288. */
  289. public function zrangebyscore($key, $start, $end, array $options = array()){
  290. $key = $this->prefix . $key;
  291. return $this->resource->zRangeByScore($key, $start, $end, $options);
  292. }
  293. /**
  294. * 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。
  295. */
  296. public function zrem($key, $value){
  297. $key = $this->prefix . $key;
  298. return $this->resource->zRem($key, $value);
  299. }
  300. /**
  301. * 除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员
  302. */
  303. public function zremrangebyscore($key, $start, $end){
  304. $key = $this->prefix . $key;
  305. return $this->resource->zRemRangeByScore($key, $start, $end);
  306. }
  307. /**
  308. * 判断member元素是否是集合Key的成员
  309. *
  310. * @param $key
  311. * @param $value
  312. *
  313. * @return bool
  314. */
  315. public function sismember($key, $value)
  316. {
  317. $key = $this->prefix . $key;
  318. return $this->resource->sIsMember($key, $value);
  319. }
  320. /**
  321. * 返回集合中的所有成员
  322. *
  323. * @param $key
  324. *
  325. * @return bool
  326. */
  327. public function smembers($key)
  328. {
  329. $key = $this->prefix . $key;
  330. return $this->resource->sMembers($key);
  331. }
  332. /**
  333. * 返回集合之间的差集
  334. *
  335. * @param string $key1
  336. * @param string $key2
  337. *
  338. * @return object
  339. */
  340. public function sdiff($key1, $key2)
  341. {
  342. $key1 = $this->prefix . $key1;
  343. $key2 = $this->prefix . $key2;
  344. return $this->resource->sDiff($key1, $key2);
  345. }
  346. /**
  347. * 返回集合之间的交集
  348. *
  349. * @param string $key1
  350. * @param string $key2
  351. *
  352. * @return object
  353. */
  354. public function sinter($key1, $key2)
  355. {
  356. $key1 = $this->prefix . $key1;
  357. $key2 = $this->prefix . $key2;
  358. return $this->resource->sInter($key1, $key2);
  359. }
  360. /**
  361. * 计算集合之间的交集并写入一个新的key中
  362. *
  363. * @param string $deskey 要存入的新key
  364. * @param string $key1
  365. * @param string $key2
  366. *
  367. * @return int 新集合得数量
  368. */
  369. public function sinterStore($deskey, $key1, $key2)
  370. {
  371. $deskey = $this->prefix .$deskey;
  372. $key1 = $this->prefix . $key1;
  373. $key2 = $this->prefix . $key2;
  374. return $this->resource->sInterStore($deskey, $key1, $key2);
  375. }
  376. /**
  377. * 返回集合之间的并集
  378. *
  379. * @param string $key1
  380. * @param string $key2
  381. *
  382. * @return object
  383. */
  384. public function sunion($key1, $key2)
  385. {
  386. $key1 = $this->prefix . $key1;
  387. $key2 = $this->prefix . $key2;
  388. return $this->resource->sUnion($key1, $key2);
  389. }
  390. /**
  391. * 事物支持
  392. */
  393. public function multi()
  394. {
  395. return $this->resource->multi();
  396. }
  397. /**
  398. * 执行事务
  399. */
  400. public function exec()
  401. {
  402. return $this->resource->exec();
  403. }
  404. /**
  405. * 返回整数为列表键长度
  406. *
  407. * @param string $key
  408. *
  409. * @return int
  410. */
  411. public function llen($key)
  412. {
  413. $key = $this->prefix . $key;
  414. return $this->resource->llen($key);
  415. }
  416. /**
  417. * 有序集合中对指定成员的分数加上增量 increment
  418. *
  419. * @param $key
  420. * @param $value
  421. * @param $member
  422. *
  423. * @return float
  424. */
  425. public function zincrby($key, $value, $member)
  426. {
  427. $key = $this->prefix . $key;
  428. return $this->resource->zIncrBy($key, $value, $member);
  429. }
  430. /**
  431. * 返回有序集中,成员的分数值
  432. *
  433. * @param $key
  434. * @param $member
  435. *
  436. * @return float
  437. */
  438. public function zscore($key, $member)
  439. {
  440. $key = $this->prefix . $key;
  441. return $this->resource->zScore($key, $member);
  442. }
  443. /**
  444. * 获取有序集合的成员数
  445. *
  446. * @param $key
  447. *
  448. * @return int
  449. */
  450. public function zcard($key)
  451. {
  452. $key = $this->prefix . $key;
  453. return $this->resource->zCard($key);
  454. }
  455. /**
  456. * 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量
  457. *
  458. * @return int
  459. */
  460. public function zcount($key, $min, $max)
  461. {
  462. $key = $this->prefix . $key;
  463. return $this->resource->zCount($key, $min, $max);
  464. }
  465. /**
  466. * 返回有序集 key 中,指定区间内的成员。
  467. *
  468. * @param $key
  469. * @param $start
  470. * @param $end
  471. *
  472. * @return array
  473. */
  474. public function zrange($key, $start, $end, $withscores = null)
  475. {
  476. $key = $this->prefix . $key;
  477. return $this->resource->zRange($key, $start, $end, $withscores);
  478. }
  479. /**
  480. * 返回有序集 key 中,指定区间内的成员,按score从大到小的顺序
  481. *
  482. * @param $key
  483. * @param $start
  484. * @param $end
  485. *
  486. * @return array
  487. */
  488. public function zRevRange($key, $start, $end, $withscores = null)
  489. {
  490. $key = $this->prefix . $key;
  491. return $this->resource->zRevRange($key, $start, $end, $withscores);
  492. }
  493. /**
  494. * 获取所有的keys
  495. */
  496. public function keys($key)
  497. {
  498. $key = $this->prefix . $key;
  499. return $this->resource->keys($key);
  500. }
  501. /**
  502. * 获取配置项的值
  503. */
  504. public function getOption($name)
  505. {
  506. return $this->resource->getOption($name);
  507. }
  508. /**
  509. * 设置key的生存时间
  510. */
  511. public function expire($key, $time)
  512. {
  513. $key = $this->prefix . $key;
  514. return $this->resource->expire($key, $time);
  515. }
  516. /**
  517. * 获取键到期的剩余时间(秒)
  518. * @param $key
  519. * @return bool|int
  520. */
  521. public function ttl($key)
  522. {
  523. $key = $this->prefix . $key;
  524. return $this->resource->ttl($key);
  525. }
  526. }