初始化代码

This commit is contained in:
2025-12-22 14:34:25 +08:00
parent c2c5ae2fdd
commit a77dbc743f
1510 changed files with 213008 additions and 0 deletions

View File

@@ -0,0 +1,347 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache;
use Closure;
use DateInterval;
use DateTime;
use DateTimeInterface;
use Exception;
use Psr\SimpleCache\CacheInterface;
use think\Container;
use think\contract\CacheHandlerInterface;
use think\exception\InvalidArgumentException;
use throwable;
/**
* 缓存基础类
*/
abstract class Driver implements CacheInterface, CacheHandlerInterface
{
/**
* 驱动句柄
* @var object
*/
protected $handler = null;
/**
* 缓存读取次数
* @var integer
*/
protected $readTimes = 0;
/**
* 缓存写入次数
* @var integer
*/
protected $writeTimes = 0;
/**
* 缓存参数
* @var array
*/
protected $options = [];
/**
* 缓存标签
* @var array
*/
protected $tag = [];
/**
* 获取有效期
* @access protected
* @param integer|DateTimeInterface|DateInterval $expire 有效期
* @return int
*/
protected function getExpireTime($expire): int
{
if ($expire instanceof DateTimeInterface) {
$expire = $expire->getTimestamp() - time();
} elseif ($expire instanceof DateInterval) {
$expire = DateTime::createFromFormat('U', (string) time())
->add($expire)
->format('U') - time();
}
return (int) $expire;
}
/**
* 获取实际的缓存标识
* @access public
* @param string $name 缓存名
* @return string
*/
public function getCacheKey(string $name): string
{
return $this->options['prefix'] . $name;
}
/**
* 读取缓存并删除
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function pull(string $name)
{
$result = $this->get($name, false);
if ($result) {
$this->delete($name);
return $result;
}
}
/**
* 追加(数组)缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @return void
*/
public function push(string $name, $value): void
{
$item = $this->get($name, []);
if (!is_array($item)) {
throw new InvalidArgumentException('only array cache can be push');
}
$item[] = $value;
if (count($item) > 1000) {
array_shift($item);
}
$item = array_unique($item);
$this->set($name, $item);
}
/**
* 如果不存在则写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return mixed
*/
public function remember(string $name, $value, $expire = null)
{
if ($this->has($name)) {
return $this->get($name);
}
$time = time();
while ($time + 5 > time() && $this->has($name . '_lock')) {
// 存在锁定则等待
usleep(200000);
}
try {
// 锁定
$this->set($name . '_lock', true);
if ($value instanceof Closure) {
// 获取缓存数据
$value = Container::getInstance()->invokeFunction($value);
}
// 缓存数据
$this->set($name, $value, $expire);
// 解锁
$this->delete($name . '_lock');
} catch (Exception | throwable $e) {
$this->delete($name . '_lock');
throw $e;
}
return $value;
}
/**
* 缓存标签
* @access public
* @param string|array $name 标签名
* @return TagSet
*/
public function tag($name): TagSet
{
$name = (array) $name;
$key = implode('-', $name);
if (!isset($this->tag[$key])) {
$name = array_map(function ($val) {
return $this->getTagKey($val);
}, $name);
$this->tag[$key] = new TagSet($name, $this);
}
return $this->tag[$key];
}
/**
* 获取标签包含的缓存标识
* @access public
* @param string $tag 标签标识
* @return array
*/
public function getTagItems(string $tag): array
{
return $this->get($tag, []);
}
/**
* 获取实际标签名
* @access public
* @param string $tag 标签名
* @return string
*/
public function getTagKey(string $tag): string
{
return $this->options['tag_prefix'] . md5($tag);
}
/**
* 序列化数据
* @access protected
* @param mixed $data 缓存数据
* @return string
*/
protected function serialize($data): string
{
if (is_numeric($data)) {
return (string) $data;
}
$serialize = $this->options['serialize'][0] ?? "serialize";
return $serialize($data);
}
/**
* 反序列化数据
* @access protected
* @param string $data 缓存数据
* @return mixed
*/
protected function unserialize(string $data)
{
if (is_numeric($data)) {
return $data;
}
$unserialize = $this->options['serialize'][1] ?? "unserialize";
return $unserialize($data);
}
/**
* 返回句柄对象,可执行其它高级方法
*
* @access public
* @return object
*/
public function handler()
{
return $this->handler;
}
/**
* 返回缓存读取次数
* @access public
* @return int
*/
public function getReadTimes(): int
{
return $this->readTimes;
}
/**
* 返回缓存写入次数
* @access public
* @return int
*/
public function getWriteTimes(): int
{
return $this->writeTimes;
}
/**
* 读取缓存
* @access public
* @param iterable $keys 缓存变量名
* @param mixed $default 默认值
* @return iterable
* @throws InvalidArgumentException
*/
public function getMultiple($keys, $default = null): iterable
{
$result = [];
foreach ($keys as $key) {
$result[$key] = $this->get($key, $default);
}
return $result;
}
/**
* 写入缓存
* @access public
* @param iterable $values 缓存数据
* @param null|int|\DateInterval $ttl 有效时间 0为永久
* @return bool
*/
public function setMultiple($values, $ttl = null): bool
{
foreach ($values as $key => $val) {
$result = $this->set($key, $val, $ttl);
if (false === $result) {
return false;
}
}
return true;
}
/**
* 删除缓存
* @access public
* @param iterable $keys 缓存变量名
* @return bool
* @throws InvalidArgumentException
*/
public function deleteMultiple($keys): bool
{
foreach ($keys as $key) {
$result = $this->delete($key);
if (false === $result) {
return false;
}
}
return true;
}
public function __call($method, $args)
{
return call_user_func_array([$this->handler, $method], $args);
}
}

