muredis.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. <?php
  2. /**
  3. * 请不要删除此链接: http://www.boyaa.com/
  4. * value限制为1G
  5. * $config['Redis'] = array('127.0.0.1', '6379'); //redis存储.
  6. * $config['Redis'] = array('/tmp/redis.sock', null); //通过套接字连接
  7. * $oo = new muredis( oo::$config['Redis'], false);
  8. */
  9. class muredis{
  10. /**
  11. * @var Redis
  12. */
  13. private $oRedis = null; //连接对象
  14. private $persist = false; //是否长连接(多线程版本的Redis不支持长连接)
  15. private $connect = false; //是否连接上
  16. private $connected = false; //是否已经连接过
  17. private $connectlast = 0; //记录最后连接的时间.每隔一段时间强制连一次
  18. private $timeout = 3; //连接超时.秒为单位
  19. public $aServer = array(); //地址配置
  20. public $prefix = ''; //所有Key前缀
  21. public $die = false; //出错后是否完全退出脚本
  22. public $seria = false; //是否进行序列化
  23. public $count = 0; //连接次数
  24. public $version = '5.0.5';//当前redis版本
  25. //const REDIS_STRING = Redis::REDIS_STRING; //字符串类型 ->以下不能打开.在没有安装扩展的机器上不能正确加载此文件
  26. //const REDIS_SET = Redis::REDIS_SET; //SET类型
  27. //const REDIS_LIST = Redis::REDIS_LIST; //LIST类型
  28. //const REDIS_ZSET = Redis::REDIS_ZSET;
  29. //const REDIS_HASH = Redis::REDIS_HASH;
  30. //const REDIS_NOT_FOUND = Redis::REDIS_NOT_FOUND;
  31. //const MULTI = Redis::MULTI; //事务类型:保证原子性
  32. //const PIPELINE = Redis::PIPELINE; //事务类型:不保证原子性仅批处理
  33. public function __construct( $aServer = null, $persist=false){
  34. $temp[] = $aServer['host'];
  35. $temp[] = $aServer['port'];
  36. $temp[] = $aServer['password'];
  37. $aServer = $temp;
  38. $this->aServer = $aServer;
  39. $this->persist = $persist;
  40. if( ! class_exists( 'Redis')){ //强制使用
  41. die('This Lib Requires The Redis Extentisdcas on!');
  42. }
  43. }
  44. /**
  45. * 连接.每个实例仅连接一次
  46. * @return Boolean
  47. */
  48. private function connect(){
  49. $this->count++; //统计次数
  50. if ( (! $this->connected) || (time() - $this->connectlast > 30)){ //没有连接过或者链接超过了10秒
  51. $this->connected = true; //标志已经连接过一次
  52. $this->connectlast = time(); //记录此次连接的时间
  53. try {
  54. $this->oRedis = new Redis();
  55. if(empty( $this->aServer[1])){ //通过套接字连接
  56. $this->connect = $this->persist ? $this->oRedis->pconnect( $this->aServer[0]) : $this->oRedis->connect( $this->aServer[0]);
  57. }else{
  58. $this->connect = $this->persist ?
  59. $this->oRedis->pconnect($this->aServer[0], $this->aServer[1], (float)$this->timeout) :
  60. $this->oRedis->connect($this->aServer[0], $this->aServer[1], (float)$this->timeout);
  61. }
  62. if(isset($this->aServer[2])&&!empty($this->aServer[2])){//新增密码-wsc 2019.12.27
  63. $this->oRedis->auth($this->aServer[2]);
  64. }
  65. /*
  66. if(IS_DEBUF){
  67. $this->oRedis->select(1);
  68. }
  69. */
  70. //$this->oRedis->setOption(Redis::OPT_PREFIX, $this->prefix); //Key前缀
  71. $this->oRedis->setOption(Redis::OPT_SERIALIZER, $this->seria ? Redis::SERIALIZER_PHP : Redis::SERIALIZER_NONE); //Redis::SERIALIZER_IGBINARY二进制序列化
  72. $this->version = $this->oRedis->info()['redis_version'];//新增版本判别-wsc 2019.12.27
  73. }catch (RedisException $e){ //连接失败,记录
  74. print_r($e);
  75. $this->errorlog('Connect', $e->getCode(), $e->getMessage(), false);
  76. }
  77. }
  78. return $this->connect ? true : false;
  79. }
  80. /**
  81. * 设置.有则覆盖.true成功false失败
  82. * @param String $key
  83. * @param Mixed $value
  84. * @return Boolean
  85. */
  86. public function set($key, $value){
  87. return $this->connect() && $this->oRedis->set( $key, $value);
  88. }
  89. /**
  90. * 设置带过期时间的值
  91. * @param String $key
  92. * @param Mixed $value
  93. * @param int $expire 过期时间.默认24小时
  94. * @return Boolean
  95. */
  96. public function setex($key, $value, $expire=86400){
  97. return $this->connect() ? $this->oRedis->setex( $key, $expire, $value) : false;
  98. }
  99. /**
  100. * 添加.存在该Key则返回false.
  101. * @param String $key
  102. * @param Mixed $value
  103. * @return Boolean
  104. */
  105. public function setnx($key, $value){
  106. return $this->connect() ? $this->oRedis->setnx( $key, $value) : false;
  107. }
  108. /**
  109. * 原子递增1.不存在该key则基数为0.注意因为serialize的关系不能在set方法的key上再执行此方法
  110. * @param String $key
  111. * @return false/int 返回最新的值
  112. */
  113. public function incr( $key){
  114. return $this->connect() ? $this->oRedis->incr( $key) : false;
  115. }
  116. /**
  117. * 原子递加指定的数.不存在该key则基数为0,注意$value可以为负数.返回的结果也可能是负数
  118. * @param String $key
  119. * @param int $value
  120. * @return false/int 返回最新的值
  121. */
  122. public function incrBy($key, $value){
  123. return $this->connect() ? $this->oRedis->incrBy( $key, (int)$value) : false;
  124. }
  125. /**
  126. * 原子递减1.不存在该key则基数为0.可以减成负数
  127. * @param String $key
  128. * @return false/int 返回最新的值
  129. */
  130. public function decr( $key){
  131. return $this->connect() ? $this->oRedis->decr( $key) : false;
  132. }
  133. /**
  134. * 原子递减指定的数.不存在该key则基数为0,注意$value可以是负数(负负得正就成递增了).可以减成负数
  135. * @param String $key
  136. * @param int $value
  137. * @return false/int 返回最新的值
  138. */
  139. public function decrBy($key, $value){
  140. return $this->connect() ? $this->oRedis->decrBy( $key, (int)$value) : false;
  141. }
  142. /**
  143. * 获取.不存在则返回false
  144. * @param String $key
  145. * @return false/Mixed
  146. */
  147. public function get( $key){
  148. return $this->connect() ? $this->oRedis->get( $key) : false;
  149. }
  150. /**
  151. * 先获取该key的值,然后以新值替换掉该key.该key不存在则添加同时返回false
  152. * @param String $key
  153. * @param Mixed $value
  154. * @return Mixed/false
  155. */
  156. public function getSet($key, $value){
  157. return $this->connect() ? $this->oRedis->getSet($key, $value) : false;
  158. }
  159. /**
  160. * 从存储器中随机获取一个key
  161. * @return String
  162. */
  163. public function randomKey(){
  164. return $this->connect() ? $this->oRedis->randomKey() : '';
  165. }
  166. /**
  167. * 选择数据库
  168. * @param int $dbindex 0-16(根据配置文件中的database)
  169. * @return Boolean成功或者库不存在
  170. */
  171. public function select( $dbindex){
  172. return $this->connect() ? $this->oRedis->select( $dbindex) : false;
  173. }
  174. /**
  175. * 把某个key转移到另一个db中
  176. * @param String $key
  177. * @param int $dbindex 0-...
  178. * @return Boolean 当前db中没有该key或者...
  179. */
  180. public function move($key, $dbindex){
  181. return $this->connect() ? $this->oRedis->move($key, $dbindex) : false;
  182. }
  183. /**
  184. * 重命名某个Key.注意如果目的key存在将会被覆盖
  185. * @param String $srcKey
  186. * @param String $dstKey
  187. * @return Boolean 源key和目的key相同或者源key不存在...
  188. */
  189. public function renameKey($srcKey, $dstKey){
  190. if(version_compare($this->version,'5.0.1')>=0){
  191. return $this->connect() ? $this->oRedis->rename($srcKey, $dstKey) : false;
  192. }
  193. return $this->connect() ? $this->oRedis->renameKey($srcKey, $dstKey) : false;
  194. }
  195. /**
  196. * 重命名某个Key.和renameKey不同: 如果目的key存在将不执行
  197. * @param String $srcKey
  198. * @param String $dstKey
  199. * @return Boolean 源key和目的key相同或者源key不存在或者目的key存在
  200. */
  201. public function renameNx($srcKey, $dstKey){
  202. return $this->connect() ? $this->oRedis->renameNx($srcKey, $dstKey) : false;
  203. }
  204. /**
  205. * 设置某个key过期时间(Time To Live)expire.只能设置一次
  206. * @param String $key
  207. * @param int $ttl 存活时长(秒)
  208. * @return Boolean
  209. */
  210. public function setTimeout($key, $ttl){
  211. if(version_compare($this->version,'5.0.1')>=0){
  212. return $this->connect() ? $this->oRedis->expire($key, $ttl) : false;
  213. }
  214. return $this->connect() ? $this->oRedis->setTimeout($key, $ttl) : false;
  215. }
  216. /**
  217. * 设置某个key在特定的时间过期
  218. * @param String $key
  219. * @param int $timestamp 时间戳
  220. * @return Boolean
  221. */
  222. public function expireAt($key, $timestamp){
  223. return $this->connect() ? $this->oRedis->expireAt($key, $timestamp) : false;
  224. }
  225. /**
  226. * 设置某个key有效期
  227. * @param String $key
  228. * @param int $expire 有效时间(s)
  229. * @return Boolean
  230. */
  231. public function expire($key, $expire){
  232. return $this->connect() ? $this->oRedis->expire($key, $expire) : false;
  233. }
  234. /**
  235. * 获取对$key的描述.STRING for "encoding", LONG for "refcount" and "idletime", FALSE if the key doesn't exist
  236. * @param String $retrieve
  237. */
  238. public function object($retrieve, $key){
  239. return $this->connect() ? $this->oRedis->object($retrieve, $key) : false;
  240. }
  241. /**
  242. * 批量获取.注意: 如果某键不存在则对应的值为false
  243. * @param Array $keys
  244. * @return Array 原顺序返回
  245. */
  246. public function getMultiple( $keys){
  247. if(version_compare($this->version,'5.0.1')>=0){
  248. return $this->connect() && is_array( $keys) && count( $keys) ? $this->oRedis->mget( $keys) : array();
  249. }
  250. return $this->connect() && is_array( $keys) && count( $keys) ? $this->oRedis->getMultiple( $keys) : array();
  251. }
  252. /**
  253. * List章节 无索引序列 把元素加入到队列左边(头部).如果不存在则创建一个队列.返回该队列当前元素个数/false
  254. * 注意对值的匹配要考虑到serialize.array(1,2)和array(2,1)是不同的值
  255. * @param String $key
  256. * @param Mixed $value
  257. * @return false/Int. 如果连接不上或者该key已经存在且不是一个队列
  258. */
  259. public function lPush($key, $value){
  260. return $this->connect() ? $this->oRedis->lPush($key, $value) : false;
  261. }
  262. /**
  263. * 往一个已存在的队列左边加元素.返回0(如果队列不存在)或最新的元素个数
  264. * @param String $key
  265. * @param Mixed $value
  266. * @return false/Int. 如果连接不上或者该key不存在或者不是一个队列
  267. */
  268. public function lPushx($key, $value){
  269. return $this->connect() ? $this->oRedis->lPushx($key, $value) : false;
  270. }
  271. /**
  272. * 把元素加入到队列右边(尾部).如果不存在则创建一个队列.返回该队列当前元素个数/false
  273. * @param String $key
  274. * @param Mixed $value
  275. * @return false/int 如果连接不上或者该key已经存在且不是一个队列
  276. */
  277. public function rPush($key, $value){
  278. return $this->connect() ? $this->oRedis->rPush($key, $value) : false;
  279. }
  280. /**
  281. * 往一个已存在的队列右边加元素.返回0(如果队列不存在)或最新的元素个数
  282. * @param String $key
  283. * @param Mixed $value
  284. * @return false/Int. 如果连接不上或者该key不存在或者不是一个队列
  285. */
  286. public function rPushx($key, $value){
  287. return $this->connect() ? $this->oRedis->rPushx($key, $value) : false;
  288. }
  289. /**
  290. * 弹出(返回并清除)队列头部(最左边)元素
  291. * @param String $key
  292. * @return Mixed/false
  293. */
  294. public function lPop( $key){
  295. return $this->connect() ? $this->oRedis->lPop( $key) : false;
  296. }
  297. /**
  298. * 弹出队列尾部(最右边)元素
  299. * @param String $key
  300. * @return Mixed/false
  301. */
  302. public function rPop( $key){
  303. return $this->connect() ? $this->oRedis->rPop( $key) : false;
  304. }
  305. /**
  306. * 情况形如lPop方法.只要其中一个列表存在且有值则立即返回.否则等待对应的秒数直到有相应的列表加入为止(慎用)
  307. * 大致用途就是:监听N个列表,只要其中有一个列表有数据就立即返回该列表左边的数据
  308. * @param String/Array $keys
  309. * @param int $timeout
  310. * @return Array array('列表键名', '列表最左边的值')
  311. */
  312. public function blPop($keys, $timeout){
  313. if( ! $this->connect()){
  314. return array();
  315. }
  316. try{
  317. $value = $this->oRedis->blPop( $keys, $timeout);
  318. }catch (RedisException $e){
  319. $value = array();
  320. }
  321. return is_array( $value) ? $value : array();
  322. }
  323. /**
  324. * 情况形如rPop方法.这里指定一个延时只要其中一个列表存在且有值则立即返回.否则等待对应的秒数直到有相应的列表加入为止(慎用)
  325. * 参考:blPop
  326. * @param String/Array $keys
  327. * @param int $timeout
  328. * @return Array array('列表键名', '列表最右边的值')
  329. */
  330. public function brPop($keys, $timeout){
  331. if( ! $this->connect()){
  332. return array();
  333. }
  334. try{
  335. $value = $this->oRedis->brPop( $keys, $timeout);
  336. }catch (RedisException $e){
  337. $value = array();
  338. }
  339. return is_array( $value) ? $value : array();
  340. }
  341. /**
  342. * 返回队列里的元素个数.不存在则为0.不是队列则为false
  343. * @param String $key
  344. * @return int/false
  345. */
  346. public function lSize( $key){
  347. if(version_compare($this->version,'5.0.1')>=0){
  348. return $this->connect() ? $this->oRedis->lLen( $key) : false;
  349. }
  350. return $this->connect() ? $this->oRedis->lSize( $key) : false;
  351. }
  352. /**
  353. * 控制队列只保存某部分listTrim,即:删除队列的其余部分
  354. * @param String $key
  355. * @param int $start
  356. * @param int $end
  357. * @return Boolean 不是一个队列或者不存在...
  358. */
  359. public function lTrim($key, $start, $end){
  360. return $this->connect() && $this->oRedis->lTrim($key, $start, $end);
  361. }
  362. /**
  363. * 获取队列的某个元素
  364. * @param String $key
  365. * @param int $index 0第一个1第二个...-1最后一个-2倒数第二个
  366. * @return Mixed/false 没有则为空字符串或者false
  367. */
  368. public function lGet($key, $index){
  369. if(version_compare($this->version,'5.0.1')>=0){
  370. return $this->connect() ? $this->oRedis->lindex($key, $index) : false;
  371. }
  372. return $this->connect() ? $this->oRedis->lGet($key, $index) : false;
  373. }
  374. /**
  375. * 修改队列中指定$index的元素
  376. * @param String $key
  377. * @param int $index
  378. * @param Mixed $value
  379. * @return Boolean 该$index不存在或者该key不是一个队列为false
  380. */
  381. public function lSet($key, $index, $value){
  382. return $this->connect() && $this->oRedis->lSet($key, $index, $value);
  383. }
  384. /**
  385. * 取出队列的某一段.不存在则返回空数组
  386. * @param String $key
  387. * @param String $start 相当于$index:第一个为0...最后一个为-1
  388. * @param String $end
  389. * @return Array
  390. */
  391. public function lGetRange($key, $start, $end){
  392. if(version_compare($this->version,'5.0.1')>=0){
  393. return $this->connect() ? $this->oRedis->lrange($key, $start, $end) : array();
  394. }
  395. return $this->connect() ? $this->oRedis->lGetRange($key, $start, $end) : array();
  396. }
  397. /**
  398. * 删掉队列中的某些值
  399. * @param String $key
  400. * @param Mixed $value 要删除的值.可以是复杂数据,但要考虑serialize
  401. * @param int $count 去掉的个数,>0从左到右去除;0为去掉所有;<0从右到左去除
  402. * @return Boolean/int 删掉的值
  403. */
  404. public function lRemove($key, $value, $count=0){
  405. if(version_compare($this->version,'5.0.1')>=0){
  406. return $this->connect() ? $this->oRedis->lrem($key, $value, $count) : false;
  407. }
  408. return $this->connect() ? $this->oRedis->lRemove($key, $value, $count) : false;
  409. }
  410. /**
  411. * 在队列的某个特定值前/后面插入元素(如果有多个相同特定值则确定为左边起第一个)
  412. * @param String $key
  413. * @param int $direct 0往后面插入1往前面插入
  414. * @param Mixed $pivot
  415. * @param Mixed $value
  416. * @return false/int 列表当前元素个数或者-1表示元素不存在或不是列表
  417. */
  418. public function lInsert($key, $direct, $pivot, $value){
  419. return $this->connect() ? $this->oRedis->lInsert($key, $direct?Redis::BEFORE:Redis::AFTER, $pivot, $value) : false;
  420. }
  421. /**
  422. * 给该key添加一个唯一值.相当于制作一个没有重复值的数组
  423. * @param String $key
  424. * @param Mixed $value
  425. * @return false/int 该值存在或者该键不是一个集合返回0,连接失败为false,否则为添加成功的个数1
  426. */
  427. public function sAdd($key, $value){
  428. return $this->connect() ? $this->oRedis->sAdd($key, $value) : false;
  429. }
  430. /**
  431. * 获取某key对象个数
  432. * @param String $key
  433. * @return int 不存在则为0
  434. */
  435. public function sSize( $key){
  436. if(version_compare($this->version,'5.0.1')>=0){
  437. return $this->connect() ? $this->oRedis->scard( $key) : 0;
  438. }
  439. return $this->connect() ? $this->oRedis->sSize( $key) : 0;
  440. }
  441. /**
  442. * 随机弹出一个值.
  443. * @param String $key
  444. * @return Mixed/false 没有值了或者不是一个集合
  445. */
  446. public function sPop( $key){
  447. return $this->connect() ? $this->oRedis->sPop( $key) : false;
  448. }
  449. /**
  450. * 随机取出一个值.与sPop不同,它不删除值
  451. * @param String $key
  452. * @return Mixed/false
  453. */
  454. public function sRandMember( $key){
  455. return $this->connect() ? $this->oRedis->sRandMember( $key) : false;
  456. }
  457. /**
  458. * 返回所给key列表都有的那些值,相当于求交集
  459. * $keys Array
  460. * @return Array 如果某集合不存在或者某键非集合返回空数组
  461. */
  462. public function sInter( $keys){
  463. return $this->connect() && is_array( $result = $this->oRedis->sInter( $keys)) ? $result : array();
  464. }
  465. /**
  466. * 把所给$keys列表都有的那些值存到$key指定的数组中.相当于执行sInter操作然后再存到另一个数组中
  467. * $key String 要存到的数组key 注意该数组如果存在会被覆盖
  468. * $keys Array
  469. * @return false/int 新集合的元素个数或者某key不存在为false
  470. */
  471. public function sInterStore($key, $keys){
  472. return $this->connect() ? call_user_func_array(array($this->oRedis,'sInterStore'), array_merge(array($key), $keys)) : 0;
  473. }
  474. /**
  475. * 返回所给key列表所有的值,相当于求并集
  476. * @param Array $keys
  477. * @return Array
  478. */
  479. public function sUnion( $keys){
  480. return $this->connect() && is_array( $result = $this->oRedis->sUnion( $keys)) ? $result : array();
  481. }
  482. /**
  483. * 把所给key列表所有的值存储到另一个数组
  484. * @param String $key
  485. * @param Array $keys
  486. * @return int/false 并集(新集合)的数量
  487. */
  488. public function sUnionStore($key, $keys){
  489. return $this->connect() ? call_user_func_array(array($this->oRedis,'sUnionStore'), array_merge(array($key), (array)$keys)) : 0;
  490. }
  491. /**
  492. * 返回所给key列表想减后的集合,相当于求差集
  493. * @param Array $keys 注意顺序,前面的减后面的
  494. * @return Array
  495. */
  496. public function sDiff( $keys){
  497. return $this->connect() && is_array($result = $this->oRedis->sDiff( $keys)) ? $result : array();
  498. }
  499. /**
  500. * 把所给key列表差集存储到另一个数组
  501. * @param String $key 要存储的目的数组
  502. * @param Array $keys
  503. * @return int/false 差集的数量
  504. */
  505. public function sDiffStore($key, $keys){
  506. return $this->connect() ? call_user_func_array(array($this->oRedis,'sDiffStore'), array_merge(array($key), (array)$keys)) : 0;
  507. }
  508. /**
  509. * 删除该集合中对应的值
  510. * @param String $key
  511. * @param String $value
  512. * @return Boolean 没有该值返回false
  513. */
  514. public function sRemove($key, $value){
  515. if(version_compare($this->version,'5.0.1')>=0){
  516. return $this->connect() && $this->oRedis->srem($key, $value);
  517. }
  518. return $this->connect() && $this->oRedis->sRemove($key, $value);
  519. }
  520. /**
  521. * 把某个值从一个key转移到另一个key
  522. * @param String $srcKey
  523. * @param String $dstKey
  524. * @param Mixed $value
  525. * @return Boolean 源key不存在/目的key不存在/源值不存在->false
  526. */
  527. public function sMove($srcKey, $dstKey, $value){
  528. return $this->connect() && $this->oRedis->sMove($srcKey, $dstKey, $value);
  529. }
  530. /**
  531. * 判断该数组中是否有对应的值
  532. * @param String $key
  533. * @param String $value
  534. * @return Boolean 集合不存在或者值不存在->false
  535. */
  536. public function sContains($key, $value){
  537. if(version_compare($this->version,'5.0.1')>=0){
  538. return $this->connect() && $this->oRedis->sismember($key, $value);
  539. }
  540. return $this->connect() && $this->oRedis->sContains($key, $value);
  541. }
  542. /**
  543. * 获取某数组所有值sGetMembers
  544. * @param String $key
  545. * @return Array 顺序是不固定的
  546. */
  547. public function sMembers( $key){
  548. return $this->connect() && is_array($result = $this->oRedis->sMembers( $key)) ? $result : array();
  549. }
  550. /**
  551. * 有序集合.添加一个指定了索引值的元素(默认索引值为0).元素在集合中存在则更新对应$score
  552. * @param String $key
  553. * @param int $score 索引值
  554. * @param Mixed $value 注意考虑到默认使用了序列化,此处最好强制数据类型
  555. * @return false/int 成功加入的个数
  556. */
  557. public function zAdd($key, $score, $value){
  558. return $this->connect() ? $this->oRedis->zAdd($key, $score, $value) : false;
  559. }
  560. /**
  561. * 获取指定单元的数据
  562. * @param String $key
  563. * @param int $start 起始位置,从0开始
  564. * @param int $end 结束位置,-1结束
  565. * @param Boolean $withscores 是否返回索引值.如果是则返回[值=>索引]的数组.如果要返回索引值,存入的时候$value必须是标量
  566. * @return Array
  567. */
  568. public function zRange($key, $start, $end, $withscores=false){
  569. return $this->connect() && is_array($result = $this->oRedis->zRange($key, $start, $end, $withscores)) ? $result : array();
  570. }
  571. /**
  572. * 获取指定单元的反序排列的数据
  573. * @param String $key
  574. * @param int $start
  575. * @param int $end
  576. * @param Boolean $withscores 是否返回索引值.如果是则返回值=>索引的数组
  577. * @return Array
  578. */
  579. public function zRevRange($key, $start, $end, $withscores=false){
  580. return $this->connect() && is_array($result = $this->oRedis->zRevRange($key, $start, $end, $withscores)) ? $result : array();
  581. }
  582. /**
  583. * 获取指定条件下的集合
  584. * @param String $key
  585. * @param int $start 最小索引值
  586. * @param int $end 最大索引值
  587. * @param Array $options array('withscores'=>true,limit=>array($offset, $count))
  588. * @return Array
  589. */
  590. public function zRangeByScore($key, $start, $end, $options=array()){
  591. return $this->connect() && is_array($result = $this->oRedis->zRangeByScore($key, $start, $end, $options)) ? $result : array();
  592. }
  593. /**
  594. * 获取指定条件下的反序排列集合
  595. * @param String $key
  596. * @param int $start 最大索引值
  597. * @param int $end 最小索引值
  598. * @param Array $options array('withscores'=>true,limit=>array($offset, $count))
  599. * @return Array
  600. */
  601. public function zRevRangeByScore($key, $start, $end, $options=array()){
  602. return $this->connect() && is_array($result = $this->oRedis->zRevRangeByScore($key, $start, $end, $options)) ? $result : array();
  603. }
  604. /**
  605. * 返回指定索引值区域内的元素个数
  606. * @param String $key
  607. * @param int/String $start 最小索引值 前面加左括号表示不包括本身如: '(3' 表示>3而不是默认的>=3
  608. * @param int/String $end 最大索引值 '(4'表示...
  609. * @return int
  610. */
  611. public function zCount($key, $start, $end){
  612. return $this->connect() ? $this->oRedis->zCount($key, $start, $end) : 0;
  613. }
  614. /**
  615. * 删除指定索引值区域内的所有元素zRemRangeByScore
  616. * @param String $key
  617. * @param int $start 最小索引值
  618. * @param int $end 最大索引值
  619. * @return int
  620. */
  621. public function zDeleteRangeByScore($key, $start, $end){
  622. if(version_compare($this->version,'5.0.1')>=0){
  623. return $this->connect() ? $this->oRedis->zRemRangeByScore($key, $start, $end) : 0;
  624. }
  625. return $this->connect() ? $this->oRedis->zDeleteRangeByScore($key, $start, $end) : 0;
  626. }
  627. /**
  628. * 删除指定排序范围内的所有元素
  629. * @param int $start 排序起始值
  630. * @param int $end
  631. * @return int
  632. */
  633. public function zDeleteRangeByRank($key, $start, $end){
  634. if(version_compare($this->version,'5.0.1')>=0){
  635. return $this->connect() ? $this->oRedis->zRemRangeByRank($key, $start, $end) : 0;
  636. }
  637. return $this->connect() ? $this->oRedis->zDeleteRangeByRank($key, $start, $end) : 0;
  638. }
  639. /**
  640. * 获取集合元素个数zCard
  641. * @param String $key
  642. * @return int
  643. */
  644. public function zSize( $key){
  645. return $this->connect() ? $this->oRedis->zSize( $key) : 0;
  646. }
  647. /**
  648. * 获取某集合中某元素的索引值
  649. * @param String $key
  650. * @param String $member
  651. * @return int/false 没有该值为false
  652. */
  653. public function zScore($key, $member){
  654. return $this->connect() ? $this->oRedis->zScore( $key, $member) : 0;
  655. }
  656. /**
  657. * 获取指定元素的排序值
  658. * @param String $key
  659. * @param String $member
  660. * @return int/false 不存在为false
  661. */
  662. public function zRank($key, $member){
  663. return $this->connect() ? $this->oRedis->zRank( $key, $member) : 0;
  664. }
  665. /**
  666. * 获取指定元素的反向排序值
  667. * @param String $key
  668. * @param String $member
  669. * @return int/false 不存在为false
  670. */
  671. public function zRevRank($key, $member){
  672. return $this->connect() ? $this->oRedis->zRevRank( $key, $member) : 0;
  673. }
  674. /**
  675. * 给指定的元素累加索引值.元素不存在则会被添加
  676. * @param String $key
  677. * @param int $value 要加的索引值量
  678. * @param String $member
  679. * @return int 该元素最新的索引值
  680. */
  681. public function zIncrBy($key, $value, $member){
  682. return $this->connect() ? $this->oRedis->zIncrBy( $key, $value, $member) : 0;
  683. }
  684. /**
  685. * 得到一个并集存储到新的集合中
  686. * @param String $keyOutput 新集合名
  687. * @param Array $arrayZSetKeys 需要合并的集合 array('key1', 'key2')
  688. * @param Array $arrayWeights 对应集合中索引值要放大的倍数 array(5, 2)表示第一个集合中的索引值*5,第二个集合中的索引值*2,然后再合并
  689. * @param String $aggregateFunction 如果有相同元素,则取索引值的方法: "SUM", "MIN", "MAX"
  690. * @return int 新集合的元素个数
  691. */
  692. public function zUnion($keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction){
  693. if(version_compare($this->version,'5.0.1')>=0){
  694. return $this->connect() ? $this->oRedis->zunionstore( $keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction) : 0;
  695. }
  696. return $this->connect() ? $this->oRedis->zUnion( $keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction) : 0;
  697. }
  698. /**
  699. * 得到一个交集存储到新的集合中
  700. * @param String $keyOutput 新集合名
  701. * @param Array $arrayZSetKeys 需要合并的集合 array('key1', 'key2')
  702. * @param Array $arrayWeights 对应集合中索引值要放大的倍数 array(5, 2)表示第一个集合中的索引值*5,第二个集合中的索引值*2,然后再合并
  703. * @param String $aggregateFunction 如果有相同元素,则取索引值的方法: "SUM", "MIN", "MAX"
  704. * @return int 新集合的元素个数
  705. */
  706. public function zInter($keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction){
  707. if(version_compare($this->version,'5.0.1')>=0){
  708. return $this->connect() ? $this->oRedis->zinterstore( $keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction) : 0;
  709. }
  710. return $this->connect() ? $this->oRedis->zInter( $keyOutput, $arrayZSetKeys, $arrayWeights, $aggregateFunction) : 0;
  711. }
  712. /**
  713. * 设置或替换Hash.
  714. * @param String $key
  715. * @param String $hashKey
  716. * @param Mixed $value
  717. * @return Boolean
  718. */
  719. public function hSet($key, $hashKey, $value){
  720. return $this->connect() && in_array($this->oRedis->hSet($key, $hashKey, $value), array(0,1), true) ? true : false; //该处特殊.0为替换成功1为添加成功false为操作失败
  721. }
  722. /**
  723. * 添加式
  724. * @param String $key
  725. * @param String $hashKey
  726. * @param Mixed $value
  727. * @return Boolean
  728. */
  729. public function hSetNx($key, $hashKey, $value){
  730. return $this->connect() && $this->oRedis->hSetNx($key, $hashKey, $value);
  731. }
  732. /**
  733. * 获取单个.失败或不存在为false
  734. * @param String $key
  735. * @param String $hashKey
  736. * @return Mixed
  737. */
  738. public function hGet($key, $hashKey){
  739. return $this->connect() ? $this->oRedis->hGet($key, $hashKey) : false;
  740. }
  741. /**
  742. * 该Key上Hash数量
  743. * @param String $key
  744. * @return int
  745. */
  746. public function hLen( $key){
  747. return $this->connect() ? $this->oRedis->hLen( $key) : 0;
  748. }
  749. /**
  750. * 删除.成功为true,否则false
  751. * @param String $key
  752. * @param String $hashKey
  753. * @return Boolean
  754. */
  755. public function hDel($key, $hashKey){
  756. return $this->connect() && $this->oRedis->hDel($key, $hashKey);
  757. }
  758. /**
  759. * 获取所有Key.不存在则为空数组
  760. * @param String $key
  761. * @return Array
  762. */
  763. public function hKeys( $key){
  764. return $this->connect() && ($result = $this->oRedis->hKeys($key)) ? $result : array();
  765. }
  766. /**
  767. * 获取所有值.不存在则为空数组
  768. * @param String $key
  769. * @return Array
  770. */
  771. public function hVals( $key){
  772. return $this->connect() && ($result = $this->oRedis->hVals( $key)) ? $result : array();
  773. }
  774. /**
  775. * 获取所有键值对
  776. * @param String $key
  777. * @return Array
  778. */
  779. public function hGetAll( $key){
  780. return $this->connect() && ($result = $this->oRedis->hGetAll( $key)) ? $result : array();
  781. }
  782. /**
  783. * 判断$memberKey是否存在
  784. * @param String $key
  785. * @param String $memberKey
  786. * @return Boolean
  787. */
  788. public function hExists($key, $memberKey){
  789. return $this->connect() && $this->oRedis->hExists($key, $memberKey);
  790. }
  791. /**
  792. * 累加减操作.可以减为负数.如果初始值不是整型或者$value不是整型则为false
  793. * 注意: 因为默认启用了序列化,只能通过此方法设置的$key上做此操作!!!
  794. * @param String $key
  795. * @param String $member
  796. * @param int $value 负数则为减
  797. * @return int/false 最新的值
  798. */
  799. public function hIncrBy($key, $member, $value){
  800. return $this->connect() ? $this->oRedis->hIncrBy($key, $member, $value) : false;
  801. }
  802. /**
  803. * 批量获取.key不存在的对应的值为false
  804. * @param String $key
  805. * @param Array $memberKeys
  806. * @return Array
  807. */
  808. public function hMget($key, $memberKeys){
  809. return $this->connect() && ($result = $this->oRedis->hMget($key, $memberKeys)) ? $result : array();
  810. }
  811. /**
  812. * 批量设置
  813. * @param String $key
  814. * @param Array $members 键值对
  815. * @return Boolean
  816. */
  817. public function hMset($key, $members){
  818. return $this->connect() && $this->oRedis->hMset($key, $members);
  819. }
  820. /**
  821. * 往值后面追加字符串.不存在则创建
  822. * @param String $key
  823. * @param String $value
  824. * @return int 最新值的长度
  825. */
  826. public function append($key, $value){
  827. return $this->connect() ? $this->oRedis->append( $key, $value) : 0;
  828. }
  829. /**
  830. * 获取字符串的一部分.此方法仅针对append加的字符串有意义
  831. * @param int $start
  832. * @param int $end
  833. * @return String 不存在则为''
  834. */
  835. public function getRange($key, $start, $end){
  836. return $this->connect() ? $this->oRedis->getRange($key, $start, $end) : '';
  837. }
  838. /**
  839. * 从$offset开始替换后面的字符串.$offset从0开始
  840. * @param String $key
  841. * @param int $offset
  842. * @param String $value
  843. * @return int 字符串最新的长度
  844. */
  845. public function setRange($key, $offset, $value){
  846. return $this->connect() ? $this->oRedis->setRange($key, $offset, $value) : 0;
  847. }
  848. /**
  849. * 返回值的长度
  850. * @param String $key
  851. * @return int 不存在为0
  852. */
  853. public function strlen( $key){
  854. return $this->connect() ? $this->oRedis->strlen( $key) : 0;
  855. }
  856. /**
  857. * 返回列表,集合,有序集合排序后的数据或者存储的元素个数
  858. * $options = array('by' => 'some_pattern_*',
  859. 'limit' => array(0, 1),
  860. 'get' => 'some_other_pattern_*' or an array of patterns,
  861. 'sort' => 'asc' or 'desc',
  862. 'alpha' => true, //按字母排序
  863. 'store' => 'external-key')
  864. *@return Array/int
  865. */
  866. public function sort($key, $options){
  867. return $this->connect() ? $this->oRedis->sort( $key, $options) : array();
  868. }
  869. /**
  870. * 移除某key的过期时间使得永不过期
  871. * @return Boolean 没有设置过期时间或者没有该Key返回false
  872. */
  873. public function persist( $key){
  874. return $this->connect() && $this->oRedis->persist( $key);
  875. }
  876. /**
  877. * 启动后台回写至硬盘
  878. * @return Boolean
  879. */
  880. public function bgrewriteaof(){
  881. return $this->connect() && $this->oRedis->bgrewriteaof();
  882. }
  883. /**
  884. * 转换从DB角色
  885. * @param String $host 从DB地址
  886. * @param String $port 从DB端口
  887. * @return Boolean
  888. */
  889. public function slaveof($host, $port){
  890. return $this->connect() && $this->oRedis->slaveof($host, $port);
  891. }
  892. /**
  893. * 开始一个事务处理
  894. * @param int $mode 事务类型1保证原子性2不保证
  895. * @return muredis
  896. *$ret = $redis->multi()
  897. ->set('key1', 'val1')
  898. ->get('key1')
  899. ->set('key2', 'val2')
  900. ->get('key2')
  901. ->exec();
  902. $ret == array(
  903. 0 => TRUE,
  904. 1 => 'val1',
  905. 2 => TRUE,
  906. 3 => 'val2');
  907. */
  908. public function multi( $mode=1){
  909. return $this->connect() ? $this->oRedis->multi($mode==1 ? Redis::MULTI : Redis::PIPELINE) : $this;
  910. }
  911. /**
  912. * 回滚事务
  913. * @return Boolean
  914. */
  915. public function discard(){
  916. return $this->connect() && $this->oRedis->discard();
  917. }
  918. /**
  919. * 提交事务
  920. * @return Mixed 返回事务中各方法的返回值.如果采用了watch锁而值被改或者没有任何执行,则强制返回空数组
  921. */
  922. public function exec(){
  923. return $this->connect() && is_array( $result = $this->oRedis->exec()) ? $result : array();
  924. }
  925. /**
  926. * 被动锁定某个/某些key.用于事务处理中:如果被锁定的key在提交事务前被改了则事务提交失败
  927. * @return Boolean
  928. */
  929. public function watch( $keys){
  930. return $this->connect() && $this->oRedis->watch($keys);
  931. }
  932. /**
  933. * 解锁所有被锁key
  934. * @return Boolean
  935. */
  936. public function unwatch(){
  937. return $this->connect() && $this->oRedis->unwatch();
  938. }
  939. /**
  940. * 以下未确定
  941. public function publish(){
  942. }
  943. public function subscribe(){
  944. }
  945. public function unsubscribe(){
  946. }
  947. public function open(){
  948. }
  949. public function lLen(){
  950. }
  951. public function pipeline(){
  952. }
  953. public function brpoplpush(){
  954. }
  955. public function sortAsc(){
  956. }
  957. public function sortAscAlpha(){
  958. }
  959. public function sortDesc(){
  960. }
  961. public function sortDescAlpha(){
  962. }
  963. */
  964. /**
  965. * 获取对应值的某一位
  966. * @param String $key
  967. * @param int $offset 要获取的位置(负数返回false)
  968. * @return false/0/1 (不存在为0)
  969. */
  970. public function getBit($key, $offset){
  971. return $this->connect() ? $this->oRedis->getBit($key, $offset) : false;
  972. }
  973. /**
  974. * 设置对应值的某一位(位运算)
  975. * @param String $key
  976. * @param int $offset 要修改的位置(负数则返回false)
  977. * @param int $value 要修改的值.只能是: false,true,0,1
  978. * @return false/0/1 返回该位置修改前的值
  979. */
  980. public function setBit($key, $offset, $value){
  981. return $this->connect() ? $this->oRedis->setBit($key, $offset, $value) : false;
  982. }
  983. /**
  984. * 计算给定字符串中,被设置为 1 的比特位的数量
  985. * @param string $key
  986. */
  987. public function bitCount($key){
  988. return $this->connect() ? $this->oRedis->bitCount($key) : false;
  989. }
  990. /**
  991. * 获取环境
  992. * @param String $option
  993. * @return Mixed
  994. */
  995. public function getOption( $option){
  996. return $this->connect() ? $this->oRedis->getOption($option) : '';
  997. }
  998. /**
  999. * 设置配置.参看: Redis::...
  1000. * @param String $name
  1001. * @param String $value
  1002. * @return Boolean
  1003. */
  1004. public function setOption($name, $value){
  1005. return $this->connect() && $this->oRedis->setOption($name, $value);
  1006. }
  1007. /**
  1008. * 删除对应的值zRem
  1009. * @param String $key
  1010. * @param Mixed $value
  1011. * @return Boolean/int 删除元素的个数(0/1)
  1012. */
  1013. public function zDelete($key, $value){
  1014. if(version_compare($this->version,'5.0.1')>=0){
  1015. return $this->connect() ? $this->oRedis->zRem($key, $value) : false;
  1016. }
  1017. return $this->connect() ? $this->oRedis->zDelete($key, $value) : false;
  1018. }
  1019. /**
  1020. * 返回服务器信息
  1021. * @return Array
  1022. */
  1023. public function info(){
  1024. return $this->connect() ? $this->oRedis->info() : array();
  1025. }
  1026. /**
  1027. * 重置统计信息
  1028. * Keyspace hits
  1029. * Keyspace misses
  1030. * Number of commands processed
  1031. * Number of connections received
  1032. * Number of expired keys
  1033. * @return Boolean
  1034. */
  1035. public function resetStat(){
  1036. return $this->connect() && $this->oRedis->resetStat();
  1037. }
  1038. /**
  1039. * 返回某key剩余的时间.单位是秒
  1040. * @param String $key
  1041. * @return int/false -1为没有设置过期时间
  1042. */
  1043. public function ttl( $key){
  1044. return $this->connect() ? $this->oRedis->ttl( $key) : 0;
  1045. }
  1046. /**
  1047. * 批量设置
  1048. * @param Array $pairs 索引数组,索引为key,值为...
  1049. * @return Boolean
  1050. */
  1051. public function mset( $pairs){
  1052. return $this->connect() && is_array( $pairs) && $this->oRedis->mset( $pairs);
  1053. }
  1054. /**
  1055. * 批量添加.如果某key存在则为false并且其他key也不会被保存
  1056. * @param Array $pairs 索引数组,索引为key,值为...
  1057. * @return Boolean
  1058. */
  1059. public function msetnx( $pairs){
  1060. return $this->connect() && is_array( $pairs) && $this->oRedis->msetnx( $pairs);
  1061. }
  1062. /**
  1063. * 批量获取数据
  1064. * @param Array $pairs 数组,其value为KEY组合
  1065. * @return Mixed 如果成功,返回与KEY对应位置的VALUE组成的数组
  1066. */
  1067. public function mget( $pairs){
  1068. return $this->connect() && is_array( $pairs ) && is_array( $result = $this->oRedis->mget( $pairs)) ? $result : array();
  1069. }
  1070. /**
  1071. * 从源队列尾部弹出一项加到目的队列头部.并且返回该项
  1072. * @param String $srcKey
  1073. * @param String $dstKey
  1074. * @return Mixed/false
  1075. */
  1076. public function rpoplpush($srcKey, $dstKey){
  1077. return $this->connect() ? $this->oRedis->rpoplpush($srcKey, $dstKey) : false;
  1078. }
  1079. /**
  1080. * 判断key是否存在
  1081. * @param String $key
  1082. * @return Boolean
  1083. */
  1084. public function exists( $key){
  1085. return $this->connect() && $this->oRedis->exists( $key);
  1086. }
  1087. /**
  1088. * 获取符合匹配的key.仅支持正则中的*通配符.如->getKeys('*')
  1089. * @param String $pattern
  1090. * @return Array
  1091. */
  1092. public function getKeys( $pattern){
  1093. if(version_compare($this->version,'5.0.1')>=0){
  1094. return $this->connect() && is_array($result = $this->oRedis->keys( $pattern)) ? $result : array();
  1095. }
  1096. return $this->connect() && is_array($result = $this->oRedis->getKeys( $pattern)) ? $result : array();
  1097. }
  1098. /**
  1099. * 获取符合匹配的key.仅支持正则中的*通配符.如->getKeys('*') 禁用 - - 全局堵塞
  1100. * @param String $pattern
  1101. * @return Array
  1102. */
  1103. public function keys($pattern){
  1104. return $this->connect() && is_array($result = $this->oRedis->keys($pattern)) ? $result : array();
  1105. }
  1106. /**
  1107. * 检索符合匹配Key -- 分批非堵塞
  1108. * @param $pattern
  1109. * @param int $count
  1110. * @return array|bool
  1111. * Created by: Owen
  1112. * Created on: 2019/11/30 9:55
  1113. */
  1114. public function scan($pattern,$count = 1000){
  1115. $iterator = null;
  1116. if(!$this->connect()){
  1117. return [];
  1118. }
  1119. $result = [];
  1120. while(true) {
  1121. $keys = $this->oRedis->scan($iterator,$pattern,$count);
  1122. if($keys === false){
  1123. break;
  1124. }else if(!empty($keys)){
  1125. foreach ($keys as $key){
  1126. $result[] = $key;
  1127. }
  1128. }
  1129. }
  1130. return $result;
  1131. }
  1132. /**
  1133. * 删除某key/某些key
  1134. * @param String/Array $keys
  1135. * @return int 被删的个数
  1136. */
  1137. public function delete( $keys){
  1138. return $this->connect() ? $this->oRedis->del( $keys) : 0;
  1139. }
  1140. /**
  1141. * 返回当前key数量
  1142. * @return int
  1143. */
  1144. public function dbSize(){
  1145. return $this->connect() ? $this->oRedis->dbSize() : 0;
  1146. }
  1147. /**
  1148. * 密码验证.密码明文传输
  1149. * @param String $password
  1150. * @return Boolean
  1151. */
  1152. public function auth( $password){
  1153. return $this->connect() && $this->oRedis->auth( $password);
  1154. }
  1155. /**
  1156. * 强制把内存中的数据写回硬盘
  1157. * @return Boolean 如果正在回写则返回false
  1158. */
  1159. public function save(){
  1160. return $this->connect() && $this->oRedis->save();
  1161. }
  1162. /**
  1163. * 执行一个后台任务: 强制把内存中的数据写回硬盘
  1164. * @return Boolean 如果正在回写则返回false
  1165. */
  1166. public function bgSave(){
  1167. return $this->connect() && $this->oRedis->bgSave();
  1168. }
  1169. /**
  1170. * 返回最后一次写回硬盘的时间
  1171. * @return int 时间戳
  1172. */
  1173. public function lastSave(){
  1174. return $this->connect() ? $this->oRedis->lastSave() : 0;
  1175. }
  1176. /**
  1177. * 返回某key的数据类型
  1178. * @param String $key
  1179. * @return int 存在于: REDIS_* 中
  1180. */
  1181. public function type( $key){
  1182. return $this->connect() ? $this->oRedis->type( $key) : Redis::REDIS_NOT_FOUND;
  1183. }
  1184. /**
  1185. * 清空当前数据库.谨慎执行
  1186. * @return int 1成功
  1187. */
  1188. public function flushDB(){
  1189. return $this->connect() && $this->oRedis->flushDB();
  1190. }
  1191. /**
  1192. * 清空所有数据库.谨慎执行
  1193. * @return Boolean
  1194. */
  1195. public function flushAll(){
  1196. return $this->connect() && $this->oRedis->flushAll();
  1197. }
  1198. /**
  1199. * 获取连接信息
  1200. * @return Boolean
  1201. */
  1202. public function ping(){
  1203. try{return $this->connect() && $this->oRedis->ping();}catch(RedisException $e){return false;}
  1204. }
  1205. /**
  1206. * 设置或获取配置参数.禁用!!!
  1207. * @param String $operation GET/SET
  1208. * @param String $key
  1209. * @param null/String $value
  1210. * @return Array/Boolean
  1211. */
  1212. private function config($operation, $key, $value=null){
  1213. return $this->connect() ? $this->config($operation, $key, $value) : false;
  1214. }
  1215. /**
  1216. * 关闭非持久连接
  1217. */
  1218. public function close(){
  1219. $this->connect && (($this->connected = false) || $this->oRedis->close()); //确保关闭
  1220. }
  1221. private function errorlog($keys, $code, $msg, $die=false){
  1222. $error = date('H:i:s').":\n".$code.";\nkeys:".var_export($keys, true).";\nmsg:{$msg}\n";
  1223. //oo::logs()->logsUdp("debug4",$error, 'muredis.txt');
  1224. ($this->die || $die) && die('Redis Invalid!!!');
  1225. }
  1226. /**
  1227. * 设置变量 set setnx setex 同时使用
  1228. * @param String $key
  1229. * @param String $num
  1230. * @param String $ttl
  1231. * @return true false
  1232. */
  1233. public function setnxex($key,$num,$ttl){
  1234. return $this->connect() && $this->oRedis->set($key, $num, array('nx', 'ex' => $ttl));
  1235. }
  1236. }