12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?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.
- */
- /**
- * Description of Redis
- *
- * @author jgao
- */
- class CRedis {
- //put your code here
- var $redis = null;
-
- public function conn($host, $port, $db=0) {
- $this->redis = new Redis();
- $ret = $this->redis->connect($host, $port);
- if($ret==null || !$ret){
- $this->halt("connect failed!");
- }
- $this->redis->select($db);
- return $ret;
- }
-
- public function get($key){
- $result = $this->redis->get($key);
- if (!$result || $result == "" || $result == null){
- return null;
- }
- return JsonUtil::decode($result);
- }
-
- public function set($key, $val) {
- if($val == null || $val == ""){
- return false;
- }
- $ret = $this->redis->set($key, JsonUtil::encode($val));
- return "OK" == $ret;
- }
-
- public function delete($key) {
- $infos = array(); $infos[] = $key;
- return $this->redis->del($infos);
- }
-
- public function cas($key, $value) {
- return $this->set($key, $value);
- }
-
- public function copy($surKey, $desKey) {
- return $this->set($desKey, $this->get($surKey));
- }
-
- public function close() {
- $this->redis->close();
- }
-
- /**
- * 获得Redis扩展客户端
- * @return Redis
- */
- public function getRedisClient(){
- return $this->redis;
- }
-
- /**
- * 错误中断
- * @param type $msg
- */
- function halt($msg=null){
- exit($msg.'<br /> '.$this->redis->getLastError());
- }
- }
|