2016-12-02 14:08:35 +05:30
|
|
|
<?php
|
2019-07-15 04:29:56 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-12-02 14:08:35 +05:30
|
|
|
namespace api\filters;
|
|
|
|
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ActionFilter;
|
|
|
|
|
|
|
|
class NginxCache extends ActionFilter {
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* @var array|callable array or callback, contains pairs of route => cache duration.
|
2016-12-02 14:08:35 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Duration can be set in 2-ways:
|
|
|
|
* - if the value starts with the @ prefix, it sets the absolute time
|
|
|
|
* in unix timestamp that the response can be cached to.
|
|
|
|
* - otherwise, the value is interpreted as the number of seconds
|
|
|
|
* for which the response must be cached
|
2016-12-02 14:08:35 +05:30
|
|
|
*/
|
|
|
|
public $rules;
|
|
|
|
|
|
|
|
public function afterAction($action, $result) {
|
|
|
|
$rule = $this->rules[$action->id] ?? null;
|
|
|
|
if ($rule !== null) {
|
|
|
|
if (is_callable($rule)) {
|
|
|
|
$cacheTime = $rule($action);
|
|
|
|
} else {
|
|
|
|
$cacheTime = $rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
Yii::$app->response->headers->set('X-Accel-Expires', $cacheTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::afterAction($action, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|