cache.redis.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * redis 操作
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class Cacheredis extends Cache
  7. {
  8. private $config;
  9. private $prefix;
  10. private $master;
  11. private $slave;
  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. $this->master = null;
  21. $this->slave = null;
  22. }
  23. public function __destruct()
  24. {
  25. if(defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP)
  26. {
  27. if(!CoRedisPool::instance()->stoped())
  28. {
  29. $other = co_create_instance('Cache','connect',['cacheredis']);
  30. $other->attach($this->master,$this->slave);
  31. $this->master = null;
  32. $this->slave = null;
  33. CoRedisPool::instance()->put($other);
  34. }
  35. }
  36. }
  37. private function attach($master,$slave)
  38. {
  39. $this->master = $master;
  40. $this->slave = $slave;
  41. }
  42. private function init_master()
  43. {
  44. if(defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP)
  45. {
  46. if (!is_null($this->master))
  47. {
  48. if($this->master->isConnected()) {
  49. $this->handler = $this->master;
  50. $this->enable = true;
  51. return;
  52. } else {
  53. $this->master->close();
  54. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  55. }
  56. }
  57. if (is_mobile()) {
  58. $func = 'connect';
  59. } else {
  60. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  61. }
  62. $this->master = new Redis();
  63. $this->enable = $this->master->$func($this->config['master']['host'], $this->config['master']['port']);
  64. }
  65. else
  66. {
  67. static $_cache;
  68. if (isset($_cache))
  69. {
  70. if($_cache->isConnected()) {
  71. $this->master = $_cache;
  72. $this->handler = $this->master;
  73. $this->enable = true;
  74. return;
  75. } else {
  76. $_cache->close();
  77. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  78. }
  79. }
  80. if (is_mobile()) {
  81. $func = 'connect';
  82. } else {
  83. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  84. }
  85. $this->master = new Redis();
  86. $this->enable = $this->master->$func($this->config['master']['host'], $this->config['master']['port']);
  87. $_cache = $this->master;
  88. }
  89. $this->handler = $this->master;
  90. }
  91. private function init_slave()
  92. {
  93. if(defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP)
  94. {
  95. if (!is_null($this->slave))
  96. {
  97. if($this->slave->isConnected()) {
  98. $this->handler = $this->slave;
  99. $this->enable = true;
  100. return;
  101. } else {
  102. $this->slave->close();
  103. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  104. }
  105. }
  106. if (is_mobile()) {
  107. $func = 'connect';
  108. } else {
  109. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  110. }
  111. $this->slave = new Redis();
  112. $this->enable = $this->slave->$func($this->config['slave']['host'], $this->config['slave']['port']);
  113. }
  114. else
  115. {
  116. static $_cache;
  117. if (isset($_cache))
  118. {
  119. if($_cache->isConnected()) {
  120. $this->slave = $_cache;
  121. $this->enable = true;
  122. return;
  123. } else {
  124. $_cache->close();
  125. Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
  126. }
  127. }
  128. if (is_mobile()) {
  129. $func = 'connect';
  130. } else {
  131. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  132. }
  133. $this->slave = new Redis();
  134. $this->enable = $this->slave->$func($this->config['slave']['host'], $this->config['slave']['port']);
  135. $_cache = $this->slave;
  136. }
  137. $this->handler = $this->slave;
  138. }
  139. ///redis function
  140. public function get($key, $prefix = '')
  141. {
  142. $this->init_slave();
  143. if (!$this->enable) return false;
  144. $value = $this->handler->get($this->_key($key, $prefix));
  145. return unserialize($value);
  146. }
  147. public function set($key, $value, $prefix = '', $expire = null)
  148. {
  149. $this->init_master();
  150. if (!$this->enable) return false;
  151. $value = serialize($value);
  152. if (is_int($expire)) {
  153. $result = $this->handler->setex($this->_key($key, $prefix), $expire, $value);
  154. } else {
  155. $result = $this->handler->set($this->_key($key, $prefix), $value);
  156. }
  157. return $result;
  158. }
  159. public function set_org($key,$val,$prefix = '')
  160. {
  161. $this->init_master();
  162. if (!$this->enable) return false;
  163. $result = $this->handler->set($this->_key($key,$prefix), $val);
  164. return $result;
  165. }
  166. public function get_org($key,$prefix = '')
  167. {
  168. $this->init_slave();
  169. if (!$this->enable) return false;
  170. $result = $this->handler->get($this->_key($key,$prefix));
  171. return $result;
  172. }
  173. public function incr($key,$prefix = '')
  174. {
  175. $this->init_master();
  176. if (!$this->enable) return false;
  177. $key = $this->_key($key, $prefix);
  178. $value = $this->handler->incr($key);
  179. return $value;
  180. }
  181. public function incrby($key,$val,$prefix = '')
  182. {
  183. $this->init_master();
  184. if (!$this->enable) return false;
  185. $key = $this->_key($key, $prefix);
  186. $value = $this->handler->incrby($key,$val);
  187. return $value;
  188. }
  189. public function decr($key,$prefix = '')
  190. {
  191. $this->init_master();
  192. if (!$this->enable) return false;
  193. $key = $this->_key($key,$prefix);
  194. $value = $this->handler->decr($key);
  195. return $value;
  196. }
  197. public function decrby($key,$val,$prefix = '')
  198. {
  199. $this->init_master();
  200. if (!$this->enable) return false;
  201. $key = $this->_key($key,$prefix);
  202. $value = $this->handler->decrby($key,$val);
  203. return $value;
  204. }
  205. public function zIncrBy($key, $value, $member,$prefix='')
  206. {
  207. $this->init_master();
  208. if (!$this->enable) return false;
  209. $key = $this->_key($key,$prefix);
  210. $result = $this->handler->zIncrBy($key,$value,$member);
  211. return $result;
  212. }
  213. public function zDelete($key, $member,$prefix='')
  214. {
  215. $this->init_master();
  216. if (!$this->enable) return false;
  217. $key = $this->_key($key,$prefix);
  218. $result = $this->handler->zDelete($key,$member);
  219. return $result;
  220. }
  221. public function hset($name, $prefix, $data)
  222. {
  223. $this->init_master();
  224. if (!$this->enable || !is_array($data) || empty($data)) return false;
  225. foreach ($data as $key => $value)
  226. {
  227. if ($value[0] == 'exp')
  228. {
  229. $value[1] = str_replace(' ', '', $value[1]);
  230. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  231. if (is_numeric($matches[1])) {
  232. $this->hIncrByFloat($name, $prefix, $key, $matches[1]);
  233. }
  234. unset($data[$key]);
  235. }
  236. }
  237. if (count($data) == 1) {
  238. $this->handler->hset($this->_key($name,$prefix), key($data),current($data));
  239. } elseif (count($data) > 1) {
  240. $this->handler->hMset($this->_key($name,$prefix), $data);
  241. }
  242. }
  243. public function hget($name, $prefix, $key = null)
  244. {
  245. $this->init_slave();
  246. if (!$this->enable) return false;
  247. if ($key == '*' || is_null($key)) {
  248. return $this->handler->hGetAll($this->_key($name,$prefix));
  249. } elseif (strpos($key,',') != false) {
  250. return $this->handler->hmGet($this->_key($name,$prefix), explode(',',$key));
  251. } else {
  252. return $this->handler->hGet($this->_key($name,$prefix), $key);
  253. }
  254. }
  255. public function hdel($name, $prefix, $key = null)
  256. {
  257. $this->init_master();
  258. if (!$this->enable) return false;
  259. if (is_null($key)) {
  260. if (is_array($name)) {
  261. $array = [];
  262. return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
  263. } else {
  264. return $this->handler->del($this->_key($name,$prefix));
  265. }
  266. } elseif (is_array($name)) {
  267. foreach ($name as $key => $value) {
  268. $this->handler->hdel($this->_key($name,$prefix), $key);
  269. }
  270. return true;
  271. } else {
  272. return $this->handler->hdel($this->_key($name,$prefix), $key);
  273. }
  274. }
  275. public function hIncrBy($name, $key, $value)
  276. {
  277. $this->init_master();
  278. if (!$this->enable) return false;
  279. $prefix = '';
  280. return $this->handler->hIncrBy($this->_key($name, $prefix), $key, $value);
  281. }
  282. public function hIncrByFloat($name, $prefix, $key, $value)
  283. {
  284. $this->init_master();
  285. if (!$this->enable) return false;
  286. return $this->handler->hIncrByFloat($this->_key($name, $prefix), $key, $value);
  287. }
  288. public function rm($key, $prefix = '') {
  289. $this->init_master();
  290. if (!$this->enable) return false;
  291. return $this->handler->del($this->_key($key, $prefix));
  292. }
  293. public function clear() {
  294. $this->init_master();
  295. if (!$this->enable) return false;
  296. return $this->handler->flushDB();
  297. }
  298. private function _key($str,$type) {
  299. return $this->prefix.$type.$str;
  300. }
  301. public function keys($key = '',$prefix = '')
  302. {
  303. $this->init_slave();
  304. if(!isset($key) || empty($key)) {
  305. $key = '*';
  306. }
  307. return $this->handler->keys($this->_key($key, $prefix));
  308. }
  309. public function hkeys($key = '',$prefix = '')
  310. {
  311. $this->init_slave();
  312. if(!isset($key) || empty($key)) {
  313. $key = '*';
  314. }
  315. return $this->handler->hkeys($this->_key($key, $prefix));
  316. }
  317. public function del($key)
  318. {
  319. $this->init_master();
  320. if (!$this->enable) return false;
  321. return $this->handler->del($key);
  322. }
  323. public function lLen($key)
  324. {
  325. $this->init_slave();
  326. if (!$this->enable) return false;
  327. return $this->handler->lLen($key);
  328. }
  329. public function lpush($queue_name,$value)
  330. {
  331. $this->init_master();
  332. if (!$this->enable) return false;
  333. return $this->handler->lPush($queue_name, $value);
  334. }
  335. }