View File

@@ -0,0 +1,130 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache;
/**
* 标签集合
*/
class TagSet
{
/**
* 标签的缓存Key
* @var array
*/
protected $tag;
/**
* 缓存句柄
* @var Driver
*/
protected $handler;
/**
* 架构函数
* @access public
* @param array $tag 缓存标签
* @param Driver $cache 缓存对象
*/
public function __construct(array $tag, Driver $cache)
{
$this->tag = $tag;
$this->handler = $cache;
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\DateTime $expire 有效时间(秒)
* @return bool
*/
public function set(string $name, $value, $expire = null): bool
{
$this->handler->set($name, $value, $expire);
$this->append($name);
return true;
}
/**
* 追加缓存标识到标签
* @access public
* @param string $name 缓存变量名
* @return void
*/
public function append(string $name): void
{
$name = $this->handler->getCacheKey($name);
foreach ($this->tag as $tag) {
$this->handler->push($tag, $name);
}
}
/**
* 写入缓存
* @access public
* @param iterable $values 缓存数据
* @param null|int|\DateInterval $ttl 有效时间 0为永久
* @return bool
*/
public function setMultiple($values, $ttl = null): bool
{
foreach ($values as $key => $val) {
$result = $this->set($key, $val, $ttl);
if (false === $result) {
return false;
}
}
return true;
}
/**
* 如果不存在则写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return mixed
*/
public function remember(string $name, $value, $expire = null)
{
$result = $this->handler->remember($name, $value, $expire);
$this->append($name);
return $result;
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
// 指定标签清除
foreach ($this->tag as $tag) {
$names = $this->handler->getTagItems($tag);
$this->handler->clearTag($names);
$this->handler->delete($tag);
}
return true;
}
}

View File

@@ -0,0 +1,304 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache\driver;
use FilesystemIterator;
use think\App;
use think\cache\Driver;
/**
* 文件缓存类
*/
class File extends Driver
{
/**
* 配置参数
* @var array
*/
protected $options = [
'expire' => 0,
'cache_subdir' => true,
'prefix' => '',
'path' => '',
'hash_type' => 'md5',
'data_compress' => false,
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @param App $app
* @param array $options 参数
*/
public function __construct(App $app, array $options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if (empty($this->options['path'])) {
$this->options['path'] = $app->getRuntimePath() . 'cache';
}
if (substr($this->options['path'], -1) != DIRECTORY_SEPARATOR) {
$this->options['path'] .= DIRECTORY_SEPARATOR;
}
}
/**
* 取得变量的存储文件名
* @access public
* @param string $name 缓存变量名
* @return string
*/
public function getCacheKey(string $name): string
{
$name = hash($this->options['hash_type'], $name);
if ($this->options['cache_subdir']) {
// 使用子目录
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . substr($name, 2);
}
if ($this->options['prefix']) {
$name = $this->options['prefix'] . DIRECTORY_SEPARATOR . $name;
}
return $this->options['path'] . $name . '.php';
}
/**
* 获取缓存数据
* @param string $name 缓存标识名
* @return array|null
*/
protected function getRaw(string $name)
{
$filename = $this->getCacheKey($name);
if (!is_file($filename)) {
return;
}
$content = @file_get_contents($filename);
if (false !== $content) {
$expire = (int) substr($content, 8, 12);
if (0 != $expire && time() - $expire > filemtime($filename)) {
//缓存过期删除缓存文件
$this->unlink($filename);
return;
}
$content = substr($content, 32);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//启用数据压缩
$content = gzuncompress($content);
}
return ['content' => $content, 'expire' => $expire];
}
}
/**
* 判断缓存是否存在
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
return $this->getRaw($name) !== null;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$raw = $this->getRaw($name);
return is_null($raw) ? $default : $this->unserialize($raw['content']);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int|\DateTime $expire 有效时间 0为永久
* @return bool
*/
public function set($name, $value, $expire = null): bool
{
$this->writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$expire = $this->getExpireTime($expire);
$filename = $this->getCacheKey($name);
$dir = dirname($filename);
if (!is_dir($dir)) {
try {
mkdir($dir, 0755, true);
} catch (\Exception $e) {
// 创建失败
}
}
$data = $this->serialize($value);
if ($this->options['data_compress'] && function_exists('gzcompress')) {
//数据压缩
$data = gzcompress($data, 3);
}
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
$result = file_put_contents($filename, $data);
if ($result) {
clearstatcache();
return true;
}
return false;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
if ($raw = $this->getRaw($name)) {
$value = $this->unserialize($raw['content']) + $step;
$expire = $raw['expire'];
} else {
$value = $step;
$expire = 0;
}
return $this->set($name, $value, $expire) ? $value : false;
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
return $this->inc($name, -$step);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function delete($name): bool
{
$this->writeTimes++;
return $this->unlink($this->getCacheKey($name));
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
$dirname = $this->options['path'] . $this->options['prefix'];
$this->rmdir($dirname);
return true;
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
foreach ($keys as $key) {
$this->unlink($key);
}
}
/**
* 判断文件是否存在后,删除
* @access private
* @param string $path
* @return bool
*/
private function unlink(string $path): bool
{
try {
return is_file($path) && unlink($path);
} catch (\Exception $e) {
return false;
}
}
/**
* 删除文件夹
* @param $dirname
* @return bool
*/
private function rmdir($dirname)
{
if (!is_dir($dirname)) {
return false;
}
$items = new FilesystemIterator($dirname);
foreach ($items as $item) {
if ($item->isDir() && !$item->isLink()) {
$this->rmdir($item->getPathname());
} else {
$this->unlink($item->getPathname());
}
}
@rmdir($dirname);
return true;
}
}

View File

@@ -0,0 +1,209 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache\driver;
use think\cache\Driver;
/**
* Memcache缓存类
*/
class Memcache extends Driver
{
/**
* 配置参数
* @var array
*/
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => 0, // 超时时间(单位:毫秒)
'persistent' => true,
'prefix' => '',
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
* @throws \BadFunctionCallException
*/
public function __construct(array $options = [])
{
if (!extension_loaded('memcache')) {
throw new \BadFunctionCallException('not support: memcache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = new \Memcache;
// 支持集群
$hosts = (array) $this->options['host'];
$ports = (array) $this->options['port'];
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
foreach ($hosts as $i => $host) {
$port = $ports[$i] ?? $ports[0];
$this->options['timeout'] > 0 ?
$this->handler->addServer($host, (int) $port, $this->options['persistent'], 1, (int) $this->options['timeout']) :
$this->handler->addServer($host, (int) $port, $this->options['persistent'], 1);
}
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
$key = $this->getCacheKey($name);
return false !== $this->handler->get($key);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$result = $this->handler->get($this->getCacheKey($name));
return false !== $result ? $this->unserialize($result) : $default;
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int|\DateTime $expire 有效时间(秒)
* @return bool
*/
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 ($this->handler->set($key, $value, 0, $expire)) {
return true;
}
return false;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
if ($this->handler->get($key)) {
return $this->handler->increment($key, $step);
}
return $this->handler->set($key, $step);
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
$value = $this->handler->get($key) - $step;
$res = $this->handler->set($key, $value);
return !$res ? false : $value;
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @param bool|false $ttl
* @return bool
*/
public function delete($name, $ttl = false): bool
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return false === $ttl ?
$this->handler->delete($key) :
$this->handler->delete($key, $ttl);
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
return $this->handler->flush();
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
foreach ($keys as $key) {
$this->handler->delete($key);
}
}
}

View File

@@ -0,0 +1,221 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache\driver;
use think\cache\Driver;
/**
* Memcached缓存类
*/
class Memcached extends Driver
{
/**
* 配置参数
* @var array
*/
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => 0, // 超时时间(单位:毫秒)
'prefix' => '',
'username' => '', //账号
'password' => '', //密码
'option' => [],
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
*/
public function __construct(array $options = [])
{
if (!extension_loaded('memcached')) {
throw new \BadFunctionCallException('not support: memcached');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = new \Memcached;
if (!empty($this->options['option'])) {
$this->handler->setOptions($this->options['option']);
}
// 设置连接超时时间(单位:毫秒)
if ($this->options['timeout'] > 0) {
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
}
// 支持集群
$hosts = (array) $this->options['host'];
$ports = (array) $this->options['port'];
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
$servers = [];
foreach ($hosts as $i => $host) {
$servers[] = [$host, $ports[$i] ?? $ports[0], 1];
}
$this->handler->addServers($servers);
if ('' != $this->options['username']) {
$this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$this->handler->setSaslAuthData($this->options['username'], $this->options['password']);
}
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
$key = $this->getCacheKey($name);
return $this->handler->get($key) ? true : false;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$result = $this->handler->get($this->getCacheKey($name));
return false !== $result ? $this->unserialize($result) : $default;
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\DateTime $expire 有效时间(秒)
* @return bool
*/
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 ($this->handler->set($key, $value, $expire)) {
return true;
}
return false;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
if ($this->handler->get($key)) {
return $this->handler->increment($key, $step);
}
return $this->handler->set($key, $step);
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
$value = $this->handler->get($key) - $step;
$res = $this->handler->set($key, $value);
return !$res ? false : $value;
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @param bool|false $ttl
* @return bool
*/
public function delete($name, $ttl = false): bool
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return false === $ttl ?
$this->handler->delete($key) :
$this->handler->delete($key, $ttl);
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
return $this->handler->flush();
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
$this->handler->deleteMulti($keys);
}
}

View File

@@ -0,0 +1,250 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <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
{
/** @var \Predis\Client|\Redis */
protected $handler;
/**
* 配置参数
* @var array
*/
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => false,
'prefix' => '',
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
*/
public function __construct(array $options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if (extension_loaded('redis')) {
$this->handler = new \Redis;
// dump($this->options);exit;
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($this->options['select']);
}
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
return $this->handler->exists($this->getCacheKey($name)) ? true : false;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$value = $this->handler->get($this->getCacheKey($name));
if (false === $value || is_null($value)) {
return $default;
}
return $this->unserialize($value);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\DateTime $expire 有效时间(秒)
* @return bool
*/
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;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->incrby($key, $step);
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->decrby($key, $step);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function delete($name): bool
{
$this->writeTimes++;
$result = $this->handler->del($this->getCacheKey($name));
return $result > 0;
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
$this->handler->flushDB();
return true;
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
// 指定标签清除
$this->handler->del($keys);
}
/**
* 追加(数组)缓存数据
* @access public
* @param string $name 缓存标识
* @param mixed $value 数据
* @return void
*/
public function push(string $name, $value): void
{
$this->handler->sAdd($name, $value);
}
/**
* 获取标签包含的缓存标识
* @access public
* @param string $tag 缓存标签
* @return array
*/
public function getTagItems(string $tag): array
{
return $this->handler->sMembers($tag);
}
}

View File

@@ -0,0 +1,175 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\cache\driver;
use think\cache\Driver;
/**
* Wincache缓存驱动
*/
class Wincache extends Driver
{
/**
* 配置参数
* @var array
*/
protected $options = [
'prefix' => '',
'expire' => 0,
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
* @throws \BadFunctionCallException
*/
public function __construct(array $options = [])
{
if (!function_exists('wincache_ucache_info')) {
throw new \BadFunctionCallException('not support: WinCache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
$this->readTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_exists($key);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\DateTime $expire 有效时间(秒)
* @return bool
*/
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 (wincache_ucache_set($key, $value, $expire)) {
return true;
}
return false;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_inc($key, $step);
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_dec($key, $step);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function delete($name): bool
{
$this->writeTimes++;
return wincache_ucache_delete($this->getCacheKey($name));
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
return wincache_ucache_clear();
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
wincache_ucache_delete($keys);
}
}