CRedisUtil.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. <?php
  2. namespace loyalsoft;
  3. //define('Redis_Debug', true); // 调试redis操作
  4. require __DIR__ . '/Predis/Autoloader.php';
  5. \Predis\Autoloader::register();
  6. /**
  7. * redis操作封装,
  8. * 关于阿里云redis的支持命令和未开放命令见:
  9. * https://docs.aliyun.com/?spm=5176.7630237.9.3.WfKF0A#/pub/kvstore/quick-start/kvstore-redis-command
  10. * 腾讯云支持的命令:
  11. * https://www.qcloud.com/doc/product/239/%E4%BD%BF%E7%94%A8%E9%99%90%E5%88%B6
  12. * redis相关命令说明见:http://redisdoc.com/
  13. *
  14. * 支持命令集接口见: /phpRedisAdmin/predis/src/ClientInterface.php
  15. * @version <br/>
  16. * 1.0.4 取消公共基类CMemBase,后面可能不太会用到memcache(d)了. gwang 2020年11月23日
  17. * 1.0.3 扩展list相关api的操作方法, 针对values统一做json编解码处理.
  18. * 整理其他数据结构的操作方法, normal/hash/list api内部做json编解码处理,
  19. * set/zset value 默认不做json编解码处理, 针对set/zset的value,不推荐写入太大或复杂的数据.
  20. * gwang 2017年4月28日 <br/>
  21. * 1.0.2 扩充特有数据结构的操作方法, gwang 2016.4.21 <br/>
  22. * 1.0.1 扩充了事务的使用方法.开发竞争类业务,圣树争夺战, gwang 2016.3.21 <br/>
  23. * 1.0.0 pguan,基础功能, 方法与memcache看齐. <br/>
  24. */
  25. class CRedisUtil {
  26. // public $keyDic = null;
  27. /**
  28. *
  29. * @var ClientInterface
  30. */
  31. public $redis = null;
  32. /**
  33. * 自动回收
  34. */
  35. function __destruct() {
  36. $this->close();
  37. }
  38. private static function debug() {
  39. if (defined('Redis_Debug') && Redis_Debug) {
  40. DebugHelper::print_stack_trace();
  41. }
  42. }
  43. // <editor-fold defaultstate="collapsed" desc=" 实现CMemBase操作接口">
  44. /**
  45. * 添加一个值
  46. * @param string $key
  47. * @param string $value
  48. * @param int $ts=0 此值无效
  49. * @return true 成功, false 失败,此key已经存在
  50. */
  51. public function add($key, $value, $ts = 0) {
  52. self::debug();
  53. return $this->setnx($key, JsonUtil::encode($value));
  54. }
  55. /**
  56. * 复制一个变量到另一个key
  57. * @param string $surKey 源数据的key
  58. * @param string $desKey 目的数据的key
  59. */
  60. public function copy($surKey, $desKey) {
  61. self::debug();
  62. return $this->set($desKey, $this->get($surKey));
  63. }
  64. /**
  65. * 一次取多个值
  66. * @param array(string) $keys
  67. * @return array
  68. */
  69. public function getMulti($keys) {
  70. self::debug();
  71. // var_dump($keys);
  72. return $this->mget($keys);
  73. }
  74. /**
  75. * 设置多个值
  76. * @param dic $dict [{"key":value},{"key":value},....]
  77. * @param int $expireTs=0 此值无效
  78. * @return true 永远成功
  79. */
  80. public function setMutlti($dict, $expireTs = 0) {
  81. self::debug();
  82. $params = array();
  83. foreach ($dict as $key => $val) {
  84. $params[$key] = is_string($val) ? $val : JsonUtil::encode($val);
  85. }
  86. if (count($params) <= 0) {
  87. return TRUE;
  88. }
  89. $this->mset($params);
  90. return true;
  91. }
  92. /**
  93. * 替换某个值
  94. * @param string $key
  95. * @param string $value
  96. * @param int $ts
  97. * @return boolean true 成功,false 失败
  98. */
  99. public function replace($key, $value, $ts = 0) {
  100. self::debug();
  101. $ret = $this->redis->setex($key, $value, $ts);
  102. if (strtolower($ret) == "ok") {
  103. return true;
  104. }
  105. $this->logErr($ret);
  106. return false;
  107. }
  108. /**
  109. * 不经过jsonencode直接存储
  110. * @deprecated since version 20160413113950 经过自己考察json_encode对数据的影响并不存在
  111. * @param type $key
  112. * @return any
  113. */
  114. public function getWithoutJson($key) {
  115. self::debug();
  116. return $this->redis->get($key);
  117. }
  118. /**
  119. * 不经过jsondecode直接返回
  120. * @deprecated since version 20160413113950 经过自己考察json_encode对数据的影响并不存在
  121. * @param string $key
  122. * @param string $value
  123. * @param int $ts
  124. * @return boolean
  125. */
  126. public function setWithoutJson($key, $value, $ts = 0) {
  127. self::debug();
  128. $ret = $this->redis->set($key, $value, $ts);
  129. if (strtolower($ret) == "ok") {
  130. return true;
  131. }
  132. $this->logErr($ret);
  133. return false;
  134. }
  135. /**
  136. * 输出日志
  137. * @param mixed $msg
  138. */
  139. private function logErr($msg) {
  140. CLog::err($msg, "Redis Err:");
  141. }
  142. /**
  143. * 连接
  144. * @param string $host
  145. * @param string $port
  146. * @param string $pwd
  147. * @return \CRedisUtil
  148. */
  149. public function conn($host, $port, $pwd = "") {
  150. // DebugHelper::print_stack_trace();
  151. $this->redis = new \Predis\Client(array(
  152. 'scheme' => 'tcp',
  153. 'host' => $host,
  154. 'port' => $port,
  155. 'password' => $pwd,
  156. ));
  157. // $this->keyDic = new \stdClass();
  158. return $this;
  159. }
  160. /**
  161. * 关闭连接
  162. */
  163. public function close() {
  164. $this->redis->quit();
  165. }
  166. /**
  167. * 获取指定键值
  168. * @param string $key
  169. * @return any json_decode($result)
  170. */
  171. public function get($key) {
  172. self::debug();
  173. $result = $this->redis->get($key);
  174. if (!$result || $result == "" || $result == null) {
  175. return null;
  176. }
  177. // $this->keyDic->$key = $result;
  178. return JsonUtil::decode($result);
  179. }
  180. /**
  181. * 将字符串值 $val 关联到 key
  182. * @param string $key
  183. * @param any $value
  184. * @param int $ts default(0) 过期时间(单位秒)
  185. * @return boolean 成功true,失败false
  186. */
  187. public function set($key, $value, $ts = 0) {
  188. self::debug();
  189. if ($value === null || $value === "") { # 这里必须用全等符号
  190. return false;
  191. }
  192. if (is_string($value)) { # 使用json序列化后存储
  193. $val = $value;
  194. } else {
  195. $val = JsonUtil::encode($value);
  196. }
  197. if ($ts > 0) { # 将会设置TTL 到期删除 ps: replaced by gwang 增加过期时间
  198. $ret = $this->redis->setex($key, $ts, $val);
  199. } else { # 0:永不过期,如果原来有TTL将会清除TTL.
  200. $ret = $this->redis->set($key, $val);
  201. }
  202. if ("OK" != $ret) {
  203. $this->logErr($ret);
  204. return false;
  205. }
  206. return true;
  207. }
  208. // /**
  209. // * 安全的写入操作
  210. // * @deprecated since version 1.0 云平台暂时不支持
  211. // * @param string $key
  212. // * @param any $value
  213. // * @return boolean 成功true,失败false
  214. // */
  215. // public function cas($key, $value, $ts = 0) {
  216. // if (false) { # ps. 各云平台皆不支持eval方法执行lua脚本.等开放支持的时候才是真正支持cas的时候
  217. // $castoken = $this->keyDic->$key;
  218. //# # 定义一段脚本
  219. // $script = <<<casscript
  220. //if redis.call("get",KEYS[1]) == "$castoken"
  221. //then
  222. // return redis.call("set",KEYS[1],ARGV[1])
  223. //else
  224. // return 0
  225. //end
  226. //casscript;
  227. // $ret = $this->redis->eval($script, 1, $key, $value); # redis 执行lua脚本
  228. // if (0 == $ret) {
  229. // return false;
  230. // }
  231. // return true;
  232. // }
  233. //
  234. // // 非线程安全,原因见下
  235. // return $this->set($key, $value, $ts);
  236. // }
  237. /**
  238. * 删除一个或多个指定键值
  239. * @param string $keys
  240. * @return int 删除键的个数
  241. */
  242. public function delete($key) {
  243. self::debug();
  244. $infos = array();
  245. array_push($infos, $key);
  246. return $this->redis->del($infos);
  247. }
  248. // </editor-fold>
  249. //
  250. // <editor-fold defaultstate="collapsed" desc=" normal 普通对象操作 mget/mset">
  251. /**
  252. * 返回所有(一个或多个)给定 key 的值
  253. * 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。因此,该命令永不失败。
  254. * @param array $keys
  255. * @return array 一个包含所有给定 key 的值的列表
  256. */
  257. private function mget($keys) {
  258. // self::debug();
  259. $ret = array();
  260. if (count($keys)) {
  261. $ctx = $this->redis->mget($keys);
  262. $mval = count($ctx);
  263. for ($i = 0; $i < $mval; $i++) {
  264. $ret[] = JsonUtil::decode($ctx[$i]);
  265. }
  266. }
  267. return $ret;
  268. }
  269. /**
  270. * Add函数拥有相同效果 \n
  271. * 将 key 的值设为 value ,当且仅当 key 不存在。
  272. * 若给定的 key 已经存在,则不做任何动作。
  273. * @param type $key
  274. * @param type $val
  275. * @return boolean 成功true,失败false
  276. */
  277. private function setnx($key, $val) {
  278. self::debug();
  279. $ret = $this->redis->setnx($key, $val);
  280. // 成功返回1, 失败返回0
  281. if (1 == $ret) {
  282. return true;
  283. }
  284. $this->logErr($ret);
  285. return false;
  286. }
  287. /**
  288. * 同时设置一个或多个 key-value 对
  289. * 如果某个给定 key 已经存在,那么 MSET 会用新值覆盖原来的旧值
  290. * @param array $dict
  291. * @return mixed
  292. */
  293. private function mset($dict) {
  294. self::debug();
  295. return $this->redis->mset($dict);
  296. }
  297. /**
  298. * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
  299. * 即使只有一个给定 key 已存在, MSETNX 也会拒绝执行所有给定 key 的设置操作
  300. *
  301. * @param array $dict
  302. * @return int
  303. */
  304. public function msetnx($dict) {
  305. self::debug();
  306. $ret = $this->redis->msetnx($dict);
  307. return $ret == 1;
  308. }
  309. /**
  310. * 检测指定键值是否存在
  311. * @param type $key
  312. * @return boolean true 存在, false 不存在
  313. */
  314. public function exists($key) {
  315. self::debug();
  316. return $this->redis->exists($key) == 1;
  317. }
  318. /**
  319. * 将 key 中储存的数字值增一
  320. * 如果 key 不存在,那么 key 的值会先被初始化为 0
  321. * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误
  322. * 本操作的值限制在 64 位(bit)有符号数字表示之内
  323. * @param type $key
  324. * @return int 执行 INCR 命令之后 key 的值
  325. */
  326. public function increment($key) {
  327. self::debug();
  328. return $this->redis->incr($key);
  329. }
  330. /**
  331. * 将 key 中储存的数字值增加 一个int值
  332. * 如果 key 不存在,那么 key 的值会先被初始化为 0
  333. * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误
  334. * 本操作的值限制在 64 位(bit)有符号数字表示之内
  335. * @param string $key
  336. * @param int $i 增加的值
  337. * @return int 执行 INCR 命令之后 key 的值
  338. */
  339. public function incrby($key, $i) {
  340. self::debug();
  341. return $this->redis->incrby($key, $i);
  342. }
  343. // </editor-fold>
  344. //
  345. //======================== 以下部分 added by gwang at 2016-4-14 15:17:25 =================
  346. // <editor-fold defaultstate="collapsed" desc=" 辅助函数 ">
  347. /**
  348. * Server功能,看服务器是否连通
  349. * @return boolean true连通正常
  350. */
  351. public function ping() {
  352. $ret = $this->redis->ping();
  353. if ($ret == "PONG") {
  354. return true;
  355. }
  356. $this->logErr($ret);
  357. return false;
  358. }
  359. /**
  360. * 对数组进行json编码
  361. * @param array $values
  362. * @return array
  363. */
  364. static private function json_encode_arr($values) {
  365. $arr = array();
  366. if (is_array($values)) {
  367. $arr = array_map(function($v) {
  368. return JsonUtil::encode($v);
  369. }, $values);
  370. } else {
  371. $arr[] = JsonUtil::encode($values);
  372. }
  373. return $arr;
  374. }
  375. /**
  376. * 对数组进行json解码
  377. * @param array $values
  378. * @return array
  379. */
  380. static private function json_decode_arr(array $values) {
  381. $arr = array();
  382. if (is_array($values)) {
  383. $arr = array_map(function($v) {
  384. return JsonUtil::decode($v);
  385. }, $values);
  386. } else {
  387. $arr[] = JsonUtil::decode($values);
  388. }
  389. return $arr;
  390. }
  391. // </editor-fold>
  392. //
  393. // <editor-fold defaultstate="collapsed" desc=" 事务操作 ">
  394. /**
  395. * 监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断。
  396. * @param string $key
  397. */
  398. public function watch($key) {
  399. $this->redis->watch($key);
  400. }
  401. /**
  402. * 取消监视
  403. */
  404. public function unwatch() {
  405. $this->redis->unwatch();
  406. }
  407. /**
  408. * 开启事务
  409. */
  410. public function multi() {
  411. $this->redis->multi();
  412. }
  413. /**
  414. * 执行事务
  415. * @return type 事务块内所有命令的返回值,按命令执行的先后顺序排列。当操作被打断时,返回空值 nil 。
  416. */
  417. public function exec() {
  418. return $this->redis->exec();
  419. }
  420. // </editor-fold>
  421. //
  422. // == ↓ 下面是Redis独有的数据类型操作 ↓ ==
  423. //
  424. // <editor-fold defaultstate="collapsed" desc=" set 集合操作 ">
  425. /**
  426. * 集合(set) - 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。
  427. * 集合不存在时将会创建集合
  428. * @param string $key
  429. * @param string[] $members (mixed is ok, i will transmit it.)
  430. * @return int
  431. * @expectedException type not set error
  432. */
  433. public function sadd($key, $members) {
  434. self::debug();
  435. if (!is_array($members)) { # 防御: 参数不为数组
  436. if (!is_string($members)) { # 防御: 参数不为字符串
  437. $members = JsonUtil::encode($members);
  438. }
  439. $members = array($members); # 转为字符串数组
  440. }
  441. return $this->redis->sadd($key, $members);
  442. }
  443. /**
  444. * 集合(set) - 将一个或多个 member 元素从集合 key 当中移除
  445. * 集合不存在时将会创建集合
  446. * @param string $key
  447. * @param string[] $members
  448. * @return int
  449. * @expectedException type not set error
  450. */
  451. public function sremove($key, $members) {
  452. self::debug();
  453. return $this->redis->srem($key, $members);
  454. }
  455. /**
  456. * 集合(set) - 返回集合 key 的基数(集合中元素的数量)。
  457. * @param strig $key
  458. * @return int
  459. */
  460. public function scard($key) {
  461. self::debug();
  462. return $this->redis->scard($key);
  463. }
  464. /**
  465. * 集合(set) - 等效于scard: 返回集合 key 的基数(集合中元素的数量)。
  466. * @param string $key
  467. * @return int
  468. *
  469. */
  470. public function slen($key) {
  471. self::debug();
  472. return $this->scard($key);
  473. }
  474. /**
  475. * 集合(set) - 判断某个元素是否属于集合
  476. * @param string $key
  477. * @param string $member
  478. * @return bool
  479. */
  480. public function sismember($key, $member) {
  481. self::debug();
  482. return $this->redis->sismember($key, $member) == 1;
  483. }
  484. /**
  485. * 集合(set) - 返回集合 key 中的所有成员。
  486. * @param type $key
  487. * @return array 不存在的key视为空集合
  488. */
  489. public function smembers($key) {
  490. self::debug();
  491. return $this->redis->smembers($key);
  492. }
  493. /**
  494. * 集合(set) - 如果命令执行时,只提供了 key 参数,那么返回集合中的10个随机元素。
  495. * 如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。
  496. * 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。
  497. * @param string $key
  498. * @param int $count =10
  499. * @return array
  500. */
  501. public function srandmember($key, $count = 10) {
  502. self::debug();
  503. return $this->redis->srandmember($key, $count);
  504. }
  505. // </editor-fold>
  506. //
  507. // <editor-fold defaultstate="collapsed" desc=" hash 哈希表操作 ">
  508. /**
  509. * 哈希表 - 删除 字段
  510. * @param string $key
  511. * @param array $fields
  512. * @return int 删除的字段的数量
  513. */
  514. public function hdel($key, $fields) {
  515. self::debug();
  516. return $this->redis->hdel($key, $fields);
  517. }
  518. /**
  519. * 哈希表 - 判断field是否存在
  520. * @param string $key
  521. * @param string $field
  522. * @return boolean true=存在,false 找不到
  523. */
  524. public function hexists($key, $field) {
  525. self::debug();
  526. return $this->redis->hexists($key, $field) == 1;
  527. }
  528. /**
  529. * 哈希表 - 获取字段
  530. * @param string $key
  531. * @param string $field 字段名
  532. * @return mixed json_decoded object
  533. */
  534. public function hget($key, $field) {
  535. self::debug();
  536. $ret = $this->redis->hget($key, $field);
  537. return JsonUtil::decode($ret);
  538. }
  539. /**
  540. * 哈希表 - 获取所有字段
  541. * @param string $key
  542. * @return stdclass/associate_array (适用箭头取变量写法)
  543. */
  544. public function hgetall($key) {
  545. self::debug();
  546. $ret = ArrayInit();
  547. $arr = $this->redis->hgetall($key);
  548. foreach ($arr as $k => $v) {
  549. $ret[$k] = JsonUtil::decode($v);
  550. }
  551. // 转为关联数组({})再返回, 方便代码中使用箭头写法
  552. return JsonUtil::decode(JsonUtil::encode($ret));
  553. }
  554. /**
  555. * 哈希表 - 获取表中字段数量
  556. * @param string $key
  557. * @return int
  558. */
  559. public function hlen($key) {
  560. self::debug();
  561. return $this->redis->hlen($key);
  562. }
  563. /**
  564. * 哈希表 - 同时获取多个字段
  565. * @param string $key
  566. * @param array $fields
  567. * @return Array
  568. */
  569. public function hmget($key, $fields) {
  570. self::debug();
  571. $ret = ArrayInit();
  572. if (is_array($fields) && count($fields) > 0) {
  573. $arr = $this->redis->hmget($key, $fields);
  574. foreach ($arr as $k => $v) {
  575. $ret[$k] = JsonUtil::decode($v);
  576. }
  577. }
  578. // 转为关联数组({})再返回, 方便代码中使用箭头写法
  579. return JsonUtil::decode(JsonUtil::encode($ret));
  580. }
  581. /**
  582. * 哈希表 - 同时设置多个字段的值
  583. * @param string $key
  584. * @param assoc_array $dictionary
  585. * @return boolean true成功
  586. */
  587. public function hmset($key, $dictionary) {
  588. self::debug();
  589. $newdic = ArrayInit();
  590. foreach ($dictionary as $k => $v) {
  591. $newdic[$k] = JsonUtil::encode($v);
  592. }
  593. $ret = $this->redis->hmset($key, $newdic);
  594. if (strtolower($ret) == 'ok') {
  595. return true;
  596. }
  597. return false;
  598. }
  599. /**
  600. * 哈希表 - 设置或新建一个字段的值
  601. * @param string $key
  602. * @param string $field
  603. * @param $mixed $value
  604. * @return int 0 覆盖旧值, 1 新建字段
  605. */
  606. public function hset($key, $field, $value) {
  607. self::debug();
  608. return $this->redis->hset($key, $field, JsonUtil::encode($value));
  609. }
  610. /**
  611. * 哈希表 - 增加一个新的field,已经存在则放弃操作返回false
  612. * @param string $key
  613. * @param string $field
  614. * @param string $value
  615. * @return boolean true成功
  616. */
  617. public function hsetnx($key, $field, $value) {
  618. self::debug();
  619. return $this->redis->hsetnx($key, $field, JsonUtil::encode($value)) == 1;
  620. }
  621. /**
  622. * 哈希表 - 按字段赠加 int值
  623. * @param string $key
  624. * @param string $field
  625. * @param int $increment
  626. * @return int
  627. */
  628. public function hincrby($key, $field, $increment) {
  629. self::debug();
  630. return $this->redis->hincrby($key, $field, $increment);
  631. }
  632. /**
  633. * 哈希表 - 按字段增加 float值
  634. * @param string $key
  635. * @param string $field
  636. * @param string $increment float值用string
  637. * @return string 用string存float值
  638. */
  639. public function hincrbyfloat($key, $field, $increment) {
  640. self::debug();
  641. return $this->redis->hincrbyfloat($key, $field, $increment);
  642. }
  643. /**
  644. * @param string $key
  645. * @return array
  646. */
  647. public function hkeys($key) {
  648. self::debug();
  649. return $this->redis->hkeys($key);
  650. }
  651. // * @method array hscan($key, $cursor, array $options = null)
  652. // * @method array hvals($key)
  653. // </editor-fold>
  654. //
  655. // <editor-fold defaultstate="collapsed" desc=" list 链表操作 ">
  656. //
  657. // <editor-fold defaultstate="collapsed" desc=" 阻塞的链表操作 ">
  658. /**
  659. * 链表 - 阻塞弹出, 当列表中没有元素的时候,命令会阻塞,直到等待超时或者有元素被添加到列表中.
  660. * @param array $keys 可以指定多个数组
  661. * @param int $timeout
  662. * @return array
  663. */
  664. public function blpop(array $keys, $timeout) {
  665. self::debug();
  666. $arr = $this->redis->blpop($keys, $timeout);
  667. $arr[1] = JsonUtil::decode($arr[1]); // 对value进行解包
  668. return $arr;
  669. }
  670. /**
  671. * 链表 - 阻塞队尾弹出, 当列表中没有元素的时候,命令会阻塞,直到等待超时或者有元素被添加到列表中.
  672. * @return array
  673. */
  674. public function brpop(array $keys, $timeout) {
  675. self::debug();
  676. $arr = $this->redis->brpop($keys, $timeout);
  677. $arr[1] = JsonUtil::decode($arr[1]);
  678. return $arr;
  679. }
  680. /**
  681. * 链表 - 阻塞版的 队尾弹出插入到队首. 本操作是原子的.
  682. *
  683. * @return array 假如在指定时间内没有任何元素被弹出,则返回一个 nil 和等待时长。
  684. * 反之,返回一个含有两个元素的列表,第一个元素是被弹出元素的值,第二个元素是等待时长。
  685. */
  686. public function brpoplpush($source, $destination, $timeout) {
  687. self::debug();
  688. $arr = $this->redis > brpoplpush($source, $destination, $timeout);
  689. $arr[0] = JsonUtil::decode($arr[0]);
  690. return $arr;
  691. }
  692. // </editor-fold>
  693. //
  694. /**
  695. * 链表 - 返回下标为index的元素
  696. * @return string
  697. */
  698. public function lindex($key, $index) {
  699. self::debug();
  700. $a = $this->redis->lindex($key, $index);
  701. return JsonUtil::decode($a);
  702. }
  703. /**
  704. * 链表 - 返回 链表key的长度
  705. * @return int
  706. * @param type $key
  707. */
  708. public function llen($key) {
  709. self::debug();
  710. return $this->redis->llen($key);
  711. }
  712. /**
  713. * 链表 - 移除并返回链表的队首元素.
  714. * @return string
  715. * @param type $key
  716. */
  717. public function lpop($key) {
  718. self::debug();
  719. $a = $this->redis->lpop($key);
  720. return JsonUtil::decode($a);
  721. }
  722. /**
  723. * 链表 - 向链表插入一个或多个值,链表不存在时创建链表并插入元素
  724. * @return int key存在却不是链表类型时返回一个错误.
  725. * @param type $key
  726. * @param array $values
  727. */
  728. public function lpush($key, $values) {
  729. self::debug();
  730. $arr = self::json_encode_arr($values);
  731. return $this->redis->lpush($key, $arr);
  732. }
  733. /**
  734. * 链表 - 取链表中指定下标区间的元素
  735. * @return array
  736. * @param string $key
  737. * @param int $start
  738. * @param int $stop
  739. */
  740. public function lrange($key, $start, $stop) {
  741. self::debug();
  742. $values = $this->redis->lrange($key, $start, $stop);
  743. return self::json_decode_arr($values);
  744. }
  745. /**
  746. * 链表 - 移除链表中 count 个值为 value的元素.
  747. * @return int
  748. * @param string $key
  749. * @param int $count
  750. * @param string $value
  751. */
  752. public function lrem($key, $count, $value) {
  753. self::debug();
  754. return $this->redis->lrem($key, $count, JsonUtil::encode($value));
  755. }
  756. /**
  757. * 链表 - 将链表中下标为index的 元素设置为value
  758. * @return mixed
  759. *
  760. * @param type $key
  761. * @param type $index
  762. * @param type $value
  763. */
  764. public function lset($key, $index, $value) {
  765. self::debug();
  766. return $this->redis->lset($key, $index, JsonUtil::encode($value));
  767. }
  768. /**
  769. * 链表 - 对链表进行修剪, 让链表只保留指定下标区间的元素,其余元素删除
  770. * @return bool true 成功, false 失败
  771. * @param string $key
  772. * @param int $start
  773. * @param int $stop
  774. */
  775. public function ltrim($key, $start, $stop) {
  776. self::debug();
  777. // return mixed: ok or fails
  778. $ret = $this->redis->ltrim($key, $start, $stop);
  779. if (strtolower($ret) == 'ok') {
  780. return true;
  781. }
  782. return false;
  783. }
  784. /**
  785. * 链表 - 从链表右侧(尾部)弹出一个元素,并返回该元素
  786. * @param string $key
  787. * @return string 列表的尾元素。当 key 不存在时,返回 nil
  788. */
  789. public function rpop($key) {
  790. self::debug();
  791. return JsonUtil::decode($this->redis->rpop($key));
  792. }
  793. /**
  794. * 将一个或多个值 value 插入到列表 key 的表尾(最右边)。
  795. * @param string $key
  796. * @param array $values
  797. * @return int
  798. */
  799. public function rpush($key, array $values) {
  800. self::debug();
  801. $arr = self::json_encode_arr($values);
  802. return $this->redis->rpush($key, $arr);
  803. }
  804. //
  805. // /**
  806. // * 链表 - 向链表中插入一个或多个值,链表不存在时,什么也不做.
  807. // * @return int
  808. // * @param type $key
  809. // * @param type $value
  810. // */
  811. // public function lpushx($key, $value)
  812. // {
  813. // return $this->redis->lpushx($key, $value);
  814. // }
  815. // /**
  816. // * 链表 - 将值 value 插入到链表 key 中 , 位于pivot之前或之后
  817. // * @return int -1:未找到pivot, 0:链表为空或者不存在.
  818. // *
  819. // * @param string $key 链表名称
  820. // * @param string $whence after/before pivot之前或之后
  821. // * @param string $pivot 插入pivot之前或之后
  822. // * @param string $value 要插入的value
  823. // */
  824. // public function linsert($key, $whence, $pivot, $value)
  825. // {
  826. // $this->redis->linsert($key, $whence, $pivot, $value);
  827. // }
  828. // /**
  829. // * 在一个原子时间内,执行以下两个动作
  830. // * 将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。
  831. // * 将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素.
  832. // * @return string
  833. // */
  834. // public function rpoplpush($source, $destination)
  835. // {
  836. // throw new Exception("un implemented now!");
  837. // }
  838. // /**
  839. // * 将值 value 插入到列表 key 的表尾,当且仅当 key 存在并且是一个列表。
  840. // * 和 RPUSH 命令相反,当 key 不存在时, RPUSHX 命令什么也不做。
  841. // * @param type $key
  842. // * @param type $value
  843. // * @return int
  844. // */
  845. // public function rpushx($key, $value)
  846. // {
  847. // throw new Exception("un implemented now!");
  848. // }
  849. // </editor-fold>
  850. //
  851. // <editor-fold defaultstate="collapsed" desc=" zset 有序集合 ">
  852. /**
  853. * 有序集合 - 添加或更新元素 对于zset类型不建议写入太长, 太复杂的value
  854. * @param string $key
  855. * @param array $membersAndScoresDictionary array( 'value'=>score,'value'=>score,...), score可以是int或float
  856. * @return int 新增元素数量,不包含更新的元素
  857. */
  858. public function zadd($key, $membersAndScoresDictionary) {
  859. self::debug();
  860. return $this->redis->zadd($key, $membersAndScoresDictionary);
  861. }
  862. /**
  863. * 有序集合 - 给某个元素增加积分
  864. * @param string $key
  865. * @param string $member
  866. * @param int $increment
  867. * @return string
  868. */
  869. public function zincrby($key, $member, $increment = 1) {
  870. self::debug();
  871. return $this->redis->zincrby($key, $increment, $member);
  872. }
  873. /**
  874. * 有序集合 - 元素数量
  875. * @param string $key
  876. * @return int
  877. */
  878. public function zlen($key) {
  879. self::debug();
  880. return $this->redis->zcard($key);
  881. }
  882. /**
  883. * 有序集合 - 统计某区间元素数量
  884. * @param string $key
  885. * @param int/float $min 最低分
  886. * @param int/float $max 最高分
  887. * @return int
  888. */
  889. public function zcount($key, $min, $max) {
  890. self::debug();
  891. return $this->redis->zcount($key, $min, $max);
  892. }
  893. /**
  894. * 有序集合 - 获取集合指定区间内的成员, 第一个成员从0开始
  895. * @param string $key
  896. * @param int $start
  897. * @param int $stop Ps.以 -1 表示最后一个成员, -2 表示倒数第二个成员
  898. * @param boolean $withScore 返回值中是否带score字段
  899. * @return array (index=>member) or assoc_array (member=>score)
  900. */
  901. public function zrange($key, $start, $stop, $withScore = false) {
  902. self::debug();
  903. if ($withScore) {
  904. return $this->redis->zrange($key, $start, $stop, 'WITHSCORES');
  905. } else {
  906. return $this->redis->zrange($key, $start, $stop);
  907. }
  908. if ($withScore) {
  909. $ret = $this->redis->zrange($key, $start, $stop, 'WITHSCORES');
  910. DebugHelper::var_dump($ret);
  911. $ma = count($ret);
  912. $arr = array();
  913. for ($i = 0; $i < $ma; $i += 2) {
  914. $arr[$ret[$i]] = $ret[$i + 1];
  915. }
  916. return $arr;
  917. } else {
  918. $ret = $this->redis->zrange($key, $start, $stop);
  919. DebugHelper::var_dump($ret);
  920. return $ret;
  921. }
  922. }
  923. /**
  924. *
  925. * 有序集合 - 获取集合倒序之后,指定区间内的成员, 第一个成员从0开始
  926. * @param string $key
  927. * @param int $start
  928. * @param int $stop Ps.以 -1 表示最后一个成员, -2 表示倒数第二个成员
  929. * @param boolean $withScore 返回值中是否带score字段 default(false)
  930. * @return array (index=>member) or assoc_array (member=>score) 用foreach(=>)取
  931. */
  932. public function zrevrange($key, $start, $stop, $withScore = false) {
  933. self::debug();
  934. if ($withScore) {
  935. $r = $this->redis->zrevrange($key, $start, $stop, 'WITHSCORES');
  936. return $r;
  937. } else {
  938. return $this->redis->zrevrange($key, $start, $stop);
  939. }
  940. }
  941. /**
  942. *
  943. * 有序列表 - 获取指定积分区间内的成员
  944. * @param string $key
  945. * @param int/float $min
  946. * @param int/float $max
  947. * @param boolean $withScore
  948. * @param bool $limit 是否限制返回值大小
  949. * @param int $offset 限制返回结果的起始位置
  950. * @param type $count 返回结果的数量
  951. * @return type
  952. */
  953. public function zrangebyscore($key, $min, $max, $withScore = false, $limit = false, $offset = 0, $count = 10) {
  954. self::debug();
  955. if ($limit) {
  956. if ($withScore) {
  957. return $this->redis->zrangebyscore($key, $min, $max, 'WITHSCORES', "LIMIT", $offset, $count);
  958. } else {
  959. return $this->redis->zrangebyscore($key, $min, $max, 'LIMIT', $offset, $count);
  960. }
  961. } else {
  962. if ($withScore) {
  963. return $this->redis->zrangebyscore($key, $min, $max, 'WITHSCORES');
  964. } else {
  965. return $this->redis->zrangebyscore($key, $min, $max);
  966. }
  967. }
  968. }
  969. /**
  970. *
  971. * 有序列表 - 获取按照倒序排序后指定积分区间内的成员
  972. * @param string $key
  973. * @param int/float $max
  974. * @param int/float $min
  975. * @param boolean $withScore
  976. * @param bool $limit 是否限制返回值大小
  977. * @param int $offset 限制返回结果的起始位置
  978. * @param type $count 返回结果的数量
  979. * @return type
  980. */
  981. public function zrevrangebyscore($key, $max, $min, $withScore = false, $limit = false, $offset = 0, $count = 10) {
  982. self::debug();
  983. if ($limit) {
  984. if ($withScore) {
  985. return $this->redis->zrevrangebyscore($key, $max, $min, 'WITHSCORES', "LIMIT", $offset, $count);
  986. } else {
  987. return $this->redis->zrevrangebyscore($key, $max, $min, 'LIMIT', $offset, $count);
  988. }
  989. } else {
  990. if ($withScore) {
  991. return $this->redis->zrevrangebyscore($key, $max, $min, 'WITHSCORES');
  992. } else {
  993. return $this->redis->zrevrangebyscore($key, $max, $min);
  994. }
  995. }
  996. }
  997. /**
  998. * 有序列表 - 根据元素反查排名
  999. * @param string $key
  1000. * @param string $member
  1001. * @return int
  1002. */
  1003. public function zrank($key, $member) {
  1004. self::debug();
  1005. return $this->redis->zrank($key, $member);
  1006. }
  1007. /**
  1008. * 有序列表 - 查询某个成员的倒序排名
  1009. * @param string $key
  1010. * @param string $member
  1011. * @return int
  1012. */
  1013. public function zrevrank($key, $member) {
  1014. self::debug();
  1015. return $this->redis->zrevrank($key, $member);
  1016. }
  1017. /**
  1018. * 有序列表 - 根据元素反查积分
  1019. * @param string $key
  1020. * @param string $member
  1021. * @return int/float
  1022. */
  1023. public function zscore($key, $member) {
  1024. self::debug();
  1025. return $this->redis->zscore($key, $member);
  1026. }
  1027. /**
  1028. * 有序列表 - 移除元素
  1029. * @param string $key
  1030. * @param string $member
  1031. */
  1032. public function zrem($key, $member) {
  1033. self::debug();
  1034. $this->redis->zrem($key, $member);
  1035. }
  1036. /**
  1037. * 有序列表 - 移除指定区间的元素
  1038. * @param string $key
  1039. * @param int $start
  1040. * @param int $stop
  1041. */
  1042. public function zremrangebyrank($key, $start, $stop) {
  1043. self::debug();
  1044. $this->redis->zremrangebyrank($key, $start, $stop);
  1045. }
  1046. /**
  1047. * 有序列表 - 移除指定积分区间的元素.
  1048. * @param string $key
  1049. * @param int/float $min
  1050. * @param int/float $max
  1051. */
  1052. public function zremrangebyscore($key, $min, $max) {
  1053. self::debug();
  1054. $this->redis->zremrangebyscore($key, $min, $max);
  1055. }
  1056. /**
  1057. * 有序列表 - 复制有序类表到一个新的key
  1058. * @param string $key_source 源集合
  1059. * @param string $key_new 目标集合
  1060. */
  1061. public function zcopy($key_source, $key_new) {
  1062. self::debug();
  1063. $this->redis->zunionstore($key_new, 1, $key_source);
  1064. }
  1065. // </editor-fold>
  1066. //
  1067. // <editor-fold defaultstate="collapsed" desc=" 其他api ">
  1068. // == end of zSet ===
  1069. // * @method string zincrby($key, $increment, $member)
  1070. // * @method int zinterstore($destination, array $keys, array $options = null)
  1071. // * @method array zrevrangebyscore($key, $min, $max, array $options = null)
  1072. // * @method int zunionstore($destination, array $keys, array $options = null)
  1073. // * @method array zscan($key, $cursor, array $options = null)
  1074. // * @method array zrangebylex($key, $start, $stop, array $options = null)
  1075. // * @method int zremrangebylex($key, $min, $max)
  1076. // === end of zSet 操作 =====
  1077. // ======= hash 操作 ======
  1078. //
  1079. // // ======== set 操作 ======
  1080. // * @method array sdiff(array $keys)
  1081. // * @method int sdiffstore($destination, array $keys)
  1082. // * @method array sinter(array $keys)
  1083. // * @method int sinterstore($destination, array $keys)
  1084. //
  1085. // * @method int smove($source, $destination, $member)
  1086. // * @method string spop($key)
  1087. // * @method array sscan($key, $cursor, array $options = null)
  1088. // * @method array sunion(array $keys)
  1089. // * @method int sunionstore($destination, array $keys)
  1090. // ======= 订阅相关api =======s
  1091. // * @method int pfadd($key, array $elements)
  1092. // * @method mixed pfmerge($destinationKey, array $sourceKeys)
  1093. // * @method int pfcount(array $keys)
  1094. // * @method mixed pubsub($subcommand, $argument)
  1095. // * @method int publish($channel, $message)
  1096. // *
  1097. // ======= 事务相关api =======
  1098. // * @method mixed discard()
  1099. // * @method array exec()
  1100. // * @method mixed multi()
  1101. // * @method mixed unwatch()
  1102. // * @method mixed watch($key)
  1103. // ======= 执行lua脚本 =======
  1104. // * @method mixed eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
  1105. // * @method mixed evalsha($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
  1106. // * @method mixed script($subcommand, $argument = null)
  1107. // ======= 服务器操作api ======
  1108. // * @method mixed auth($password)
  1109. // * @method string echo($message)
  1110. // * @method mixed ping($message = null)
  1111. // * @method mixed select($database)
  1112. // * @method mixed bgrewriteaof()
  1113. // * @method mixed bgsave()
  1114. // </editor-fold>
  1115. }