cache.redis.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. {
  14. $this->config = C('redis');
  15. if (empty($this->config['slave'])) $this->config['slave'] = $this->config['master'];
  16. $this->prefix = $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
  17. if ( !extension_loaded('redis') ) {
  18. throw_exception('redis failed to load');
  19. }
  20. }
  21. private function init_master()
  22. {
  23. static $_cache;
  24. if (isset($_cache)){
  25. $this->handler = $_cache;
  26. }else{
  27. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  28. $this->handler = new Redis;
  29. $this->enable = $this->handler->$func($this->config['master']['host'], $this->config['master']['port']);
  30. $_cache = $this->handler;
  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. }
  43. }
  44. private function isConnected() {
  45. $this->init_master();
  46. return $this->enable;
  47. }
  48. public function get($key, $type = ''){
  49. $this->init_slave();
  50. if (!$this->enable) return false;
  51. $this->type = $type;
  52. $value = $this->handler->get($this->_key($key));
  53. return unserialize($value);
  54. }
  55. public function set($key, $value, $prefix = '', $expire = null) {
  56. $this->init_master();
  57. if (!$this->enable) return false;
  58. $this->type = $prefix;
  59. $value = serialize($value);
  60. if(is_int($expire)) {
  61. $result = $this->handler->setex($this->_key($key), $expire, $value);
  62. }else{
  63. $result = $this->handler->set($this->_key($key), $value);
  64. }
  65. return $result;
  66. }
  67. public function set_org($key,$val,$prefix = '')
  68. {
  69. $this->init_master();
  70. if (!$this->enable) return false;
  71. $this->type = $prefix;
  72. $result = $this->handler->set($this->_key($key), $val);
  73. return $result;
  74. }
  75. public function incr($key,$prefix = '')
  76. {
  77. $this->init_master();
  78. if (!$this->enable) return false;
  79. $this->type = $prefix;
  80. $key = $this->_key($key);
  81. $value = $this->handler->incr($key);
  82. return $value;
  83. }
  84. public function incrby($key,$val,$prefix = '') {
  85. }
  86. public function decr($key,$prefix)
  87. {
  88. }
  89. public function decrby($key,$val,$prefix = '')
  90. {
  91. }
  92. public function zIncrBy($key, $value, $member,$prefix='')
  93. {
  94. $this->init_master();
  95. if (!$this->enable) return false;
  96. $this->type = $prefix;
  97. $key = $this->_key($key);
  98. $result = $this->handler->zIncrBy($key,$value,$member);
  99. return $result;
  100. }
  101. public function zDelete($key, $member,$prefix='')
  102. {
  103. $this->init_master();
  104. if (!$this->enable) return false;
  105. $this->type = $prefix;
  106. $key = $this->_key($key);
  107. $result = $this->handler->zDelete($key,$member);
  108. return $result;
  109. }
  110. public function hset($name, $prefix, $data)
  111. {
  112. $this->init_master();
  113. if (!$this->enable || !is_array($data) || empty($data)) return false;
  114. $this->type = $prefix;
  115. foreach ($data as $key => $value) {
  116. if ($value[0] == 'exp') {
  117. $value[1] = str_replace(' ', '', $value[1]);
  118. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  119. if (is_numeric($matches[1])) {
  120. $this->hIncrBy($name, $prefix, $key, $matches[1]);
  121. }
  122. unset($data[$key]);
  123. }
  124. }
  125. if (count($data) == 1) {
  126. $this->handler->hset($this->_key($name), key($data),current($data));
  127. } elseif (count($data) > 1) {
  128. $this->handler->hMset($this->_key($name), $data);
  129. }
  130. }
  131. public function hget($name, $prefix, $key = null) {
  132. $this->init_slave();
  133. if (!$this->enable) return false;
  134. $this->type = $prefix;
  135. if ($key == '*' || is_null($key)) {
  136. return $this->handler->hGetAll($this->_key($name));
  137. } elseif (strpos($key,',') != false) {
  138. return $this->handler->hmGet($this->_key($name), explode(',',$key));
  139. } else {
  140. return $this->handler->hget($this->_key($name), $key);
  141. }
  142. }
  143. public function hdel($name, $prefix, $key = null)
  144. {
  145. $this->init_master();
  146. if (!$this->enable) return false;
  147. $this->type = $prefix;
  148. if (is_null($key)) {
  149. if (is_array($name)) {
  150. return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
  151. } else {
  152. return $this->handler->del($this->_key($name));
  153. }
  154. } elseif (is_array($name)) {
  155. foreach ($name as $key => $value) {
  156. $this->handler->hdel($this->_key($name), $key);
  157. }
  158. return true;
  159. } else {
  160. return $this->handler->hdel($this->_key($name), $key);
  161. }
  162. }
  163. public function hIncrBy($name, $prefix, $key, $num = 1) {
  164. if ($this->hget($name, $prefix,$key) !== false) {
  165. $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
  166. }
  167. }
  168. public function rm($key, $type = '') {
  169. $this->init_master();
  170. if (!$this->enable) return false;
  171. $this->type = $type;
  172. return $this->handler->del($this->_key($key));
  173. }
  174. public function clear() {
  175. $this->init_master();
  176. if (!$this->enable) return false;
  177. return $this->handler->flushDB();
  178. }
  179. private function _key($str) {
  180. return $this->prefix.$this->type.$str;
  181. }
  182. public function keys($key = '') {
  183. $this->init_master();
  184. if(!isset($key) || empty($key)) {
  185. $key = '*';
  186. }
  187. return $this->handler->keys($this->_key($key));
  188. }
  189. public function del($key){
  190. $this->init_master();
  191. if (!$this->enable) return false;
  192. return $this->handler->del($key);
  193. }
  194. }