<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\cache\driver;
use FilesystemIterator;
use think\App;
use think\cache\Driver;
class File extends Driver
{
protected $options = [
'expire' => 0,
'cache_subdir' => true,
'prefix' => '',
'path' => '',
'hash_type' => 'md5',
'data_compress' => false,
'tag_prefix' => 'tag:',
'serialize' => [],
];
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;
}
}
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';
}
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];
}
}
public function has($name): bool
{
return $this->getRaw($name) !== null;
}
public function get($name, $default = null)
{
$this->readTimes++;
$raw = $this->getRaw($name);
return is_null($raw) ? $default : $this->unserialize($raw['content']);
}
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;
}
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;
}
public function dec(string $name, int $step = 1)
{
return $this->inc($name, -$step);
}
public function delete($name): bool
{
$this->writeTimes++;
return $this->unlink($this->getCacheKey($name));
}
public function clear(): bool
{
$this->writeTimes++;
$dirname = $this->options['path'] . $this->options['prefix'];
$this->rmdir($dirname);
return true;
}
public function clearTag(array $keys): void
{
foreach ($keys as $key) {
$this->unlink($key);
}
}
private function unlink(string $path): bool
{
try {
return is_file($path) && unlink($path);
} catch (\Exception $e) {
return false;
}
}
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;
}
}