Final Class luya\admin\Bootstrap
Inheritance | luya\admin\Bootstrap |
---|---|
Implements | yii\base\BootstrapInterface |
Uses Traits | luya\traits\CacheableTrait |
Available since version | 2.0.0 |
Source Code | https://github.com/luyadev/luya-module-admin/blob/master/src/Bootstrap.php |
Admin Bootstrap
The main purpose of this Bootstrap files is to have an option to run the queue command trough a "fake cronjob".
Public Methods
Method | Description | Defined By |
---|---|---|
bootstrap() | Bootstrap method to be called during application bootstrap stage. | luya\admin\Bootstrap |
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 |
runQueueJob() | Evaluate whether the current queue job should be run or not. | luya\admin\Bootstrap |
setHasCache() | Store cache data for a specific key if caching is enabled in this application. | luya\traits\CacheableTrait |
Method Details
Bootstrap method to be called during application bootstrap stage.
public void bootstrap ( $app ) | ||
$app | yii\base\Application |
The application currently running |
public function bootstrap($app)
{
/** @var \luya\admin\Module $admin */
$admin = $app->getModule('admin');
// boot the queue job only if enabled and module available.
if ($admin && $admin->autoBootstrapQueue) {
$app->on(Application::EVENT_BEFORE_REQUEST, [$this, 'runQueueJob']);
}
}
Defined in: luya\traits\CacheableTrait::deleteHasCache()
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;
}
Defined in: luya\traits\CacheableTrait::flushHasCache()
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;
}
Defined in: luya\traits\CacheableTrait::getHasCache()
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;
}
Defined in: 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.
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;
}
Defined in: luya\traits\CacheableTrait::isCachable()
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;
}
Evaluate whether the current queue job should be run or not.
public void runQueueJob ( $event ) | ||
$event | yii\base\Event |
public function runQueueJob($event)
{
if (!$event->sender->request->isConsoleRequest) {
// use cache to ensure this will run only every 30min
$this->getOrSetHasCache(['admin', 'bootstrap', 'queue'], function () {
$timestamp = Config::get(Config::CONFIG_QUEUE_TIMESTAMP);
// if last execution has NOT been done previously (maybe trough a cronjob)
if ((time() - $timestamp) > (60*25)) {
Yii::debug('"Fake-Cronjob" queue run execution.');
Yii::$app->adminqueue->run(false);
Config::set(Config::CONFIG_QUEUE_TIMESTAMP, time());
}
return $timestamp;
}, 60*30);
}
}
Defined in: luya\traits\CacheableTrait::setHasCache()
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;
}