This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

163
vendor/yiisoft/yii2/caching/ApcCache.php vendored Normal file
View File

@@ -0,0 +1,163 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use yii\base\InvalidConfigException;
/**
* ApcCache provides APC caching in terms of an application component.
*
* To use this application component, the [APC PHP extension](http://www.php.net/apc) must be loaded.
* Alternatively [APCu PHP extension](http://www.php.net/apcu) could be used via setting `useApcu` to `true`.
* In order to enable APC or APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
*
* See [[Cache]] for common cache operations that ApcCache supports.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ApcCache extends Cache
{
/**
* @var bool whether to use apcu or apc as the underlying caching extension.
* If true, [apcu](http://pecl.php.net/package/apcu) will be used.
* If false, [apc](http://pecl.php.net/package/apc) will be used.
* Defaults to false.
* @since 2.0.7
*/
public $useApcu = false;
/**
* Initializes this application component.
* It checks if extension required is loaded.
*/
public function init()
{
parent::init();
$extension = $this->useApcu ? 'apcu' : 'apc';
if (!extension_loaded($extension)) {
throw new InvalidConfigException("ApcCache requires PHP $extension extension to be loaded.");
}
}
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$key = $this->buildKey($key);
return $this->useApcu ? apcu_exists($key) : apc_exists($key);
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return $this->useApcu ? apcu_fetch($key) : apc_fetch($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
$values = $this->useApcu ? apcu_fetch($keys) : apc_fetch($keys);
return is_array($values) ? $values : [];
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise.
*/
protected function setValue($key, $value, $duration)
{
return $this->useApcu ? apcu_store($key, $value, $duration) : apc_store($key, $value, $duration);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $duration)
{
$result = $this->useApcu ? apcu_store($data, null, $duration) : apc_store($data, null, $duration);
return is_array($result) ? array_keys($result) : [];
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
return $this->useApcu ? apcu_add($key, $value, $duration) : apc_add($key, $value, $duration);
}
/**
* Adds multiple key-value pairs to cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $duration)
{
$result = $this->useApcu ? apcu_add($data, null, $duration) : apc_add($data, null, $duration);
return is_array($result) ? array_keys($result) : [];
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return $this->useApcu ? apcu_delete($key) : apc_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
return $this->useApcu ? apcu_clear_cache() : apc_clear_cache('user');
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* ArrayCache provides caching for the current request only by storing the values in an array.
*
* See [[Cache]] for common cache operations that ArrayCache supports.
*
* Unlike the [[Cache]], ArrayCache allows the expire parameter of [[set]], [[add]], [[multiSet]] and [[multiAdd]] to
* be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
*
* For enhanced performance of ArrayCache, you can disable serialization of the stored data by setting [[$serializer]] to `false`.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ArrayCache extends Cache
{
private $_cache;
/**
* {@inheritdoc}
*/
public function exists($key)
{
$key = $this->buildKey($key);
return isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true));
}
/**
* {@inheritdoc}
*/
protected function getValue($key)
{
if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
return $this->_cache[$key][0];
}
return false;
}
/**
* {@inheritdoc}
*/
protected function setValue($key, $value, $duration)
{
$this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
return true;
}
/**
* {@inheritdoc}
*/
protected function addValue($key, $value, $duration)
{
if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
return false;
}
$this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
return true;
}
/**
* {@inheritdoc}
*/
protected function deleteValue($key)
{
unset($this->_cache[$key]);
return true;
}
/**
* {@inheritdoc}
*/
protected function flushValues()
{
$this->_cache = [];
return true;
}
}

582
vendor/yiisoft/yii2/caching/Cache.php vendored Normal file
View File

