123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- * redis 操作
- */
- defined('InShopNC') or exit('Access Invalid!');
- class CacheRedis extends Cache
- {
- private $config;
- private $connected;
- private $type;
- private $prefix;
- public function __construct()
- {
- $this->config = C('redis');
- if (empty($this->config['slave'])) $this->config['slave'] = $this->config['master'];
- $this->prefix = $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
- if ( !extension_loaded('redis') ) {
- throw_exception('redis failed to load');
- }
- }
- private function init_master()
- {
- static $_cache;
- if (isset($_cache)){
- $this->handler = $_cache;
- }else{
- $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
- $this->handler = new Redis;
- $this->enable = $this->handler->$func($this->config['master']['host'], $this->config['master']['port']);
- $_cache = $this->handler;
- }
- }
- private function init_slave(){
- static $_cache;
- if (isset($_cache)){
- $this->handler = $_cache;
- }else{
- $func = $this->config['pconnect'] ? 'pconnect' : 'connect';
- $this->handler = new Redis;
- $this->enable = $this->handler->$func($this->config['slave']['host'], $this->config['slave']['port']);
- $_cache = $this->handler;
- }
- }
- private function isConnected() {
- $this->init_master();
- return $this->enable;
- }
- public function get($key, $prefix = ''){
- $this->init_slave();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $value = $this->handler->get($this->_key($key));
- return unserialize($value);
- }
- public function set($key, $value, $prefix = '', $expire = null) {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $value = serialize($value);
- if(is_int($expire)) {
- $result = $this->handler->setex($this->_key($key), $expire, $value);
- }else{
- $result = $this->handler->set($this->_key($key), $value);
- }
- return $result;
- }
- public function set_org($key,$val,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $result = $this->handler->set($this->_key($key), $val);
- return $result;
- }
- public function get_org($key,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $result = $this->handler->get($this->_key($key));
- return $result;
- }
- public function incr($key,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $value = $this->handler->incr($key);
- return $value;
- }
- public function incrby($key,$val,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $value = $this->handler->incrby($key,$val);
- return $value;
- }
- public function decr($key,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $value = $this->handler->decr($key);
- return $value;
- }
- public function decrby($key,$val,$prefix = '')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $value = $this->handler->decrby($key,$val);
- return $value;
- }
- public function zIncrBy($key, $value, $member,$prefix='')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $result = $this->handler->zIncrBy($key,$value,$member);
- return $result;
- }
- public function zDelete($key, $member,$prefix='')
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- $key = $this->_key($key);
- $result = $this->handler->zDelete($key,$member);
- return $result;
- }
- public function hset($name, $prefix, $data)
- {
- $this->init_master();
- if (!$this->enable || !is_array($data) || empty($data)) return false;
- $this->type = $prefix;
- foreach ($data as $key => $value) {
- if ($value[0] == 'exp') {
- $value[1] = str_replace(' ', '', $value[1]);
- preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
- if (is_numeric($matches[1])) {
- $this->hIncrByFloat($name, $prefix, $key, $matches[1]);
- }
- unset($data[$key]);
- }
- }
- if (count($data) == 1) {
- $this->handler->hset($this->_key($name), key($data),current($data));
- } elseif (count($data) > 1) {
- $this->handler->hMset($this->_key($name), $data);
- }
- }
- public function hget($name, $prefix, $key = null) {
- $this->init_slave();
- if (!$this->enable) return false;
- $this->type = $prefix;
- if ($key == '*' || is_null($key)) {
- return $this->handler->hGetAll($this->_key($name));
- } elseif (strpos($key,',') != false) {
- return $this->handler->hmGet($this->_key($name), explode(',',$key));
- } else {
- return $this->handler->hGet($this->_key($name), $key);
- }
- }
- public function hdel($name, $prefix, $key = null)
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $prefix;
- if (is_null($key)) {
- if (is_array($name)) {
- return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
- } else {
- return $this->handler->del($this->_key($name));
- }
- } elseif (is_array($name)) {
- foreach ($name as $key => $value) {
- $this->handler->hdel($this->_key($name), $key);
- }
- return true;
- } else {
- return $this->handler->hdel($this->_key($name), $key);
- }
- }
- public function hIncrBy($name, $key, $value)
- {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = '';
- return $this->handler->hIncrBy($this->_key($name), $key, $value);
- }
- public function hIncrByFloat($name, $prefix, $key, $num = 1) {
- if ($this->hget($name, $prefix,$key) !== false) {
- $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
- }
- }
- public function rm($key, $type = '') {
- $this->init_master();
- if (!$this->enable) return false;
- $this->type = $type;
- return $this->handler->del($this->_key($key));
- }
- public function clear() {
- $this->init_master();
- if (!$this->enable) return false;
- return $this->handler->flushDB();
- }
- private function _key($str) {
- return $this->prefix.$this->type.$str;
- }
- public function keys($key = '') {
- $this->init_master();
- if(!isset($key) || empty($key)) {
- $key = '*';
- }
- return $this->handler->keys($this->_key($key));
- }
- public function del($key){
- $this->init_master();
- if (!$this->enable) return false;
- return $this->handler->del($key);
- }
- }
|