feat: 同步下新环境
This commit is contained in:
170
Server/thinkphp/library/think/cache/driver/Redis.php
vendored
170
Server/thinkphp/library/think/cache/driver/Redis.php
vendored
@@ -31,121 +31,98 @@ class Redis extends Driver
|
||||
'expire' => 0,
|
||||
'persistent' => false,
|
||||
'prefix' => '',
|
||||
'serialize' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* 构造函数
|
||||
* @param array $options 缓存参数
|
||||
* @access public
|
||||
* @param array $options 缓存参数
|
||||
*/
|
||||
public function __construct($options = [])
|
||||
{
|
||||
if (!extension_loaded('redis')) {
|
||||
throw new \BadFunctionCallException('not support: redis');
|
||||
}
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if (extension_loaded('redis')) {
|
||||
$this->handler = new \Redis;
|
||||
|
||||
if ($this->options['persistent']) {
|
||||
$this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
|
||||
} else {
|
||||
$this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
|
||||
}
|
||||
|
||||
if ('' != $this->options['password']) {
|
||||
$this->handler->auth($this->options['password']);
|
||||
}
|
||||
|
||||
if (0 != $this->options['select']) {
|
||||
$this->handler->select($this->options['select']);
|
||||
}
|
||||
} elseif (class_exists('\Predis\Client')) {
|
||||
$params = [];
|
||||
foreach ($this->options as $key => $val) {
|
||||
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
|
||||
$params[$key] = $val;
|
||||
unset($this->options[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if ('' == $this->options['password']) {
|
||||
unset($this->options['password']);
|
||||
}
|
||||
|
||||
$this->handler = new \Predis\Client($this->options, $params);
|
||||
|
||||
$this->options['prefix'] = '';
|
||||
$this->handler = new \Redis;
|
||||
if ($this->options['persistent']) {
|
||||
$this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
|
||||
} else {
|
||||
throw new \BadFunctionCallException('not support: redis');
|
||||
$this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
|
||||
}
|
||||
|
||||
if ('' != $this->options['password']) {
|
||||
$this->handler->auth($this->options['password']);
|
||||
}
|
||||
|
||||
if (0 != $this->options['select']) {
|
||||
$this->handler->select($this->options['select']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->handler->exists($this->getCacheKey($name)) ? true : false;
|
||||
return (bool) $this->handler->exists($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$value = $this->handler->get($this->getCacheKey($name));
|
||||
|
||||
if (is_null($value) || false === $value) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $this->unserialize($value);
|
||||
try {
|
||||
$result = 0 === strpos($value, 'think_serialize:') ? unserialize(substr($value, 16)) : $value;
|
||||
} catch (\Exception $e) {
|
||||
$result = $default;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return boolean
|
||||
*/
|
||||
public function set($name, $value, $expire = null)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
if ($expire instanceof \DateTime) {
|
||||
$expire = $expire->getTimestamp() - time();
|
||||
}
|
||||
if ($this->tag && !$this->has($name)) {
|
||||
$first = true;
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = $this->getExpireTime($expire);
|
||||
|
||||
$value = $this->serialize($value);
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$value = is_scalar($value) ? $value : 'think_serialize:' . serialize($value);
|
||||
if ($expire) {
|
||||
$result = $this->handler->setex($key, $expire, $value);
|
||||
} else {
|
||||
$result = $this->handler->set($key, $value);
|
||||
}
|
||||
|
||||
isset($first) && $this->setTagItem($key);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -158,8 +135,6 @@ class Redis extends Driver
|
||||
*/
|
||||
public function inc($name, $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return $this->handler->incrby($key, $step);
|
||||
@@ -174,8 +149,6 @@ class Redis extends Driver
|
||||
*/
|
||||
public function dec($name, $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return $this->handler->decrby($key, $step);
|
||||
@@ -184,20 +157,18 @@ class Redis extends Driver
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param string $name 缓存变量名
|
||||
* @return boolean
|
||||
*/
|
||||
public function rm($name)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->handler->del($this->getCacheKey($name));
|
||||
return $this->handler->delete($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @param string $tag 标签名
|
||||
* @return boolean
|
||||
*/
|
||||
public function clear($tag = null)
|
||||
@@ -205,68 +176,13 @@ class Redis extends Driver
|
||||
if ($tag) {
|
||||
// 指定标签清除
|
||||
$keys = $this->getTagItem($tag);
|
||||
|
||||
$this->handler->del($keys);
|
||||
|
||||
$tagName = $this->getTagKey($tag);
|
||||
$this->handler->del($tagName);
|
||||
foreach ($keys as $key) {
|
||||
$this->handler->delete($key);
|
||||
}
|
||||
$this->rm('tag_' . md5($tag));
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->handler->flushDB();
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string $name 标签名
|
||||
* @param string|array $keys 缓存标识
|
||||
* @param bool $overlay 是否覆盖
|
||||
* @return $this
|
||||
*/
|
||||
public function tag($name, $keys = null, $overlay = false)
|
||||
{
|
||||
if (is_null($keys)) {
|
||||
$this->tag = $name;
|
||||
} else {
|
||||
$tagName = $this->getTagKey($name);
|
||||
if ($overlay) {
|
||||
$this->handler->del($tagName);
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$this->handler->sAdd($tagName, $key);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签
|
||||
* @access protected
|
||||
* @param string $name 缓存标识
|
||||
* @return void
|
||||
*/
|
||||
protected function setTagItem($name)
|
||||
{
|
||||
if ($this->tag) {
|
||||
$tagName = $this->getTagKey($this->tag);
|
||||
$this->handler->sAdd($tagName, $name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签包含的缓存标识
|
||||
* @access protected
|
||||
* @param string $tag 缓存标签
|
||||
* @return array
|
||||
*/
|
||||
protected function getTagItem($tag)
|
||||
{
|
||||
$tagName = $this->getTagKey($tag);
|
||||
return $this->handler->sMembers($tagName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user