cache.redis.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * redis 操作
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class CacheRedis extends Cache {
  7. private $config;
  8. private $connected;
  9. private $type;
  10. private $prefix;
  11. public function __construct() {
  12. $this->config = C('redis');
  13. if (empty($this->config['slave'])) $this->config['slave'] = $this->config['master'];
  14. $this->prefix = $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
  15. if ( !extension_loaded('redis') ) {
  16. throw_exception('redis failed to load');
  17. }
  18. }
  19. private function init_master(){
  20. static $_cache;
  21. if (isset($_cache)){
  22. $this->handler = $_cache;
  23. }else{
  24. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  25. $this->handler = new Redis;
  26. $this->enable = $this->handler->$func($this->config['master']['host'], $this->config['master']['port']);
  27. $_cache = $this->handler;
  28. //$_cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
  29. }
  30. }
  31. private function init_slave(){
  32. static $_cache;
  33. if (isset($_cache)){
  34. $this->handler = $_cache;
  35. }else{
  36. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  37. $this->handler = new Redis;
  38. $this->enable = $this->handler->$func($this->config['slave']['host'], $this->config['slave']['port']);
  39. $_cache = $this->handler;
  40. //$_cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
  41. }
  42. }
  43. private function isConnected() {
  44. $this->init_master();
  45. return $this->enable;
  46. }
  47. public function get($key, $type = ''){
  48. $this->init_slave();
  49. if (!$this->enable) return false;
  50. $this->type = $type;
  51. $value = $this->handler->get($this->_key($key));
  52. return unserialize($value);
  53. }
  54. public function set($key, $value, $prefix = '', $expire = null) {
  55. $this->init_master();
  56. if (!$this->enable) return false;
  57. $this->type = $prefix;
  58. $value = serialize($value);
  59. if(is_int($expire)) {
  60. $result = $this->handler->setex($this->_key($key), $expire, $value);
  61. }else{
  62. $result = $this->handler->set($this->_key($key), $value);
  63. }
  64. return $result;
  65. }
  66. public function hset($name, $prefix, $data) {
  67. $this->init_master();
  68. if (!$this->enable || !is_array($data) || empty($data)) return false;
  69. $this->type = $prefix;
  70. foreach ($data as $key => $value) {
  71. if ($value[0] == 'exp') {
  72. $value[1] = str_replace(' ', '', $value[1]);
  73. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  74. if (is_numeric($matches[1])) {
  75. $this->hIncrBy($name, $prefix, $key, $matches[1]);
  76. }
  77. unset($data[$key]);
  78. }
  79. }
  80. if (count($data) == 1) {
  81. $this->handler->hset($this->_key($name), key($data),current($data));
  82. } elseif (count($data) > 1) {
  83. $this->handler->hMset($this->_key($name), $data);
  84. }
  85. }
  86. public function hget($name, $prefix, $key = null) {
  87. $this->init_slave();
  88. if (!$this->enable) return false;
  89. $this->type = $prefix;
  90. if ($key == '*' || is_null($key)) {
  91. return $this->handler->hGetAll($this->_key($name));
  92. } elseif (strpos($key,',') != false) {
  93. return $this->handler->hmGet($this->_key($name), explode(',',$key));
  94. } else {
  95. return $this->handler->hget($this->_key($name), $key);
  96. }
  97. }
  98. public function hdel($name, $prefix, $key = null) {
  99. $this->init_master();
  100. if (!$this->enable) return false;
  101. $this->type = $prefix;
  102. if (is_null($key)) {
  103. if (is_array($name)) {
  104. return $this->handler->delete(array_walk($array,array(self,'_key')));
  105. } else {
  106. return $this->handler->delete($this->_key($name));
  107. }
  108. } else {
  109. if (is_array($name)) {
  110. foreach ($name as $key => $value) {
  111. $this->handler->hdel($this->_key($name), $key);
  112. }
  113. return true;
  114. } else {
  115. return $this->handler->hdel($this->_key($name), $key);
  116. }
  117. }
  118. }
  119. public function hIncrBy($name, $prefix, $key, $num = 1) {
  120. if ($this->hget($name, $prefix,$key) !== false) {
  121. $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
  122. }
  123. }
  124. public function rm($key, $type = '') {
  125. $this->init_master();
  126. if (!$this->enable) return false;
  127. $this->type = $type;
  128. return $this->handler->delete($this->_key($key));
  129. }
  130. public function clear() {
  131. $this->init_master();
  132. if (!$this->enable) return false;
  133. return $this->handler->flushDB();
  134. }
  135. private function _key($str) {
  136. return $this->prefix.$this->type.$str;
  137. }
  138. public function keys($key = '') {
  139. $this->init_master();
  140. if(!isset($key) || empty($key)) {
  141. $key = '*';
  142. }
  143. return $this->handler->keys($this->_key($key));
  144. }
  145. public function del($key){
  146. $this->init_master();
  147. if (!$this->enable) return false;
  148. return $this->handler->delete($key);
  149. }
  150. }