@@ -0,0 +1,582 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\base\Component;
use yii\helpers\StringHelper;
/**
* Cache is the base class for cache classes supporting different cache storage implementations.
*
* A data item can be stored in the cache by calling [[set()]] and be retrieved back
* later (in the same or different request) by [[get()]]. In both operations,
* a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]]
* can also be specified when calling [[set()]]. If the data item expires or the dependency
* changes at the time of calling [[get()]], the cache will return no data.
*
* A typical usage pattern of cache is like the following:
*
* ```php
* $key = 'demo';
* $data = $cache->get($key);
* if ($data === false) {
* // ...generate $data here...
* $cache->set($key, $data, $duration, $dependency);
* }
* ```
*
* Because Cache implements the [[\ArrayAccess]] interface, it can be used like an array. For example,
*
* ```php
* $cache['foo'] = 'some data';
* echo $cache['foo'];
* ```
*
* Derived classes should implement the following methods which do the actual cache storage operations:
*
* - [[getValue()]]: retrieve the value with a key (if any) from cache
* - [[setValue()]]: store the value with a key into cache
* - [[addValue()]]: store the value only if the cache does not have this key before
* - [[deleteValue()]]: delete the value with the specified key from cache
* - [[flushValues()]]: delete all values from cache
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract class Cache extends Component implements CacheInterface
{
/**
* @var string a string prefixed to every cache key so that it is unique globally in the whole cache storage.
* It is recommended that you set a unique cache key prefix for each application if the same cache
* storage is being used by different applications.
*
* To ensure interoperability, only alphanumeric characters should be used.
*/
public $keyPrefix;
/**
* @var null|array|false the functions used to serialize and unserialize cached data. Defaults to null, meaning
* using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient
* serializer (e.g. [igbinary](http://pecl.php.net/package/igbinary)), you may configure this property with
* a two-element array. The first element specifies the serialization function, and the second the deserialization
* function. If this property is set false, data will be directly sent to and retrieved from the underlying
* cache component without any serialization or deserialization. You should not turn off serialization if
* you are using [[Dependency|cache dependency]], because it relies on data serialization. Also, some
* implementations of the cache can not correctly save and retrieve data different from a string type.
*/
public $serializer;
/**
* @var int default duration in seconds before a cache entry will expire. Default value is 0, meaning infinity.
* This value is used by [[set()]] if the duration is not explicitly given.
* @since 2.0.11
*/
public $defaultDuration = 0;
/**
* Builds a normalized cache key from a given key.
*
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
*
* @param mixed $key the key to be normalized
* @return string the generated cache key
*/
public function buildKey($key)
{
if (is_string($key)) {
$key = ctype_alnum($key) && StringHelper::byteLength($key) <= 32 ? $key : md5($key);
} else {
$key = md5(json_encode($key));
}
return $this->keyPrefix . $key;
}
/**
* Retrieves a value from cache with a specified key.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return mixed the value stored in cache, false if the value is not in the cache, expired,
* or the dependency associated with the cached data has changed.
*/
public function get($key)
{
$key = $this->buildKey($key);
$value = $this->getValue($key);
if ($value === false || $this->serializer === false) {
return $value;
} elseif ($this->serializer === null) {
$value = unserialize($value);
} else {
$value = call_user_func($this->serializer[1], $value);
}
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) {
return $value[0];
}
return false;
}
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* In case a cache does not support this feature natively, this method will try to simulate it
* but has no performance improvement over getting it.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$key = $this->buildKey($key);
$value = $this->getValue($key);
return $value !== false;
}
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time,
* which may improve the performance. In case a cache does not support this feature natively,
* this method will try to simulate it.
*
* @param string[] $keys list of string keys identifying the cached values
* @return array list of cached values corresponding to the specified keys. The array
* is returned in terms of (key, value) pairs.
* If a value is not cached or expired, the corresponding array value will be false.
* @deprecated This method is an alias for [[multiGet()]] and will be removed in 2.1.0.
*/
public function mget($keys)
{
return $this->multiGet($keys);
}
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time,
* which may improve the performance. In case a cache does not support this feature natively,
* this method will try to simulate it.
* @param string[] $keys list of string keys identifying the cached values
* @return array list of cached values corresponding to the specified keys. The array
* is returned in terms of (key, value) pairs.
* If a value is not cached or expired, the corresponding array value will be false.
* @since 2.0.7
*/
public function multiGet($keys)
{
$keyMap = [];
foreach ($keys as $key) {
$keyMap[$key] = $this->buildKey($key);
}
$values = $this->getValues(array_values($keyMap));
$results = [];
foreach ($keyMap as $key => $newKey) {
$results[$key] = false;
if (isset($values[$newKey])) {
if ($this->serializer === false) {
$results[$key] = $values[$newKey];
} else {
$value = $this->serializer === null ? unserialize($values[$newKey])
: call_user_func($this->serializer[1], $values[$newKey]);
if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) {
$results[$key] = $value[0];
}
}
}
}
return $results;
}
/**
* Stores a value identified by a key into cache.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param int $duration default duration in seconds before the cache will expire. If not set,
* default [[defaultDuration]] value is used.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return bool whether the value is successfully stored into cache
*/
public function set($key, $value, $duration = null, $dependency = null)
{
if ($duration === null) {
$duration = $this->defaultDuration;
}
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
}
if ($this->serializer === null) {
$value = serialize([$value, $dependency]);
} elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], [$value, $dependency]);
}
$key = $this->buildKey($key);
return $this->setValue($key, $value, $duration);
}
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
* @deprecated This method is an alias for [[multiSet()]] and will be removed in 2.1.0.
*/
public function mset($items, $duration = 0, $dependency = null)
{
return $this->multiSet($items, $duration, $dependency);
}
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
* @since 2.0.7
*/
public function multiSet($items, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
}
$data = [];
foreach ($items as $key => $value) {
if ($this->serializer === null) {
$value = serialize([$value, $dependency]);
} elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], [$value, $dependency]);
}
$key = $this->buildKey($key);
$data[$key] = $value;
}
return $this->setValues($data, $duration);
}
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and expiration time will be preserved.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
* @deprecated This method is an alias for [[multiAdd()]] and will be removed in 2.1.0.
*/
public function madd($items, $duration = 0, $dependency = null)
{
return $this->multiAdd($items, $duration, $dependency);
}
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and expiration time will be preserved.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
* @since 2.0.7
*/
public function multiAdd($items, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
}
$data = [];
foreach ($items as $key => $value) {
if ($this->serializer === null) {
$value = serialize([$value, $dependency]);
} elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], [$value, $dependency]);
}
$key = $this->buildKey($key);
$data[$key] = $value;
}
return $this->addValues($data, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return bool whether the value is successfully stored into cache
*/
public function add($key, $value, $duration = 0, $dependency = null)
{
if ($dependency !== null && $this->serializer !== false) {
$dependency->evaluateDependency($this);
}
if ($this->serializer === null) {
$value = serialize([$value, $dependency]);
} elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], [$value, $dependency]);
}
$key = $this->buildKey($key);
return $this->addValue($key, $value, $duration);
}
/**
* Deletes a value with the specified key from cache.
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool if no error happens during deletion
*/
public function delete($key)
{
$key = $this->buildKey($key);
return $this->deleteValue($key);
}
/**
* Deletes all values from cache.
* Be careful of performing this operation if the cache is shared among multiple applications.
* @return bool whether the flush operation was successful.
*/
public function flush()
{
return $this->flushValues();
}
/**
* Retrieves a value from cache with a specified key.
* This method should be implemented by child classes to retrieve the data
* from specific cache storage.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired. Most often
* value is a string. If you have disabled [[serializer]], it could be something else.
*/
abstract protected function getValue($key);
/**
* Stores a value identified by a key in cache.
* This method should be implemented by child classes to store the data
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
abstract protected function setValue($key, $value, $duration);
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This method should be implemented by child classes to store the data
* in specific cache storage.
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
abstract protected function addValue($key, $value, $duration);
/**
* Deletes a value with the specified key from cache
* This method should be implemented by child classes to delete the data from actual cache storage.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
abstract protected function deleteValue($key);
/**
* Deletes all values from cache.
* Child classes may implement this method to realize the flush operation.
* @return bool whether the flush operation was successful.
*/
abstract protected function flushValues();
/**
* Retrieves multiple values from cache with the specified keys.
* The default implementation calls [[getValue()]] multiple times to retrieve
* the cached values one by one. If the underlying cache storage supports multiget,
* this method should be overridden to exploit that feature.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
$results = [];
foreach ($keys as $key) {
$results[$key] = $this->getValue($key);
}
return $results;
}
/**
* Stores multiple key-value pairs in cache.
* The default implementation calls [[setValue()]] multiple times store values one by one. If the underlying cache
* storage supports multi-set, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $duration)
{
$failedKeys = [];
foreach ($data as $key => $value) {
if ($this->setValue($key, $value, $duration) === false) {
$failedKeys[] = $key;
}
}
return $failedKeys;
}
/**
* Adds multiple key-value pairs to cache.
* The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
* storage supports multi-add, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored.
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $duration)
{
$failedKeys = [];
foreach ($data as $key => $value) {
if ($this->addValue($key, $value, $duration) === false) {
$failedKeys[] = $key;
}
}
return $failedKeys;
}
/**
* Returns whether there is a cache entry with a specified key.
* This method is required by the interface [[\ArrayAccess]].
* @param string $key a key identifying the cached value
* @return bool
*/
public function offsetExists($key)
{
return $this->get($key) !== false;
}
/**
* Retrieves the value from cache with a specified key.
* This method is required by the interface [[\ArrayAccess]].
* @param string $key a key identifying the cached value
* @return mixed the value stored in cache, false if the value is not in the cache or expired.
*/
public function offsetGet($key)
{
return $this->get($key);
}
/**
* Stores the value identified by a key into cache.
* If the cache already contains such a key, the existing value will be
* replaced with the new ones. To add expiration and dependencies, use the [[set()]] method.
* This method is required by the interface [[\ArrayAccess]].
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached
*/
public function offsetSet($key, $value)
{
$this->set($key, $value);
}
/**
* Deletes the value with the specified key from cache
* This method is required by the interface [[\ArrayAccess]].
* @param string $key the key of the value to be deleted
*/
public function offsetUnset($key)
{
$this->delete($key);
}
/**
* Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key,
* or to store the result of $callable execution if there is no cache available for the $key.
*
* Usage example:
*
* ```php
* public function getTopProducts($count = 10) {
* $cache = $this->cache; // Could be Yii::$app->cache
* return $cache->getOrSet(['top-n-products', 'n' => $count], function ($cache) use ($count) {
* return Products::find()->mostPopular()->limit(10)->all();
* }, 1000);
* }
* ```
*
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached.
* In case $callable returns `false`, the value will not be cached.
* @param int $duration default duration in seconds before the cache will expire. If not set,
* [[defaultDuration]] value will be used.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is `false`.
* @return mixed result of $callable execution
* @since 2.0.11
*/
public function getOrSet($key, $callable, $duration = null, $dependency = null)
{
if (($value = $this->get($key)) !== false) {
return $value;
}
$value = call_user_func($callable, $this);
if (!$this->set($key, $value, $duration, $dependency)) {
Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__);
}
return $value;
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* CacheInterface is the base interface for cache.
*
* A data item can be stored in the cache by calling [[set()]] and be retrieved back
* later (in the same or different request) by [[get()]]. In both operations,
* a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]]
* can also be specified when calling [[set()]]. If the data item expires or the dependency
* changes at the time of calling [[get()]], the cache will return no data.
*
* A typical usage pattern of cache is like the following:
*
* ```php
* $key = 'demo';
* $data = $cache->get($key);
* if ($data === false) {
* // ...generate $data here...
* $cache->set($key, $data, $duration, $dependency);
* }
* ```
*
* Because CacheInterface extends the [[\ArrayAccess]] interface, it can be used like an array. For example,
*
* ```php
* $cache['foo'] = 'some data';
* echo $cache['foo'];
* ```
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Dmitry Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.13. Previous framework versions used abstract class [[yii\caching\Cache]] as interface.
*/
interface CacheInterface extends \ArrayAccess
{
/**
* Builds a normalized cache key from a given key.
*
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
*
* @param mixed $key the key to be normalized
* @return string the generated cache key
*/
public function buildKey($key);
/**
* Retrieves a value from cache with a specified key.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return mixed the value stored in cache, false if the value is not in the cache, expired,
* or the dependency associated with the cached data has changed.
*/
public function get($key);
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* In case a cache does not support this feature natively, this method will try to simulate it
* but has no performance improvement over getting it.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key);
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time,
* which may improve the performance. In case a cache does not support this feature natively,
* this method will try to simulate it.
* @param string[] $keys list of string keys identifying the cached values
* @return array list of cached values corresponding to the specified keys. The array
* is returned in terms of (key, value) pairs.
* If a value is not cached or expired, the corresponding array value will be false.
*/
public function multiGet($keys);
/**
* Stores a value identified by a key into cache.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param int $duration default duration in seconds before the cache will expire. If not set,
* default [[defaultDuration]] value is used.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return bool whether the value is successfully stored into cache
*/
public function set($key, $value, $duration = null, $dependency = null);
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
*/
public function multiSet($items, $duration = 0, $dependency = null);
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return bool whether the value is successfully stored into cache
*/
public function add($key, $value, $duration = 0, $dependency = null);
/**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and expiration time will be preserved.
*
* @param array $items the items to be cached, as key-value pairs.
* @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached items. If the dependency changes,
* the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is false.
* @return array array of failed keys
*/
public function multiAdd($items, $duration = 0, $dependency = null);
/**
* Deletes a value with the specified key from cache.
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool if no error happens during deletion
*/
public function delete($key);
/**
* Deletes all values from cache.
* Be careful of performing this operation if the cache is shared among multiple applications.
* @return bool whether the flush operation was successful.
*/
public function flush();
/**
* Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key,
* or to store the result of $callable execution if there is no cache available for the $key.
*
* Usage example:
*
* ```php
* public function getTopProducts($count = 10) {
* $cache = $this->cache; // Could be Yii::$app->cache
* return $cache->getOrSet(['top-n-products', 'n' => $count], function ($cache) use ($count) {
* return Products::find()->mostPopular()->limit($count)->all();
* }, 1000);
* }
* ```
*
* @param mixed $key a key identifying the value to be cached. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached.
* In case $callable returns `false`, the value will not be cached.
* @param int $duration default duration in seconds before the cache will expire. If not set,
* [[defaultDuration]] value will be used.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* This parameter is ignored if [[serializer]] is `false`.
* @return mixed result of $callable execution
*/
public function getOrSet($key, $callable, $duration = null, $dependency = null);
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* ChainedDependency represents a dependency which is composed of a list of other dependencies.
*
* When [[dependOnAll]] is true, if any of the dependencies has changed, this dependency is
* considered changed; When [[dependOnAll]] is false, if one of the dependencies has NOT changed,
* this dependency is considered NOT changed.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ChainedDependency extends Dependency
{
/**
* @var Dependency[] list of dependencies that this dependency is composed of.
* Each array element must be a dependency object.
*/
public $dependencies = [];
/**
* @var bool whether this dependency is depending on every dependency in [[dependencies]].
* Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
* When it is set false, it means if one of the dependencies has NOT changed, this dependency
* is considered NOT changed.
*/
public $dependOnAll = true;
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
*/
public function evaluateDependency($cache)
{
foreach ($this->dependencies as $dependency) {
$dependency->evaluateDependency($cache);
}
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData($cache)
{
return null;
}
/**
* {@inheritdoc}
*/
public function isChanged($cache)
{
foreach ($this->dependencies as $dependency) {
if ($this->dependOnAll && $dependency->isChanged($cache)) {
return true;
} elseif (!$this->dependOnAll && !$dependency->isChanged($cache)) {
return false;
}
}
return !$this->dependOnAll;
}
}

286
vendor/yiisoft/yii2/caching/DbCache.php vendored Normal file
View File

@@ -0,0 +1,286 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\PdoValue;
use yii\db\Query;
use yii\di\Instance;
/**
* DbCache implements a cache application component by storing cached data in a database.
*
* By default, DbCache stores session data in a DB table named 'cache'. This table
* must be pre-created. The table name can be changed by setting [[cacheTable]].
*
* Please refer to [[Cache]] for common cache operations that are supported by DbCache.
*
* The following example shows how you can configure the application to use DbCache:
*
* ```php
* 'cache' => [
* 'class' => 'yii\caching\DbCache',
* // 'db' => 'mydb',
* // 'cacheTable' => 'my_cache',
* ]
* ```
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DbCache extends Cache
{
/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbCache object is created, if you want to change this property, you should only assign it
* with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/
public $db = 'db';
/**
* @var string name of the DB table to store cache content.
* The table should be pre-created as follows:
*
* ```php
* CREATE TABLE cache (
* id char(128) NOT NULL PRIMARY KEY,
* expire int(11),
* data BLOB
* );
* ```
*
* where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
* that can be used for some popular DBMS:
*
* - MySQL: LONGBLOB
* - PostgreSQL: BYTEA
* - MSSQL: BLOB
*
* When using DbCache in a production server, we recommend you create a DB index for the 'expire'
* column in the cache table to improve the performance.
*/
public $cacheTable = '{{%cache}}';
/**
* @var int the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
*/
public $gcProbability = 100;
/**
* Initializes the DbCache component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
}
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$key = $this->buildKey($key);
$query = new Query();
$query->select(['COUNT(*)'])
->from($this->cacheTable)
->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
if ($this->db->enableQueryCache) {
// temporarily disable and re-enable query caching
$this->db->enableQueryCache = false;
$result = $query->createCommand($this->db)->queryScalar();
$this->db->enableQueryCache = true;
} else {
$result = $query->createCommand($this->db)->queryScalar();
}
return $result > 0;
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$query = new Query();
$query->select(['data'])
->from($this->cacheTable)
->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
if ($this->db->enableQueryCache) {
// temporarily disable and re-enable query caching
$this->db->enableQueryCache = false;
$result = $query->createCommand($this->db)->queryScalar();
$this->db->enableQueryCache = true;
return $result;
}
return $query->createCommand($this->db)->queryScalar();
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
if (empty($keys)) {
return [];
}
$query = new Query();
$query->select(['id', 'data'])
->from($this->cacheTable)
->where(['id' => $keys])
->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')');
if ($this->db->enableQueryCache) {
$this->db->enableQueryCache = false;
$rows = $query->createCommand($this->db)->queryAll();
$this->db->enableQueryCache = true;
} else {
$rows = $query->createCommand($this->db)->queryAll();
}
$results = [];
foreach ($keys as $key) {
$results[$key] = false;
}
foreach ($rows as $row) {
if (is_resource($row['data']) && get_resource_type($row['data']) === 'stream') {
$results[$row['id']] = stream_get_contents($row['data']);
} else {
$results[$row['id']] = $row['data'];
}
}
return $results;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
$result = $this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
$command = $db->createCommand()
->update($this->cacheTable, [
'expire' => $duration > 0 ? $duration + time() : 0,
'data' => new PdoValue($value, \PDO::PARAM_LOB),
], ['id' => $key]);
return $command->execute();
});
if ($result) {
$this->gc();
return true;
}
return $this->addValue($key, $value, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
$this->gc();
try {
$this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
$db->createCommand()
->insert($this->cacheTable, [
'id' => $key,
'expire' => $duration > 0 ? $duration + time() : 0,
'data' => new PdoValue($value, \PDO::PARAM_LOB),
])->execute();
});
return true;
} catch (\Exception $e) {
return false;
}
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
$this->db->noCache(function (Connection $db) use ($key) {
$db->createCommand()
->delete($this->cacheTable, ['id' => $key])
->execute();
});
return true;
}
/**
* Removes the expired data values.
* @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
*/
public function gc($force = false)
{
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$this->db->createCommand()
->delete($this->cacheTable, '[[expire]] > 0 AND [[expire]] < ' . time())
->execute();
}
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
$this->db->createCommand()
->delete($this->cacheTable)
->execute();
return true;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\di\Instance;
/**
* DbDependency represents a dependency based on the query result of a SQL statement.
*
* If the query result changes, the dependency is considered as changed.
* The query is specified via the [[sql]] property.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DbDependency extends Dependency
{
/**
* @var string the application component ID of the DB connection.
*/
public $db = 'db';
/**
* @var string the SQL query whose result is used to determine if the dependency has been changed.
* Only the first row of the query result will be used.
*/
public $sql;
/**
* @var array the parameters (name => value) to be bound to the SQL statement specified by [[sql]].
*/
public $params = [];
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
* @throws InvalidConfigException if [[db]] is not a valid application component ID
*/
protected function generateDependencyData($cache)
{
/* @var $db Connection */
$db = Instance::ensure($this->db, Connection::className());
if ($this->sql === null) {
throw new InvalidConfigException('DbDependency::sql must be set.');
}
if ($db->enableQueryCache) {
// temporarily disable and re-enable query caching
$db->enableQueryCache = false;
$result = $db->createCommand($this->sql, $this->params)->queryOne();
$db->enableQueryCache = true;
} else {
$result = $db->createCommand($this->sql, $this->params)->queryOne();
}
return $result;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use yii\base\InvalidConfigException;
use yii\db\QueryInterface;
use yii\di\Instance;
/**
* DbQueryDependency represents a dependency based on the query result of an [[QueryInterface]] instance.
*
* If the query result changes, the dependency is considered as changed.
* The query is specified via the [[query]] property.
*
* Object of any class which matches [[QueryInterface]] can be used, so this dependency can be used not only
* with regular relational databases but with MongoDB, Redis and so on as well.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @see QueryInterface
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0.12
*/
class DbQueryDependency extends Dependency
{
/**
* @var string|array|object the application component ID of the database connection, connection object or
* its array configuration.
* This field can be left blank, allowing query to determine connection automatically.
*/
public $db;
/**
* @var QueryInterface the query which result is used to determine if the dependency has been changed.
* Actual query method to be invoked is determined by [[method]].
*/
public $query;
/**
* @var string|callable method which should be invoked in over the [[query]] object.
*
* If specified as a string an own query method with such name will be invoked, passing [[db]] value as its
* first argument. For example: `exists`, `all`.
*
* This field can be specified as a PHP callback of following signature:
*
* ```php
* function (QueryInterface $query, mixed $db) {
* //return mixed;
* }
* ```
*
* If not set - [[QueryInterface::one()]] will be used.
*/
public $method;
/**
* Generates the data needed to determine if dependency is changed.
*
* This method returns the query result.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
* @throws InvalidConfigException on invalid configuration.
*/
protected function generateDependencyData($cache)
{
$db = $this->db;
if ($db !== null) {
$db = Instance::ensure($db);
}
if (!$this->query instanceof QueryInterface) {
throw new InvalidConfigException('"' . get_class($this) . '::$query" should be an instance of "yii\db\QueryInterface".');
}
if (!empty($db->enableQueryCache)) {
// temporarily disable and re-enable query caching
$originEnableQueryCache = $db->enableQueryCache;
$db->enableQueryCache = false;
$result = $this->executeQuery($this->query, $db);
$db->enableQueryCache = $originEnableQueryCache;
} else {
$result = $this->executeQuery($this->query, $db);
}
return $result;
}
/**
* Executes the query according to [[method]] specification.
* @param QueryInterface $query query to be executed.
* @param mixed $db connection.
* @return mixed query result.
*/
private function executeQuery($query, $db)
{
if ($this->method === null) {
return $query->one($db);
}
if (is_string($this->method)) {
return call_user_func([$query, $this->method], $db);
}
return call_user_func($this->method, $query, $db);
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* Dependency is the base class for cache dependency classes.
*
* Child classes should override its [[generateDependencyData()]] for generating
* the actual dependency data.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract class Dependency extends \yii\base\BaseObject
{
/**
* @var mixed the dependency data that is saved in cache and later is compared with the
* latest dependency data.
*/
public $data;
/**
* @var bool whether this dependency is reusable or not. True value means that dependent
* data for this cache dependency will be generated only once per request. This allows you
* to use the same cache dependency for multiple separate cache calls while generating the same
* page without an overhead of re-evaluating dependency data each time. Defaults to false.
*/
public $reusable = false;
/**
* @var array static storage of cached data for reusable dependencies.
*/
private static $_reusableData = [];
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
*/
public function evaluateDependency($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$this->data = self::$_reusableData[$hash];
} else {
$this->data = $this->generateDependencyData($cache);
}
}
/**
* Returns a value indicating whether the dependency has changed.
* @deprecated since version 2.0.11. Will be removed in version 2.1. Use [[isChanged()]] instead.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return bool whether the dependency has changed.
*/
public function getHasChanged($cache)
{
return $this->isChanged($cache);
}
/**
* Checks whether the dependency is changed.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return bool whether the dependency has changed.
* @since 2.0.11
*/
public function isChanged($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$data = self::$_reusableData[$hash];
} else {
$data = $this->generateDependencyData($cache);
}
return $data !== $this->data;
}
/**
* Resets all cached data for reusable dependencies.
*/
public static function resetReusableData()
{
self::$_reusableData = [];
}
/**
* Generates a unique hash that can be used for retrieving reusable dependency data.
* @return string a unique hash value for this cache dependency.
* @see reusable
*/
protected function generateReusableHash()
{
$data = $this->data;
$this->data = null; // https://github.com/yiisoft/yii2/issues/3052
$key = sha1(serialize($this));
$this->data = $data;
return $key;
}
/**
* Generates the data needed to determine if dependency is changed.
* Derived classes should override this method to generate the actual dependency data.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
abstract protected function generateDependencyData($cache);
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* DummyCache is a placeholder cache component.
*
* DummyCache does not cache anything. It is provided so that one can always configure
* a 'cache' application component and save the check of existence of `\Yii::$app->cache`.
* By replacing DummyCache with some other cache component, one can quickly switch from
* non-caching mode to caching mode.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DummyCache extends Cache
{
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
return true;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
return true;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return true;
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
return true;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* ExpressionDependency represents a dependency based on the result of a PHP expression.
*
* ExpressionDependency will use `eval()` to evaluate the PHP expression.
* The dependency is reported as unchanged if and only if the result of the expression is
* the same as the one evaluated when storing the data to cache.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ExpressionDependency extends Dependency
{
/**
* @var string the string representation of a PHP expression whose result is used to determine the dependency.
* A PHP expression can be any PHP code that evaluates to a value. To learn more about what an expression is,
* please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
*/
public $expression = 'true';
/**
* @var mixed custom parameters associated with this dependency. You may get the value
* of this property in [[expression]] using `$this->params`.
*/
public $params;
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the result of the PHP expression.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData($cache)
{
return eval("return {$this->expression};");
}
}

View File

@@ -0,0 +1,273 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\helpers\FileHelper;
/**
* FileCache implements a cache component using files.
*
* For each data value being cached, FileCache will store it in a separate file.
* The cache files are placed under [[cachePath]]. FileCache will perform garbage collection
* automatically to remove expired cache files.
*
* Please refer to [[Cache]] for common cache operations that are supported by FileCache.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FileCache extends Cache
{
/**
* @var string a string prefixed to every cache key. This is needed when you store
* cache data under the same [[cachePath]] for different applications to avoid
* conflict.
*
* To ensure interoperability, only alphanumeric characters should be used.
*/
public $keyPrefix = '';
/**
* @var string the directory to store cache files. You may use [path alias](guide:concept-aliases) here.
* If not set, it will use the "cache" subdirectory under the application runtime path.
*/
public $cachePath = '@runtime/cache';
/**
* @var string cache file suffix. Defaults to '.bin'.
*/
public $cacheFileSuffix = '.bin';
/**
* @var int the level of sub-directories to store cache files. Defaults to 1.
* If the system has huge number of cache files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*/
public $directoryLevel = 1;
/**
* @var int the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
* This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
*/
public $gcProbability = 10;
/**
* @var int the permission to be set for newly created cache files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
public $fileMode;
/**
* @var int the permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
public $dirMode = 0775;
/**
* Initializes this component by ensuring the existence of the cache path.
*/
public function init()
{
parent::init();
$this->cachePath = Yii::getAlias($this->cachePath);
if (!is_dir($this->cachePath)) {
FileHelper::createDirectory($this->cachePath, $this->dirMode, true);
}
}
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$cacheFile = $this->getCacheFile($this->buildKey($key));
return @filemtime($cacheFile) > time();
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$cacheFile = $this->getCacheFile($key);
if (@filemtime($cacheFile) > time()) {
$fp = @fopen($cacheFile, 'r');
if ($fp !== false) {
@flock($fp, LOCK_SH);
$cacheValue = @stream_get_contents($fp);
@flock($fp, LOCK_UN);
@fclose($fp);
return $cacheValue;
}
}
return false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached. Other types (If you have disabled [[serializer]]) unable to get is
* correct in [[getValue()]].
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
$this->gc();
$cacheFile = $this->getCacheFile($key);
if ($this->directoryLevel > 0) {
@FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true);
}
if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
if ($this->fileMode !== null) {
@chmod($cacheFile, $this->fileMode);
}
if ($duration <= 0) {
$duration = 31536000; // 1 year
}
return @touch($cacheFile, $duration + time());
}
$error = error_get_last();
Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__);
return false;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached. Other types (if you have disabled [[serializer]]) unable to get is
* correct in [[getValue()]].
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
$cacheFile = $this->getCacheFile($key);
if (@filemtime($cacheFile) > time()) {
return false;
}
return $this->setValue($key, $value, $duration);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
$cacheFile = $this->getCacheFile($key);
return @unlink($cacheFile);
}
/**
* Returns the cache file path given the cache key.
* @param string $key cache key
* @return string the cache file path
*/
protected function getCacheFile($key)
{
if ($this->directoryLevel > 0) {
$base = $this->cachePath;
for ($i = 0; $i < $this->directoryLevel; ++$i) {
if (($prefix = substr($key, $i + $i, 2)) !== false) {
$base .= DIRECTORY_SEPARATOR . $prefix;
}
}
return $base . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
}
return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
$this->gc(true, false);
return true;
}
/**
* Removes expired cache files.
* @param bool $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
* @param bool $expiredOnly whether to removed expired cache files only.
* If false, all cache files under [[cachePath]] will be removed.
*/
public function gc($force = false, $expiredOnly = true)
{
if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$this->gcRecursive($this->cachePath, $expiredOnly);
}
}
/**
* Recursively removing expired cache files under a directory.
* This method is mainly used by [[gc()]].
* @param string $path the directory under which expired cache files are removed.
* @param bool $expiredOnly whether to only remove expired cache files. If false, all files
* under `$path` will be removed.
*/
protected function gcRecursive($path, $expiredOnly)
{
if (($handle = opendir($path)) !== false) {
while (($file = readdir($handle)) !== false) {
if ($file[0] === '.') {
continue;
}
$fullPath = $path . DIRECTORY_SEPARATOR . $file;
if (is_dir($fullPath)) {
$this->gcRecursive($fullPath, $expiredOnly);
if (!$expiredOnly) {
if (!@rmdir($fullPath)) {
$error = error_get_last();
Yii::warning("Unable to remove directory '{$fullPath}': {$error['message']}", __METHOD__);
}
}
} elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) {
if (!@unlink($fullPath)) {
$error = error_get_last();
Yii::warning("Unable to remove file '{$fullPath}': {$error['message']}", __METHOD__);
}
}
}
closedir($handle);
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException;
/**
* FileDependency represents a dependency based on a file's last modification time.
*
* If the last modification time of the file specified via [[fileName]] is changed,
* the dependency is considered as changed.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FileDependency extends Dependency
{
/**
* @var string the file path or [path alias](guide:concept-aliases) whose last modification time is used to
* check if the dependency has been changed.
*/
public $fileName;
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the file's last modification time.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
* @throws InvalidConfigException if [[fileName]] is not set
*/
protected function generateDependencyData($cache)
{
if ($this->fileName === null) {
throw new InvalidConfigException('FileDependency::fileName must be set');
}
$fileName = Yii::getAlias($this->fileName);
clearstatcache(false, $fileName);
return @filemtime($fileName);
}
}

