CRedis.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of Redis
  9. *
  10. * @author jgao
  11. */
  12. class CRedis {
  13. //put your code here
  14. var $redis = null;
  15. public function conn($host, $port, $db=0) {
  16. $this->redis = new Redis();
  17. $ret = $this->redis->connect($host, $port);
  18. if($ret==null || !$ret){
  19. $this->halt("connect failed!");
  20. }
  21. $this->redis->select($db);
  22. return $ret;
  23. }
  24. public function get($key){
  25. $result = $this->redis->get($key);
  26. if (!$result || $result == "" || $result == null){
  27. return null;
  28. }
  29. return JsonUtil::decode($result);
  30. }
  31. public function set($key, $val) {
  32. if($val == null || $val == ""){
  33. return false;
  34. }
  35. $ret = $this->redis->set($key, JsonUtil::encode($val));
  36. return "OK" == $ret;
  37. }
  38. public function delete($key) {
  39. $infos = array(); $infos[] = $key;
  40. return $this->redis->del($infos);
  41. }
  42. public function cas($key, $value) {
  43. return $this->set($key, $value);
  44. }
  45. public function copy($surKey, $desKey) {
  46. return $this->set($desKey, $this->get($surKey));
  47. }
  48. public function close() {
  49. $this->redis->close();
  50. }
  51. /**
  52. * 获得Redis扩展客户端
  53. * @return Redis
  54. */
  55. public function getRedisClient(){
  56. return $this->redis;
  57. }
  58. /**
  59. * 错误中断
  60. * @param type $msg
  61. */
  62. function halt($msg=null){
  63. exit($msg.'<br /> '.$this->redis->getLastError());
  64. }
  65. }