CRedisUtil.php 37 KB

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