Trait luya\traits\CacheableTrait
Cacheable trait allows caching whether application has caching enabled or not.
implementation example:
$cacheKey = 'foobar';
$cache = $this->getHasCache($cacheKey);
if ($cache === false) {
// execute code and save cache data into variable
$cache = "Hello World";
$this->setHasCache($cacheKey, $cache); // variable $cache has been changed from above
}
return $cache;
An example for a simple cache query dependency
$this->setHasCache('myCacheKey', $data, new DbDependency(['sql' => 'SELECT MAX(id) FROM admin_storage_folder WHERE is_deleted=0']), 0);
You can also use an array notation in order to generate cache dependency:
$dependency = [
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT max(timestamp) FROM table WHERE id=:id',
'params' => [':id' => Yii::$app->request->get('id')],
];
$this->setHasCache(['my', 'key'], $data, $dependency);
Public Methods
Method | Description | Defined By |
---|---|---|
deleteHasCache() | Remove a value from the cache if caching is enabled. | luya\traits\CacheableTrait |
flushHasCache() | Deletes all values from cache. | luya\traits\CacheableTrait |
getHasCache() | Get the caching data if caching is allowed and there is any data stored for this key. | luya\traits\CacheableTrait |
getOrSetHasCache() | Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key. | luya\traits\CacheableTrait |
isCachable() | Check if the current configuration of the application and the property allows a caching of the language container data. | luya\traits\CacheableTrait |
setHasCache() | Store cache data for a specific key if caching is enabled in this application. | luya\traits\CacheableTrait |
Method Details
Remove a value from the cache if caching is enabled.
public boolean deleteHasCache ( $key ) | ||
$key | string|array |
The cache identifier |
return | boolean |
Whether delete of key has been success or not |
---|
public function deleteHasCache($key)
{
if ($this->isCachable()) {
return Yii::$app->cache->delete($key);
}
return false;
}
Deletes all values from cache.
public boolean flushHasCache ( ) | ||
return | boolean |
Whether the flush operation was successful. |
---|
public function flushHasCache()
{
if ($this->isCachable()) {
return Yii::$app->cache->flush();
}
return false;
}
Get the caching data if caching is allowed and there is any data stored for this key.
public mixed|boolean getHasCache ( $key ) | ||
$key | string|array |
The identifiere key, can be a string or an array which will be calculated. |
return | mixed|boolean |
Returns the data, if not found returns false. |
---|
public function getHasCache($key)
{
if ($this->isCachable()) {
$data = Yii::$app->cache->get($key);
$enumKey = (is_array($key)) ? implode(",", $key) : $key;
if ($data) {
Yii::debug("Cacheable trait key '$enumKey' successfully loaded from cache.", __METHOD__);
return $data;
}
Yii::debug("Cacheable trait key '$enumKey' has not been found in cache.", __METHOD__);
}
return false;
}
Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key.
Usage example:
use CacheableTrait;
public function getTopProducts($count = 10)
{
return $this->getOrSetHasCache(['top-n-products', 'n' => $count], function ($cache) use ($count) {
return Products::find()->mostPopular()->limit(10)->all();
}, 1000);
}
public mixed getOrSetHasCache ( $key, Closure $closure, $duration = null, $dependency = null ) | ||
$key | mixed |
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. |
$closure | Closure |
The closure that will be used to generate a value to be cached.
In case $closure returns |
$duration | integer |
Default duration in seconds before the cache will expire. If not set, defaultDuration value will be used. 0 means never expire. |
$dependency | yii\caching\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 |
return | mixed |
Result of $closure execution |
---|
public function getOrSetHasCache($key, \Closure $closure, $duration = null, $dependency = null)
{
if (($value = $this->getHasCache($key)) !== false) {
return $value;
}
$value = call_user_func($closure, $this);
$this->setHasCache($key, $value, $dependency, $duration);
return $value;
}
Check if the current configuration of the application and the property allows a caching of the language container data.
public boolean isCachable ( ) |
public function isCachable()
{
if ($this->_cachable === null) {
$this->_cachable = Yii::$app->has('cache') ? true : false;
}
return $this->_cachable;
}
Store cache data for a specific key if caching is enabled in this application.
public boolean setHasCache ( $key, $value, $dependency = null, $cacheExpiration = null ) | ||
$key | string|array |
The identifier key or a array to store complex keys |
$value | mixed |
The value to store in the cache component. |
$dependency | yii\caching\Dependency|array |
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. You can also define an array with defintion which will generate the Object instead of object is provided. |
$cacheExpiration |
Integer The time in seconds before the cache data expires, 0 means never expire. |
|
return | boolean |
Whether set has been success or not |
---|
public function setHasCache($key, $value, $dependency = null, $cacheExpiration = null)
{
if ($this->isCachable()) {
if (is_array($dependency)) {
$dependency = Yii::createObject($dependency);
}
return Yii::$app->cache->set($key, $value, is_null($cacheExpiration) ? $this->_cacheExpiration : $cacheExpiration, $dependency);
}
return false;
}