Files
EasySoft-ZenTaoPMS/lib/cache/driver/ApcuDriver.php
T

85 lines
1.9 KiB
PHP

<?php
/**
* The cache library of zentaopms.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Lu Fei <lufei@easycorp.ltd>
* @package cache
* @link https://www.zentao.net
*/
namespace ZenTao\Cache\Driver;
use ZenTao\Cache\SimpleCache\CacheInterface;
use ZenTao\Cache\SimpleCache\InvalidArgumentException;
class ApcuDriver implements CacheInterface
{
/**
* @var string
*/
private $namespace;
/**
* @var int
*/
private $defaultLifetime;
public function __construct($namespace = '', $defaultLifetime = 0)
{
$this->namespace = $namespace;
$this->defaultLifetime = $defaultLifetime;
}
public function get($key, $default = null)
{
$value = apcu_fetch($key, $success);
return $success === false ? $default : $value;
}
public function set($key, $value, $ttl = null)
{
$ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
return apcu_store($key, $value, (int) $ttl);
}
public function delete($key)
{
return apcu_delete($key);
}
public function clear()
{
return apcu_clear_cache();
}
public function getMultiple($keys, $default = null)
{
return apcu_fetch($keys);
}
public function setMultiple($values, $ttl = null)
{
$ttl = is_null($ttl) ? $this->defaultLifetime : $ttl;
$result = apcu_store($values, (int) $ttl);
return $result === true ? true : (is_array($result) && count($result) == 0 ? true : false);
}
public function deleteMultiple($keys)
{
$result = apcu_delete($keys);
return count($result) === count($keys) ? false : true;
}
public function has($key)
{
return (bool) apcu_exists($key);
}
}