<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\cache\driver;
use think\cache\Driver;
/**
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
*
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
* @author 尘缘 <130775@qq.com>
*/
class Redis extends Driver
{
protected $handler;
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => false,
'prefix' => '',
'tag_prefix' => 'tag:',
'serialize' => [],
];
public function __construct(array $options = [])
{
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'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']);
} else {
$this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
}
if ('' != $this->options['password']) {
$this->handler->auth($this->options['password']);
}
} 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'] = '';
} else {
throw new \BadFunctionCallException('not support: redis');
}
if (0 != $this->options['select']) {
$this->handler->select((int) $this->options['select']);
}
}
public function has($name): bool
{
return $this->handler->exists($this->getCacheKey($name)) ? true : false;
}
public function get($name, $default = null)
{
$this->readTimes++;
$key = $this->getCacheKey($name);
$value = $this->handler->get($key);
if (false === $value || is_null($value)) {
return $default;
}
return $this->unserialize($value);
}
public function set($name, $value, $expire = null): bool
{
$this->writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$key = $this->getCacheKey($name);
$expire = $this->getExpireTime($expire);
$value = $this->serialize($value);
if ($expire) {
$this->handler->setex($key, $expire, $value);
} else {
$this->handler->set($key, $value);
}
return true;
}
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->incrby($key, $step);
}
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->decrby($key, $step);
}
public function delete($name): bool
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
$result = $this->handler->del($key);
return $result > 0;
}
public function clear(): bool
{
$this->writeTimes++;
$this->handler->flushDB();
return true;
}
public function clearTag(array $keys): void
{
$this->handler->del($keys);
}
public function append(string $name, $value): void
{
$key = $this->getCacheKey($name);
$this->handler->sAdd($key, $value);
}
public function getTagItems(string $tag): array
{
$name = $this->getTagKey($tag);
$key = $this->getCacheKey($name);
return $this->handler->sMembers($key);
}
}