cache.redis.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, $prefix = ''){
  49. $this->init_slave();
  50. if (!$this->enable) return false;
  51. $this->type = $prefix;
  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 get_org($key,$prefix = '')
  76. {
  77. $this->init_master();
  78. if (!$this->enable) return false;
  79. $this->type = $prefix;
  80. $result = $this->handler->get($this->_key($key));
  81. return $result;
  82. }
  83. public function incr($key,$prefix = '')
  84. {
  85. $this->init_master();
  86. if (!$this->enable) return false;
  87. $this->type = $prefix;
  88. $key = $this->_key($key);
  89. $value = $this->handler->incr($key);
  90. return $value;
  91. }
  92. public function incrby($key,$val,$prefix = '')
  93. {
  94. $this->init_master();
  95. if (!$this->enable) return false;
  96. $this->type = $prefix;
  97. $key = $this->_key($key);
  98. $value = $this->handler->incrby($key,$val);
  99. return $value;
  100. }
  101. public function decr($key,$prefix = '')
  102. {
  103. $this->init_master();
  104. if (!$this->enable) return false;
  105. $this->type = $prefix;
  106. $key = $this->_key($key);
  107. $value = $this->handler->decr($key);
  108. return $value;
  109. }
  110. public function decrby($key,$val,$prefix = '')
  111. {
  112. $this->init_master();
  113. if (!$this->enable) return false;
  114. $this->type = $prefix;
  115. $key = $this->_key($key);
  116. $value = $this->handler->decrby($key,$val);
  117. return $value;
  118. }
  119. public function zIncrBy($key, $value, $member,$prefix='')
  120. {
  121. $this->init_master();
  122. if (!$this->enable) return false;
  123. $this->type = $prefix;
  124. $key = $this->_key($key);
  125. $result = $this->handler->zIncrBy($key,$value,$member);
  126. return $result;
  127. }
  128. public function zDelete($key, $member,$prefix='')
  129. {
  130. $this->init_master();
  131. if (!$this->enable) return false;
  132. $this->type = $prefix;
  133. $key = $this->_key($key);
  134. $result = $this->handler->zDelete($key,$member);
  135. return $result;
  136. }
  137. public function hset($name, $prefix, $data)
  138. {
  139. $this->init_master();
  140. if (!$this->enable || !is_array($data) || empty($data)) return false;
  141. $this->type = $prefix;
  142. foreach ($data as $key => $value) {
  143. if ($value[0] == 'exp') {
  144. $value[1] = str_replace(' ', '', $value[1]);
  145. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  146. if (is_numeric($matches[1])) {
  147. $this->hIncrByFloat($name, $prefix, $key, $matches[1]);
  148. }
  149. unset($data[$key]);
  150. }
  151. }
  152. if (count($data) == 1) {
  153. $this->handler->hset($this->_key($name), key($data),current($data));
  154. } elseif (count($data) > 1) {
  155. $this->handler->hMset($this->_key($name), $data);
  156. }
  157. }
  158. public function hget($name, $prefix, $key = null) {
  159. $this->init_slave();
  160. if (!$this->enable) return false;
  161. $this->type = $prefix;
  162. if ($key == '*' || is_null($key)) {
  163. return $this->handler->hGetAll($this->_key($name));
  164. } elseif (strpos($key,',') != false) {
  165. return $this->handler->hmGet($this->_key($name), explode(',',$key));
  166. } else {
  167. return $this->handler->hGet($this->_key($name), $key);
  168. }
  169. }
  170. public function hdel($name, $prefix, $key = null)
  171. {
  172. $this->init_master();
  173. if (!$this->enable) return false;
  174. $this->type = $prefix;
  175. if (is_null($key)) {
  176. if (is_array($name)) {
  177. return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
  178. } else {
  179. return $this->handler->del($this->_key($name));
  180. }
  181. } elseif (is_array($name)) {
  182. foreach ($name as $key => $value) {
  183. $this->handler->hdel($this->_key($name), $key);
  184. }
  185. return true;
  186. } else {
  187. return $this->handler->hdel($this->_key($name), $key);
  188. }
  189. }
  190. public function hIncrBy($name, $key, $value)
  191. {
  192. $this->init_master();
  193. if (!$this->enable) return false;
  194. $this->type = '';
  195. return $this->handler->hIncrBy($this->_key($name), $key, $value);
  196. }
  197. public function hIncrByFloat($name, $prefix, $key, $num = 1) {
  198. if ($this->hget($name, $prefix,$key) !== false) {
  199. $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
  200. }
  201. }
  202. public function rm($key, $type = '') {
  203. $this->init_master();
  204. if (!$this->enable) return false;
  205. $this->type = $type;
  206. return $this->handler->del($this->_key($key));
  207. }
  208. public function clear() {
  209. $this->init_master();
  210. if (!$this->enable) return false;
  211. return $this->handler->flushDB();
  212. }
  213. private function _key($str) {
  214. return $this->prefix.$this->type.$str;
  215. }
  216. public function keys($key = '') {
  217. $this->init_master();
  218. if(!isset($key) || empty($key)) {
  219. $key = '*';
  220. }
  221. return $this->handler->keys($this->_key($key));
  222. }
  223. public function del($key){
  224. $this->init_master();
  225. if (!$this->enable) return false;
  226. return $this->handler->del($key);
  227. }
  228. }