12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- require __DIR__.'/Predis/Autoloader.php';
- Predis\Autoloader::register();
- /**
- * Description of Predis
- *
- * @author jgao
- */
- class PRedis {
- //put your code here
- var $redis = null;
-
- public function conn($host, $port, $pwd, $db=0){
- $this->redis = new Predis\Client(array(
- 'scheme'=>'tcp',
- 'host'=>$host,
- 'port'=>$port,
- 'password'=>$pwd,
- ));
- $this->redis->select($db);
- return $this;
- }
-
- public function get($key){
- $result = $this->redis->get($key);
- if (!$result || $result == "" || $result == null){
- return null;
- }
- return JsonUtil::decode($result);
- }
-
- public function getMulti($keys) {
- $ctx = $this->redis->mget($keys);
- $count = count($ctx);
- for($i = 0; $i < $count; $i++) {
- $ctx[$i] = JsonUtil::decode($ctx[$i]);
- }
- return $ctx;
- }
-
- public function set($key, $val) {
- if($val == null || $val == ""){
- return false;
- }
- $ret = $this->redis->set($key, JsonUtil::encode($val));
- return "OK" == $ret;
- }
-
- public function cas($key, $value){
- return $this->set($key, $value);
- }
-
- public function delete($key) {
- $infos = array(); $infos[] = $key;
- return $this->redis->del($infos) == 1;
- }
-
- public function increment($key) {
- return $this->redis->incr($key);
- }
-
- public function copy($surKey, $desKey) {
- return $this->set($desKey, $this->get($surKey));
- }
- public function close() {
- $this->redis->quit();
- }
-
- /**
- * 获得Redis扩展客户端
- * @return Predis\Client
- */
- public function getRedisClient(){
- return $this->redis;
- }
-
- /**
- * 错误中断
- * @param type $msg
- */
- function halt($msg=null){
- exit($msg.'<br /> '.$this->redis->getLastError());
- }
- }
|