* [refac] refactor cache library.
This commit is contained in:
Vendored
+17
-24
@@ -30,6 +30,8 @@ class cache
|
||||
|
||||
const DRIVER_REDIS = 'Redis';
|
||||
|
||||
const DRIVER_LIST = [self::DRIVER_APCU, self::DRIVER_FILE, self::DRIVER_YAC, self::DRIVER_REDIS];
|
||||
|
||||
/**
|
||||
* 全局应用程序对象。
|
||||
* Global application object.
|
||||
@@ -155,33 +157,24 @@ class cache
|
||||
if(empty($this->config->cache->enable)) return $this->log('The cache is not enabled', __FILE__, __LINE__);
|
||||
|
||||
$driver = ucfirst(strtolower($this->config->cache->driver));
|
||||
switch($driver)
|
||||
{
|
||||
case self::DRIVER_APCU:
|
||||
$this->setConnector('-');
|
||||
$className = 'ZenTao\Cache\Driver\ApcuDriver';
|
||||
break;
|
||||
case self::DRIVER_YAC:
|
||||
$this->setConnector('-');
|
||||
$className = 'ZenTao\Cache\Driver\YacDriver';
|
||||
break;
|
||||
case self::DRIVER_FILE:
|
||||
$this->setConnector('_');
|
||||
$className = 'ZenTao\Cache\Driver\FileDriver';
|
||||
break;
|
||||
case self::DRIVER_REDIS:
|
||||
$this->setConnector(':');
|
||||
$className = 'ZenTao\Cache\Driver\RedisDriver';
|
||||
break;
|
||||
default:
|
||||
return $this->log("Driver {$driver} is not supported.", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
if(!in_array($driver, self::DRIVER_LIST)) return $this->log("Driver {$driver} is not supported.", __FILE__, __LINE__);
|
||||
if($driver != self::DRIVER_FILE && !extension_loaded($driver)) return $this->log("Driver ext-{$driver} is not loaded.", __FILE__, __LINE__);
|
||||
|
||||
$this->cache = new $className($this->config->cache->namespace, $this->config->cache->lifetime, $app->getCacheRoot());
|
||||
$connector = $driver == self::DRIVER_REDIS ? ':' : '-';
|
||||
$className = "ZenTao\Cache\Driver\\{$driver}Driver";
|
||||
$scope = $this->config->cache->scope;
|
||||
$namespace = $this->config->cache->namespace;
|
||||
$lifetime = $this->config->cache->lifetime;
|
||||
$redis = $this->config->redis;
|
||||
|
||||
$this->setNamespace($this->config->cache->namespace);
|
||||
$this->setNamespace($namespace);
|
||||
$this->setConnector($connector);
|
||||
|
||||
if($driver == self::DRIVER_APCU) return $this->cache = new $className($namespace, $lifetime, $scope, $connector);
|
||||
if($driver == self::DRIVER_REDIS) return $this->cache = new $className($namespace, $lifetime, $scope, $connector, $redis);
|
||||
if($driver == self::DRIVER_YAC) return $this->cache = new $className($namespace, $lifetime);
|
||||
if($driver == self::DRIVER_FILE) return $this->cache = new $className($namespace, $lifetime, $app->getCacheRoot());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -466,7 +459,7 @@ class cache
|
||||
/* 如果没有设置关联字段则整个缓存都受影响。If no associated fields are set, the entire cache is affected. */
|
||||
if(empty($res->fields))
|
||||
{
|
||||
$keys[] = $this->getResCacheKey($res->name);
|
||||
$keys = $this->getResCacheKey($res->name);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Vendored
+42
-3
@@ -17,19 +17,47 @@ use ZenTao\Cache\SimpleCache\InvalidArgumentException;
|
||||
class ApcuDriver implements CacheInterface
|
||||
{
|
||||
/**
|
||||
* 缓存命名空间。
|
||||
* The cache namespace.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $namespace;
|
||||
|
||||
/**
|
||||
* 缓存默认生命周期。
|
||||
* The cache default lifetime.
|
||||
*
|
||||
* @access private
|
||||
* @var int
|
||||
*/
|
||||
private $defaultLifetime;
|
||||
|
||||
public function __construct($namespace = '', $defaultLifetime = 0)
|
||||
/**
|
||||
* 缓存服务范围。private 独享|public 共享。
|
||||
* The cache scope.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $scope;
|
||||
|
||||
/**
|
||||
* 缓存键连接符。
|
||||
* Cache key connector.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $connector;
|
||||
|
||||
public function __construct($namespace = '', $defaultLifetime = 0, $scope = '', $connector = '')
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
$this->namespace = $namespace;
|
||||
$this->defaultLifetime = $defaultLifetime;
|
||||
$this->scope = $scope;
|
||||
$this->connector = $connector;
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
@@ -53,7 +81,18 @@ class ApcuDriver implements CacheInterface
|
||||
|
||||
public function clear()
|
||||
{
|
||||
return apcu_clear_cache();
|
||||
if($this->scope == 'private') return apcu_clear_cache();
|
||||
|
||||
$keys = [];
|
||||
$info = apcu_cache_info();
|
||||
$cacheList = $info['cache_list'];
|
||||
foreach($cacheList as $cache)
|
||||
{
|
||||
if(strpos($cache['info'], $this->namespace . $this->connector) === 0) $keys[] = $cache['info'];
|
||||
}
|
||||
if(!$keys) return true;
|
||||
|
||||
return $this->deleteMultiple($keys);
|
||||
}
|
||||
|
||||
public function getMultiple($keys, $default = null)
|
||||
|
||||
Vendored
+44
-34
@@ -33,57 +33,49 @@ class RedisDriver implements CacheInterface
|
||||
private $defaultLifetime;
|
||||
|
||||
/**
|
||||
* 缓存序列化器,默认使用 PHP 内置的序列化器。
|
||||
* The cache serializer, the default is the PHP built-in serializer.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $serializer = \Redis::SERIALIZER_PHP;
|
||||
|
||||
public function __construct($namespace = '', $defaultLifetime = 0)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
$this->defaultLifetime = $defaultLifetime;
|
||||
|
||||
$this->setSerializer();
|
||||
|
||||
$this->connectRedis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置序列化器。
|
||||
* Set the serializer.
|
||||
* 缓存服务范围。private 独享|public 共享。
|
||||
* The cache scope.
|
||||
*
|
||||
* @access private
|
||||
* @return void
|
||||
* @var string
|
||||
*/
|
||||
private function setSerializer()
|
||||
private $scope;
|
||||
|
||||
/**
|
||||
* 缓存键连接符。
|
||||
* Cache key connector.
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $connector;
|
||||
|
||||
public function __construct($namespace = '', $defaultLifetime = 0, $scope = '', $connector = '', $setting = null)
|
||||
{
|
||||
if(function_exists('igbinary_serialize'))
|
||||
{
|
||||
$this->serializer = \Redis::SERIALIZER_IGBINARY;
|
||||
}
|
||||
elseif(function_exists('msgpack_pack'))
|
||||
{
|
||||
$this->serializer = \Redis::SERIALIZER_MSGPACK;
|
||||
}
|
||||
$this->namespace = $namespace;
|
||||
$this->defaultLifetime = $defaultLifetime;
|
||||
$this->scope = $scope;
|
||||
$this->connector = $connector;
|
||||
|
||||
$this->connectRedis($setting);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接 Redis 服务器。
|
||||
* Connect to the Redis server.
|
||||
*
|
||||
* @param object $setting
|
||||
* @access private
|
||||
* @return object
|
||||
*/
|
||||
private function connectRedis()
|
||||
private function connectRedis($setting)
|
||||
{
|
||||
global $config;
|
||||
|
||||
try
|
||||
{
|
||||
$this->redis = \helper::connectRedis($config->redis);
|
||||
$this->redis->setOption(\Redis::OPT_SERIALIZER, $this->serializer);
|
||||
$this->redis = \helper::connectRedis($setting);
|
||||
$this->redis->setOption(\Redis::OPT_SERIALIZER, $this->getSerializer($setting->serializer));
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
@@ -91,6 +83,22 @@ class RedisDriver implements CacheInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置序列化器。
|
||||
* Set the serializer.
|
||||
*
|
||||
* @param string $serializer
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function getSerializer($serializer)
|
||||
{
|
||||
if($serializer == 'igbinary') return \Redis::SERIALIZER_IGBINARY;
|
||||
if($serializer == 'php') return \Redis::SERIALIZER_PHP;
|
||||
if($serializer == 'msgpack') return \Redis::SERIALIZER_MSGPACK;
|
||||
if($serializer == 'json') return \Redis::SERIALIZER_JSON;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value related to the specified key.
|
||||
*
|
||||
@@ -141,13 +149,15 @@ class RedisDriver implements CacheInterface
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
if($this->scope == 'private') return $this->redis->flushDB();
|
||||
|
||||
/* With Redis::SCAN_RETRY enabled */
|
||||
$this->redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
|
||||
|
||||
$it = null;
|
||||
$keys = [];
|
||||
|
||||
while($cachedKeys = $this->redis->scan($it, $this->namespace . ':*'))
|
||||
while($cachedKeys = $this->redis->scan($it, $this->namespace . $this->connector . '*'))
|
||||
{
|
||||
$keys = array_merge($keys, $cachedKeys);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user