|
@@ -8,7 +8,6 @@ defined('InShopNC') or exit('Access Invalid!');
|
|
|
class Cacheredis extends Cache
|
|
|
{
|
|
|
private $config;
|
|
|
- private $type;
|
|
|
private $prefix;
|
|
|
|
|
|
private $master;
|
|
@@ -18,7 +17,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$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).'_';
|
|
|
+ $this->prefix = $this->config['prefix'] ?? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
|
|
|
+
|
|
|
if ( !extension_loaded('redis') ) {
|
|
|
throw_exception('redis failed to load');
|
|
|
}
|
|
@@ -85,6 +85,7 @@ class Cacheredis extends Cache
|
|
|
Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if (is_mobile()) {
|
|
|
$func = 'connect';
|
|
|
} else {
|
|
@@ -113,6 +114,7 @@ class Cacheredis extends Cache
|
|
|
Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if (is_mobile()) {
|
|
|
$func = 'connect';
|
|
|
} else {
|
|
@@ -150,12 +152,12 @@ class Cacheredis extends Cache
|
|
|
$this->handler = $this->slave;
|
|
|
}
|
|
|
|
|
|
+ ///redis function
|
|
|
public function get($key, $prefix = '')
|
|
|
{
|
|
|
$this->init_slave();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $value = $this->handler->get($this->_key($key));
|
|
|
+ $value = $this->handler->get($this->_key($key, $prefix));
|
|
|
|
|
|
return unserialize($value);
|
|
|
}
|
|
@@ -164,14 +166,14 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$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);
|
|
|
+ if (is_int($expire)) {
|
|
|
+ $result = $this->handler->setex($this->_key($key, $prefix), $expire, $value);
|
|
|
+ } else {
|
|
|
+ $result = $this->handler->set($this->_key($key, $prefix), $value);
|
|
|
}
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
@@ -179,8 +181,7 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $result = $this->handler->set($this->_key($key), $val);
|
|
|
+ $result = $this->handler->set($this->_key($key,$prefix), $val);
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
@@ -189,9 +190,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_slave();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $result = $this->handler->get($this->_key($key));
|
|
|
|
|
|
+ $result = $this->handler->get($this->_key($key,$prefix));
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
@@ -199,8 +199,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key, $prefix);
|
|
|
$value = $this->handler->incr($key);
|
|
|
return $value;
|
|
|
}
|
|
@@ -209,8 +209,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key, $prefix);
|
|
|
$value = $this->handler->incrby($key,$val);
|
|
|
return $value;
|
|
|
}
|
|
@@ -219,8 +219,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key,$prefix);
|
|
|
$value = $this->handler->decr($key);
|
|
|
return $value;
|
|
|
}
|
|
@@ -229,8 +229,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key,$prefix);
|
|
|
$value = $this->handler->decrby($key,$val);
|
|
|
return $value;
|
|
|
}
|
|
@@ -239,8 +239,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key,$prefix);
|
|
|
$result = $this->handler->zIncrBy($key,$value,$member);
|
|
|
return $result;
|
|
|
}
|
|
@@ -249,8 +249,8 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
- $key = $this->_key($key);
|
|
|
+
|
|
|
+ $key = $this->_key($key,$prefix);
|
|
|
$result = $this->handler->zDelete($key,$member);
|
|
|
return $result;
|
|
|
}
|
|
@@ -259,10 +259,11 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$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') {
|
|
|
+ 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])) {
|
|
@@ -271,23 +272,25 @@ class Cacheredis extends Cache
|
|
|
unset($data[$key]);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if (count($data) == 1) {
|
|
|
- $this->handler->hset($this->_key($name), key($data),current($data));
|
|
|
+ $this->handler->hset($this->_key($name,$prefix), key($data),current($data));
|
|
|
} elseif (count($data) > 1) {
|
|
|
- $this->handler->hMset($this->_key($name), $data);
|
|
|
+ $this->handler->hMset($this->_key($name,$prefix), $data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function hget($name, $prefix, $key = null) {
|
|
|
+ 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));
|
|
|
+ return $this->handler->hGetAll($this->_key($name,$prefix));
|
|
|
} elseif (strpos($key,',') != false) {
|
|
|
- return $this->handler->hmGet($this->_key($name), explode(',',$key));
|
|
|
+ return $this->handler->hmGet($this->_key($name,$prefix), explode(',',$key));
|
|
|
} else {
|
|
|
- return $this->handler->hGet($this->_key($name), $key);
|
|
|
+ return $this->handler->hGet($this->_key($name,$prefix), $key);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -295,44 +298,45 @@ class Cacheredis extends Cache
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $prefix;
|
|
|
+
|
|
|
if (is_null($key)) {
|
|
|
if (is_array($name)) {
|
|
|
+ $array = [];
|
|
|
return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
|
|
|
} else {
|
|
|
- return $this->handler->del($this->_key($name));
|
|
|
+ return $this->handler->del($this->_key($name,$prefix));
|
|
|
}
|
|
|
} elseif (is_array($name)) {
|
|
|
foreach ($name as $key => $value) {
|
|
|
- $this->handler->hdel($this->_key($name), $key);
|
|
|
+ $this->handler->hdel($this->_key($name,$prefix), $key);
|
|
|
}
|
|
|
return true;
|
|
|
} else {
|
|
|
- return $this->handler->hdel($this->_key($name), $key);
|
|
|
+ return $this->handler->hdel($this->_key($name,$prefix), $key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function hIncrBy($name, $key, $value)
|
|
|
+ public function hIncrBy($name, $prefix, $key, $value)
|
|
|
{
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
|
|
|
- $this->type = '';
|
|
|
- return $this->handler->hIncrBy($this->_key($name), $key, $value);
|
|
|
+ return $this->handler->hIncrBy($this->_key($name, $prefix), $key, $value);
|
|
|
}
|
|
|
|
|
|
- public function hIncrByFloat($name, $prefix, $key, $num = 1) {
|
|
|
+ public function hIncrByFloat($name, $prefix, $key, $value)
|
|
|
+ {
|
|
|
$this->init_master();
|
|
|
- if ($this->hget($name, $prefix,$key) !== false) {
|
|
|
- $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
|
|
|
- }
|
|
|
+ if (!$this->enable) return false;
|
|
|
+
|
|
|
+ return $this->handler->hIncrByFloat($this->_key($name, $prefix), $key, $value);
|
|
|
}
|
|
|
|
|
|
- public function rm($key, $type = '') {
|
|
|
+ public function rm($key, $prefix = '') {
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
- $this->type = $type;
|
|
|
- return $this->handler->del($this->_key($key));
|
|
|
+
|
|
|
+ return $this->handler->del($this->_key($key, $prefix));
|
|
|
}
|
|
|
|
|
|
public function clear() {
|
|
@@ -341,28 +345,31 @@ class Cacheredis extends Cache
|
|
|
return $this->handler->flushDB();
|
|
|
}
|
|
|
|
|
|
- private function _key($str) {
|
|
|
- return $this->prefix.$this->type.$str;
|
|
|
+ private function _key($str,$type) {
|
|
|
+ return $this->prefix.$type.$str;
|
|
|
}
|
|
|
|
|
|
- public function keys($key = '')
|
|
|
+ public function keys($key = '',$prefix = '')
|
|
|
{
|
|
|
$this->init_slave();
|
|
|
if(!isset($key) || empty($key)) {
|
|
|
$key = '*';
|
|
|
}
|
|
|
- return $this->handler->keys($this->_key($key));
|
|
|
+ return $this->handler->keys($this->_key($key, $prefix));
|
|
|
}
|
|
|
|
|
|
- public function del($key){
|
|
|
+ public function del($key)
|
|
|
+ {
|
|
|
$this->init_master();
|
|
|
if (!$this->enable) return false;
|
|
|
return $this->handler->del($key);
|
|
|
}
|
|
|
|
|
|
- public function lLen($key) {
|
|
|
+ public function lLen($key)
|
|
|
+ {
|
|
|
$this->init_slave();
|
|
|
if (!$this->enable) return false;
|
|
|
+
|
|
|
return $this->handler->lLen($key);
|
|
|
}
|
|
|
|