365
vendor/yiisoft/yii2/caching/MemCache.php vendored Normal file
View File

@@ -0,0 +1,365 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
use Yii;
use yii\base\InvalidConfigException;
/**
* MemCache implements a cache application component based on [memcache](http://pecl.php.net/package/memcache)
* and [memcached](http://pecl.php.net/package/memcached).
*
* MemCache supports both [memcache](http://pecl.php.net/package/memcache) and
* [memcached](http://pecl.php.net/package/memcached). By setting [[useMemcached]] to be true or false,
* one can let MemCache to use either memcached or memcache, respectively.
*
* MemCache can be configured with a list of memcache servers by settings its [[servers]] property.
* By default, MemCache assumes there is a memcache server running on localhost at port 11211.
*
* See [[Cache]] for common cache operations that MemCache supports.
*
* Note, there is no security measure to protected data in memcache.
* All data in memcache can be accessed by any process running in the system.
*
* To use MemCache as the cache application component, configure the application as follows,
*
* ```php
* [
* 'components' => [
* 'cache' => [
* 'class' => 'yii\caching\MemCache',
* 'servers' => [
* [
* 'host' => 'server1',
* 'port' => 11211,
* 'weight' => 60,
* ],
* [
* 'host' => 'server2',
* 'port' => 11211,
* 'weight' => 40,
* ],
* ],
* ],
* ],
* ]
* ```
*
* In the above, two memcache servers are used: server1 and server2. You can configure more properties of
* each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @property \Memcache|\Memcached $memcache The memcache (or memcached) object used by this cache component.
* This property is read-only.
* @property MemCacheServer[] $servers List of memcache server configurations. Note that the type of this
* property differs in getter and setter. See [[getServers()]] and [[setServers()]] for details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MemCache extends Cache
{
/**
* @var bool whether to use memcached or memcache as the underlying caching extension.
* If true, [memcached](http://pecl.php.net/package/memcached) will be used.
* If false, [memcache](http://pecl.php.net/package/memcache) will be used.
* Defaults to false.
*/
public $useMemcached = false;
/**
* @var string an ID that identifies a Memcached instance. This property is used only when [[useMemcached]] is true.
* By default the Memcached instances are destroyed at the end of the request. To create an instance that
* persists between requests, you may specify a unique ID for the instance. All instances created with the
* same ID will share the same connection.
* @see http://ca2.php.net/manual/en/memcached.construct.php
*/
public $persistentId;
/**
* @var array options for Memcached. This property is used only when [[useMemcached]] is true.
* @see http://ca2.php.net/manual/en/memcached.setoptions.php
*/
public $options;
/**
* @var string memcached sasl username. This property is used only when [[useMemcached]] is true.
* @see http://php.net/manual/en/memcached.setsaslauthdata.php
*/
public $username;
/**
* @var string memcached sasl password. This property is used only when [[useMemcached]] is true.
* @see http://php.net/manual/en/memcached.setsaslauthdata.php
*/
public $password;
/**
* @var \Memcache|\Memcached the Memcache instance
*/
private $_cache;
/**
* @var array list of memcache server configurations
*/
private $_servers = [];
/**
* Initializes this application component.
* It creates the memcache instance and adds memcache servers.
*/
public function init()
{
parent::init();
$this->addServers($this->getMemcache(), $this->getServers());
}
/**
* Add servers to the server pool of the cache specified.
*
* @param \Memcache|\Memcached $cache
* @param MemCacheServer[] $servers
* @throws InvalidConfigException
*/
protected function addServers($cache, $servers)
{
if (empty($servers)) {
$servers = [new MemCacheServer([
'host' => '127.0.0.1',
'port' => 11211,
])];
} else {
foreach ($servers as $server) {
if ($server->host === null) {
throw new InvalidConfigException("The 'host' property must be specified for every memcache server.");
}
}
}
if ($this->useMemcached) {
$this->addMemcachedServers($cache, $servers);
} else {
$this->addMemcacheServers($cache, $servers);
}
}
/**
* Add servers to the server pool of the cache specified
* Used for memcached PECL extension.
*
* @param \Memcached $cache
* @param MemCacheServer[] $servers
*/
protected function addMemcachedServers($cache, $servers)
{
$existingServers = [];
if ($this->persistentId !== null) {
foreach ($cache->getServerList() as $s) {
$existingServers[$s['host'] . ':' . $s['port']] = true;
}
}
foreach ($servers as $server) {
if (empty($existingServers) || !isset($existingServers[$server->host . ':' . $server->port])) {
$cache->addServer($server->host, $server->port, $server->weight);
}
}
}
/**
* Add servers to the server pool of the cache specified
* Used for memcache PECL extension.
*
* @param \Memcache $cache
* @param MemCacheServer[] $servers
*/
protected function addMemcacheServers($cache, $servers)
{
$class = new \ReflectionClass($cache);
$paramCount = $class->getMethod('addServer')->getNumberOfParameters();
foreach ($servers as $server) {
// $timeout is used for memcache versions that do not have $timeoutms parameter
$timeout = (int) ($server->timeout / 1000) + (($server->timeout % 1000 > 0) ? 1 : 0);
if ($paramCount === 9) {
$cache->addserver(
$server->host,
$server->port,
$server->persistent,
$server->weight,
$timeout,
$server->retryInterval,
$server->status,
$server->failureCallback,
$server->timeout
);
} else {
$cache->addserver(
$server->host,
$server->port,
$server->persistent,
$server->weight,
$timeout,
$server->retryInterval,
$server->status,
$server->failureCallback
);
}
}
}
/**
* Returns the underlying memcache (or memcached) object.
* @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
* @throws InvalidConfigException if memcache or memcached extension is not loaded
*/
public function getMemcache()
{
if ($this->_cache === null) {
$extension = $this->useMemcached ? 'memcached' : 'memcache';
if (!extension_loaded($extension)) {
throw new InvalidConfigException("MemCache requires PHP $extension extension to be loaded.");
}
if ($this->useMemcached) {
$this->_cache = $this->persistentId !== null ? new \Memcached($this->persistentId) : new \Memcached();
if ($this->username !== null || $this->password !== null) {
$this->_cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$this->_cache->setSaslAuthData($this->username, $this->password);
}
if (!empty($this->options)) {
$this->_cache->setOptions($this->options);
}
} else {
$this->_cache = new \Memcache();
}
}
return $this->_cache;
}
/**
* Returns the memcache or memcached server configurations.
* @return MemCacheServer[] list of memcache server configurations.
*/
public function getServers()
{
return $this->_servers;
}
/**
* @param array $config list of memcache or memcached server configurations. Each element must be an array
* with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
* @see http://php.net/manual/en/memcache.addserver.php
* @see http://php.net/manual/en/memcached.addserver.php
*/
public function setServers($config)
{
foreach ($config as $c) {
$this->_servers[] = new MemCacheServer($c);
}
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return $this->_cache->get($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached.
* @see [Memcache::set()](http://php.net/manual/en/memcache.set.php)
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
// Use UNIX timestamp since it doesn't have any limitation
// @see http://php.net/manual/en/memcache.set.php
// @see http://php.net/manual/en/memcached.expiration.php
$expire = $duration > 0 ? $duration + time() : 0;
return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys.
*/
protected function setValues($data, $duration)
{
if ($this->useMemcached) {
// Use UNIX timestamp since it doesn't have any limitation
// @see http://php.net/manual/en/memcache.set.php
// @see http://php.net/manual/en/memcached.expiration.php
$expire = $duration > 0 ? $duration + time() : 0;
// Memcached::setMulti() returns boolean
// @see http://php.net/manual/en/memcached.setmulti.php
return $this->_cache->setMulti($data, $expire) ? [] : array_keys($data);
}
return parent::setValues($data, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached
* @see [Memcache::set()](http://php.net/manual/en/memcache.set.php)
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
// Use UNIX timestamp since it doesn't have any limitation
// @see http://php.net/manual/en/memcache.set.php
// @see http://php.net/manual/en/memcached.expiration.php
$expire = $duration > 0 ? $duration + time() : 0;
return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return $this->_cache->delete($key, 0);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
return $this->_cache->flush();
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* MemCacheServer represents the configuration data for a single memcache or memcached server.
*
* See [PHP manual](http://php.net/manual/en/memcache.addserver.php) for detailed explanation
* of each configuration property.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MemCacheServer extends \yii\base\BaseObject
{
/**
* @var string memcache server hostname or IP address
*/
public $host;
/**
* @var int memcache server port
*/
public $port = 11211;
/**
* @var int probability of using this server among all servers.
*/
public $weight = 1;
/**
* @var bool whether to use a persistent connection. This is used by memcache only.
*/
public $persistent = true;
/**
* @var int timeout in milliseconds which will be used for connecting to the server.
* This is used by memcache only. For old versions of memcache that only support specifying
* timeout in seconds this will be rounded up to full seconds.
*/
public $timeout = 1000;
/**
* @var int how often a failed server will be retried (in seconds). This is used by memcache only.
*/
public $retryInterval = 15;
/**
* @var bool if the server should be flagged as online upon a failure. This is used by memcache only.
*/
public $status = true;
/**
* @var \Closure this callback function will run upon encountering an error.
* The callback is run before fail over is attempted. The function takes two parameters,
* the [[host]] and the [[port]] of the failed server.
* This is used by memcache only.
*/
public $failureCallback;
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* TagDependency associates a cached data item with one or multiple [[tags]].
*
* By calling [[invalidate()]], you can invalidate all cached data items that are associated with the specified tag name(s).
*
* ```php
* // setting multiple cache keys to store data forever and tagging them with "user-123"
* Yii::$app->cache->set('user_42_profile', '', 0, new TagDependency(['tags' => 'user-123']));
* Yii::$app->cache->set('user_42_stats', '', 0, new TagDependency(['tags' => 'user-123']));
*
* // invalidating all keys tagged with "user-123"
* TagDependency::invalidate(Yii::$app->cache, 'user-123');
* ```
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TagDependency extends Dependency
{
/**
* @var string|array a list of tag names for this dependency. For a single tag, you may specify it as a string.
*/
public $tags = [];
/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param CacheInterface $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
$newKeys = [];
foreach ($timestamps as $key => $timestamp) {
if ($timestamp === false) {
$newKeys[] = $key;
}
}
if (!empty($newKeys)) {
$timestamps = array_merge($timestamps, static::touchKeys($cache, $newKeys));
}
return $timestamps;
}
/**
* {@inheritdoc}
*/
public function isChanged($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
return $timestamps !== $this->data;
}
/**
* Invalidates all of the cached data items that are associated with any of the specified [[tags]].
* @param CacheInterface $cache the cache component that caches the data items
* @param string|array $tags
*/
public static function invalidate($cache, $tags)
{
$keys = [];
foreach ((array) $tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
static::touchKeys($cache, $keys);
}
/**
* Generates the timestamp for the specified cache keys.
* @param CacheInterface $cache
* @param string[] $keys
* @return array the timestamp indexed by cache keys
*/
protected static function touchKeys($cache, $keys)
{
$items = [];
$time = microtime();
foreach ($keys as $key) {
$items[$key] = $time;
}
$cache->multiSet($items);
return $items;
}
/**
* Returns the timestamps for the specified tags.
* @param CacheInterface $cache
* @param string[] $tags
* @return array the timestamps indexed by the specified tags.
*/
protected function getTimestamps($cache, $tags)
{
if (empty($tags)) {
return [];
}
$keys = [];
foreach ($tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
return $cache->multiGet($keys);
}
}

137
vendor/yiisoft/yii2/caching/WinCache.php vendored Normal file
View File

@@ -0,0 +1,137 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* WinCache provides Windows Cache caching in terms of an application component.
*
* To use this application component, the [WinCache PHP extension](http://www.iis.net/expand/wincacheforphp)
* must be loaded. Also note that "wincache.ucenabled" should be set to "On" in your php.ini file.
*
* See [[Cache]] manual for common cache operations that are supported by WinCache.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class WinCache extends Cache
{
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$key = $this->buildKey($key);
return wincache_ucache_exists($key);
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string|bool the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return wincache_ucache_get($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return wincache_ucache_get($keys);
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
return wincache_ucache_set($key, $value, $duration);
}
/**
* Stores multiple key-value pairs in cache.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function setValues($data, $duration)
{
return wincache_ucache_set($data, null, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
return wincache_ucache_add($key, $value, $duration);
}
/**
* Adds multiple key-value pairs to cache.
* The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
* storage supports multiadd, this method should be overridden to exploit that feature.
* @param array $data array where key corresponds to cache key while value is the value stored
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
* @return array array of failed keys
*/
protected function addValues($data, $duration)
{
return wincache_ucache_add($data, null, $duration);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return wincache_ucache_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
return wincache_ucache_clear();
}
}

111
vendor/yiisoft/yii2/caching/XCache.php vendored Normal file
View File

@@ -0,0 +1,111 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* XCache provides XCache caching in terms of an application component.
*
* To use this application component, the [XCache PHP extension](http://xcache.lighttpd.net/) must be loaded.
* Also note that the [[flush()]] functionality will work correctly only if "xcache.admin.enable_auth"
* is set to "Off" in php.ini.
*
* See [[Cache]] for common cache operations that XCache supports.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @deprecated since 2.0.14. This class will be removed in 2.1.0.
*/
class XCache extends Cache
{
/**
* Checks whether a specified key exists in the cache.
* This can be faster than getting the value from the cache if the data is big.
* Note that this method does not check whether the dependency associated
* with the cached data, if there is any, has changed. So a call to [[get]]
* may return false while exists returns true.
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return bool true if a value exists in cache, false if the value is not in the cache or expired.
*/
public function exists($key)
{
$key = $this->buildKey($key);
return xcache_isset($key);
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return xcache_isset($key) ? xcache_get($key) : false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
return xcache_set($key, $value, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
return !xcache_isset($key) ? $this->setValue($key, $value, $duration) : false;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return xcache_unset($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* ZendDataCache provides Zend data caching in terms of an application component.
*
* To use this application component, the [Zend Data Cache PHP extension](http://www.zend.com/en/products/server/)
* must be loaded.
*
* See [[Cache]] for common cache operations that ZendDataCache supports.
*
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @deprecated since 2.0.14. This class will be removed in 2.1.0.
*/
class ZendDataCache extends Cache
{
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$result = zend_shm_cache_fetch($key);
return $result === null ? false : $result;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
return zend_shm_cache_store($key, $value, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
* it could be something else.
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
* @return bool true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
return zend_shm_cache_fetch($key) === null ? $this->setValue($key, $value, $duration) : false;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return bool if no error happens during deletion
*/
protected function deleteValue($key)
{
return zend_shm_cache_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return bool whether the flush operation was successful.
*/
protected function flushValues()
{
return zend_shm_cache_clear();
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
use yii\base\InvalidConfigException;
use yii\caching\DbCache;
use yii\db\Migration;
/**
* Initializes Cache tables.
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 2.0.7
*/
class m150909_153426_cache_init extends Migration
{
/**
* @throws yii\base\InvalidConfigException
* @return DbCache
*/
protected function getCache()
{
$cache = Yii::$app->getCache();
if (!$cache instanceof DbCache) {
throw new InvalidConfigException('You should configure "cache" component to use database before executing this migration.');
}
return $cache;
}
/**
* {@inheritdoc}
*/
public function up()
{
$cache = $this->getCache();
$this->db = $cache->db;
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable($cache->cacheTable, [
'id' => $this->string(128)->notNull(),
'expire' => $this->integer(),
'data' => $this->binary(),
'PRIMARY KEY ([[id]])',
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function down()
{
$cache = $this->getCache();
$this->db = $cache->db;
$this->dropTable($cache->cacheTable);
}
}

View File

@@ -0,0 +1,22 @@
/**
* Database schema required by \yii\caching\DbCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0.7
*/
if object_id('[cache]', 'U') is not null
drop table [cache];
drop table if exists [cache];
create table [cache]
(
[id] varchar(128) not null,
[expire] integer,
[data] BLOB,
primary key ([id])
);

View File

@@ -0,0 +1,20 @@
/**
* Database schema required by \yii\caching\DbCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0.7
*/
drop table if exists `cache`;
create table `cache`
(
`id` varchar(128) not null,
`expire` integer,
`data` LONGBLOB,
primary key (`id`)
) engine InnoDB;

View File

@@ -0,0 +1,20 @@
/**
* Database schema required by \yii\caching\DbCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0.7
*/
drop table if exists "cache";
create table "cache"
(
"id" varchar(128) not null,
"expire" integer,
"data" BYTEA,
primary key ("id")
);

View File

@@ -0,0 +1,20 @@
/**
* Database schema required by \yii\caching\DbCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0.7
*/
drop table if exists "cache";
create table "cache"
(
"id" varchar(128) not null,
"expire" integer,
"data" bytea,
primary key ("id")
);

View File

@@ -0,0 +1,20 @@
/**
* Database schema required by \yii\caching\DbCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0.7
*/
drop table if exists "cache";
create table "cache"
(
"id" varchar(128) not null,
"expire" integer,
"data" BLOB,
primary key ("id")
);