cache.cacheredis.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $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. static $_cache;
  22. if (isset($_cache)){
  23. $this->handler = $_cache;
  24. }else{
  25. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  26. $this->handler = new Redis;
  27. $this->enable = $this->handler->$func($this->config['master']['host'], $this->config['master']['port']);
  28. $_cache = $this->handler;
  29. //$_cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
  30. }
  31. }
  32. private function init_slave(){
  33. static $_cache;
  34. if (isset($_cache)){
  35. $this->handler = $_cache;
  36. }else{
  37. $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
  38. $this->handler = new Redis;
  39. $this->enable = $this->handler->$func($this->config['slave']['host'], $this->config['slave']['port']);
  40. $_cache = $this->handler;
  41. //$_cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
  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 hset($name, $prefix, $data) {
  68. $this->init_master();
  69. if (!$this->enable || !is_array($data) || empty($data)) return false;
  70. $this->type = $prefix;
  71. foreach ($data as $key => $value) {
  72. if ($value[0] == 'exp') {
  73. $value[1] = str_replace(' ', '', $value[1]);
  74. preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
  75. if (is_numeric($matches[1])) {
  76. $this->hIncrBy($name, $prefix, $key, $matches[1]);
  77. }
  78. unset($data[$key]);
  79. }
  80. }
  81. if (count($data) == 1) {
  82. $this->handler->hset($this->_key($name), key($data),current($data));
  83. } elseif (count($data) > 1) {
  84. $this->handler->hMset($this->_key($name), $data);
  85. }
  86. }
  87. public function hget($name, $prefix, $key = null) {
  88. $this->init_slave();
  89. if (!$this->enable) return false;
  90. $this->type = $prefix;
  91. if ($key == '*' || is_null($key)) {
  92. return $this->handler->hGetAll($this->_key($name));
  93. } elseif (strpos($key,',') != false) {
  94. return $this->handler->hmGet($this->_key($name), explode(',',$key));
  95. } else {
  96. return $this->handler->hget($this->_key($name), $key);
  97. }
  98. }
  99. public function hdel($name, $prefix, $key = null) {
  100. $this->init_master();
  101. if (!$this->enable) return false;
  102. $this->type = $prefix;
  103. if (is_null($key)) {
  104. if (is_array($name)) {
  105. return $this->handler->delete(array_walk($array,array(self,'_key')));
  106. } else {
  107. return $this->handler->delete($this->_key($name));
  108. }
  109. } else {
  110. if (is_array($name)) {
  111. foreach ($name as $key => $value) {
  112. $this->handler->hdel($this->_key($name), $key);
  113. }
  114. return true;
  115. } else {
  116. return $this->handler->hdel($this->_key($name), $key);
  117. }
  118. }
  119. }
  120. public function hIncrBy($name, $prefix, $key, $num = 1) {
  121. if ($this->hget($name, $prefix,$key) !== false) {
  122. $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
  123. }
  124. }
  125. public function rm($key, $type = '') {
  126. $this->init_master();
  127. if (!$this->enable) return false;
  128. $this->type = $type;
  129. return $this->handler->delete($this->_key($key));
  130. }
  131. public function clear() {
  132. $this->init_master();
  133. if (!$this->enable) return false;
  134. return $this->handler->flushDB();
  135. }
  136. private function _key($str) {
  137. return $this->prefix.$this->type.$str;
  138. }
  139. }