cache.redis.php 6.0 KB

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