<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\cache\driver;
use think\cache\Driver;
class Wincache extends Driver
{
protected $options = [
'prefix' => '',
'expire' => 0,
'tag_prefix' => 'tag:',
'serialize' => [],
];
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);
}
}
public function has($name): bool
{
$this->readTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_exists($key);
}
public function get($name, $default = null)
{
$this->readTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
}
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;
}
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_inc($key, $step);
}
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return wincache_ucache_dec($key, $step);
}
public function delete($name): bool
{
$this->writeTimes++;
return wincache_ucache_delete($this->getCacheKey($name));
}
public function clear(): bool
{
$this->writeTimes++;
return wincache_ucache_clear();
}
public function clearTag(array $keys): void
{
wincache_ucache_delete($keys);
}
}