cache.redis.php 8.1 KB

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