cache.redis.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * redis 操作
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class CacheRedis extends Cache
  7. {
  8. private $config;
  9. private $type;
  10. private $prefix;
  11. public function __construct()
  12. {
  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. {
  25. if($_cache->isConnected()) {
  26. $this->handler = $_cache;
  27. return;
  28. } else {
  29. $_cache->close();
  30. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  31. }
  32. }
  33. if (is_mobile()) {
  34. $func = 'connect';
  35. } else {
  36. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  37. }
  38. $this->handler = new Redis();
  39. $this->enable = $this->handler->$func($this->config['master']['host'], $this->config['master']['port']);
  40. $_cache = $this->handler;
  41. }
  42. private function init_slave()
  43. {
  44. static $_cache;
  45. if (isset($_cache))
  46. {
  47. if($_cache->isConnected()) {
  48. $this->handler = $_cache;
  49. return;
  50. } else {
  51. $_cache->close();
  52. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  53. }
  54. }
  55. if (is_mobile()) {
  56. $func = 'connect';
  57. } else {
  58. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  59. }
  60. $this->handler = new Redis();
  61. $this->enable = $this->handler->$func($this->config['slave']['host'], $this->config['slave']['port']);
  62. $_cache = $this->handler;
  63. }
  64. public function get($key, $prefix = '')
  65. {
  66. $this->init_slave();
  67. if (!$this->enable) return false;
  68. $this->type = $prefix;
  69. $value = $this->handler->get($this->_key($key));
  70. return unserialize($value);
  71. }
  72. public function set($key, $value, $prefix = '', $expire = null)
  73. {
  74. $this->init_master();
  75. if (!$this->enable) return false;
  76. $this->type = $prefix;
  77. $value = serialize($value);
  78. if(is_int($expire)) {
  79. $result = $this->handler->setex($this->_key($key), $expire, $value);
  80. }else{
  81. $result = $this->handler->set($this->_key($key), $value);
  82. }
  83. return $result;
  84. }
  85. public function set_org($key,$val,$prefix = '')
  86. {
  87. $this->init_master();
  88. if (!$this->enable) return false;
  89. $this->type = $prefix;
  90. $result = $this->handler->set($this->_key($key), $val);
  91. return $result;
  92. }
  93. public function get_org($key,$prefix = '')
  94. {
  95. $this->init_master();
  96. if (!$this->enable) return false;
  97. $this->type = $prefix;
  98. $result = $this->handler->get($this->_key($key));
  99. return $result;
  100. }
  101. public function incr($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->incr($key);
  108. return $value;
  109. }
  110. public function incrby($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->incrby($key,$val);
  117. return $value;
  118. }
  119. public function decr($key,$prefix = '')
  120. {
  121. $this->init_master();
  122. if (!$this->enable) return false;
  123. $this->type = $prefix;
  124. $key = $this->_key($key);
  125. $value = $this->handler->decr($key);
  126. return $value;
  127. }
  128. public function decrby($key,$val,$prefix = '')
  129. {
  130. $this->init_master();
  131. if (!$this->enable) return false;
  132. $this->type = $prefix;
  133. $key = $this->_key($key);
  134. $value = $this->handler->decrby($key,$val);
  135. return $value;
  136. }
  137. public function zIncrBy($key, $value, $member,$prefix='')
  138. {
  139. $this->init_master();
  140. if (!$this->enable) return false;
  141. $this->type = $prefix;
  142. $key = $this->_key($key);
  143. $result = $this->handler->zIncrBy($key,$value,$member);
  144. return $result;
  145. }
  146. public function zDelete($key, $member,$prefix='')
  147. {
  148. $this->init_master();
  149. if (!$this->enable) return false;
  150. $this->type = $prefix;
  151. $key = $this->_key($key);
  152. $result = $this->handler->zDelete($key,$member);
  153. return $result;
  154. }
  155. public function hset($name, $prefix, $data)
  156. {
  157. $this->init_master();
  158. if (!$this->enable || !is_array($data) || empty($data)) return false;
  159. $this->type = $prefix;
  160. foreach ($data as $key => $value) {
  161. if ($value[0] == 'exp') {
  162. $value[1] = str_replace(' ', '', $value[1]);
  163. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  164. if (is_numeric($matches[1])) {
  165. $this->hIncrByFloat($name, $prefix, $key, $matches[1]);
  166. }
  167. unset($data[$key]);
  168. }
  169. }
  170. if (count($data) == 1) {
  171. $this->handler->hset($this->_key($name), key($data),current($data));
  172. } elseif (count($data) > 1) {
  173. $this->handler->hMset($this->_key($name), $data);
  174. }
  175. }
  176. public function hget($name, $prefix, $key = null) {
  177. $this->init_slave();
  178. if (!$this->enable) return false;
  179. $this->type = $prefix;
  180. if ($key == '*' || is_null($key)) {
  181. return $this->handler->hGetAll($this->_key($name));
  182. } elseif (strpos($key,',') != false) {
  183. return $this->handler->hmGet($this->_key($name), explode(',',$key));
  184. } else {
  185. return $this->handler->hGet($this->_key($name), $key);
  186. }
  187. }
  188. public function hdel($name, $prefix, $key = null)
  189. {
  190. $this->init_master();
  191. if (!$this->enable) return false;
  192. $this->type = $prefix;
  193. if (is_null($key)) {
  194. if (is_array($name)) {
  195. return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
  196. } else {
  197. return $this->handler->del($this->_key($name));
  198. }
  199. } elseif (is_array($name)) {
  200. foreach ($name as $key => $value) {
  201. $this->handler->hdel($this->_key($name), $key);
  202. }
  203. return true;
  204. } else {
  205. return $this->handler->hdel($this->_key($name), $key);
  206. }
  207. }
  208. public function hIncrBy($name, $key, $value)
  209. {
  210. $this->init_master();
  211. if (!$this->enable) return false;
  212. $this->type = '';
  213. return $this->handler->hIncrBy($this->_key($name), $key, $value);
  214. }
  215. public function hIncrByFloat($name, $prefix, $key, $num = 1) {
  216. if ($this->hget($name, $prefix,$key) !== false) {
  217. $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
  218. }
  219. }
  220. public function rm($key, $type = '') {
  221. $this->init_master();
  222. if (!$this->enable) return false;
  223. $this->type = $type;
  224. return $this->handler->del($this->_key($key));
  225. }
  226. public function clear() {
  227. $this->init_master();
  228. if (!$this->enable) return false;
  229. return $this->handler->flushDB();
  230. }
  231. private function _key($str) {
  232. return $this->prefix.$this->type.$str;
  233. }
  234. public function keys($key = '') {
  235. $this->init_master();
  236. if(!isset($key) || empty($key)) {
  237. $key = '*';
  238. }
  239. return $this->handler->keys($this->_key($key));
  240. }
  241. public function del($key){
  242. $this->init_master();
  243. if (!$this->enable) return false;
  244. return $this->handler->del($key);
  245. }
  246. public function lLen($key) {
  247. $this->init_master();
  248. if (!$this->enable) return false;
  249. return $this->handler->lLen($key);
  250. }
  251. }