Final Class Yiisoft\Yii\Middleware\HttpCache
| Inheritance | Yiisoft\Yii\Middleware\HttpCache |
|---|---|
| Implements | Psr\Http\Server\MiddlewareInterface |
| Deprecated since version | Use `HttpCacheMiddleware` from `yiisoft/http-middleware` package instead. |
HttpCache implements client-side caching by utilizing the Last-Modified and ETag HTTP headers.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| process() | Yiisoft\Yii\Middleware\HttpCache | |
| withCacheControlHeader() | Returns a new instance with the specified value of the Cache-Control HTTP header. |
Yiisoft\Yii\Middleware\HttpCache |
| withEtagSeed() | Returns a new instance with the specified callable that generates the ETag seed string. | Yiisoft\Yii\Middleware\HttpCache |
| withLastModified() | Returns a new instance with the specified callable that generates the last modified UNIX timestamp. | Yiisoft\Yii\Middleware\HttpCache |
| withParams() | Returns a new instance with the specified extra parameters for ETag seed string generation. | Yiisoft\Yii\Middleware\HttpCache |
| withWeakEtag() | Returns a new instance with weak ETags generation enabled. Disabled by default. | Yiisoft\Yii\Middleware\HttpCache |
Method Details
| public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $handler | \Psr\Http\Server\RequestHandlerInterface | |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (
($this->lastModified === null && $this->etagSeed === null) ||
!in_array($request->getMethod(), [Method::GET, Method::HEAD], true)
) {
return $handler->handle($request);
}
/** @var int|null $lastModified */
$lastModified = $this->lastModified === null ? null : ($this->lastModified)($request, $this->params);
$etag = null;
if ($this->etagSeed !== null) {
/** @var string|null $seed */
$seed = ($this->etagSeed)($request, $this->params);
if ($seed !== null) {
$etag = $this->generateEtag($seed);
}
}
$response = $handler->handle($request);
if ($this->cacheControlHeader !== null) {
$response = $response->withHeader(Header::CACHE_CONTROL, $this->cacheControlHeader);
}
if ($etag !== null) {
$response = $response->withHeader(Header::ETAG, $etag);
}
$cacheIsValid = $this->validateCache($request, $lastModified, $etag);
if ($cacheIsValid) {
$response = $response->withStatus(Status::NOT_MODIFIED);
}
/** @see https://tools.ietf.org/html/rfc7232#section-4.1 */
if ($lastModified !== null && (!$cacheIsValid || $etag === null)) {
$response = $response->withHeader(
Header::LAST_MODIFIED,
gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
);
}
return $response;
}
Returns a new instance with the specified value of the Cache-Control HTTP header.
| public self withCacheControlHeader ( string|null $header ) | ||
| $header | string|null |
The value of the |
public function withCacheControlHeader(?string $header): self
{
$new = clone $this;
$new->cacheControlHeader = $header;
return $new;
}
Returns a new instance with the specified callable that generates the ETag seed string.
| public self withEtagSeed ( callable $etagSeed ) | ||
| $etagSeed | callable |
A PHP callback that generates the ETag seed string. The callback's signature should be:
Where |
public function withEtagSeed(callable $etagSeed): self
{
$new = clone $this;
$new->etagSeed = $etagSeed;
return $new;
}
Returns a new instance with the specified callable that generates the last modified UNIX timestamp.
| public self withLastModified ( callable $lastModified ) | ||
| $lastModified | callable |
A PHP callback that returns the UNIX timestamp of the last modification time. The callback's signature should be:
Where |
public function withLastModified(callable $lastModified): self
{
$new = clone $this;
$new->lastModified = $lastModified;
return $new;
}
Returns a new instance with the specified extra parameters for ETag seed string generation.
| public self withParams ( mixed $params ) | ||
| $params | mixed |
Extra parameters for {@see \Yiisoft\Yii\Middleware\withEtagSeed()} callbacks. |
public function withParams(mixed $params): self
{
$new = clone $this;
$new->params = $params;
return $new;
}
Returns a new instance with weak ETags generation enabled. Disabled by default.
You should use weak ETags if the content is semantically equal, but not byte-equal.
| public self withWeakEtag ( ) |
public function withWeakEtag(): self
{
$new = clone $this;
$new->weakEtag = true;
return $new;
}
Signup or Login in order to comment.