Better caching
This commit is contained in:
parent
4b5c13519c
commit
315c5d074e
2 changed files with 29 additions and 9 deletions
|
@ -529,12 +529,9 @@ class PostController
|
||||||
if (!is_readable($path))
|
if (!is_readable($path))
|
||||||
throw new SimpleException('Thumbnail file is not readable');
|
throw new SimpleException('Thumbnail file is not readable');
|
||||||
|
|
||||||
$ttl = 60 * 60 * 24 * 3;
|
$this->context->transport->cacheDaysToLive = 30;
|
||||||
\Chibi\HeadersHelper::set('Pragma', 'public');
|
|
||||||
\Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl);
|
|
||||||
\Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
|
|
||||||
|
|
||||||
$this->context->transport->mimeType = 'image/png';
|
$this->context->transport->mimeType = 'image/png';
|
||||||
|
$this->context->transport->fileHash = 'thumb' . $post->file_hash;
|
||||||
$this->context->transport->filePath = $path;
|
$this->context->transport->filePath = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -568,13 +565,12 @@ class PostController
|
||||||
$ext);
|
$ext);
|
||||||
$fn = preg_replace('/[[:^print:]]/', '', $fn);
|
$fn = preg_replace('/[[:^print:]]/', '', $fn);
|
||||||
|
|
||||||
$ttl = 60 * 60 * 24;
|
$ttl = 60 * 60 * 24 * 14;
|
||||||
\Chibi\HeadersHelper::set('Pragma', 'public');
|
|
||||||
\Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl);
|
|
||||||
\Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
|
|
||||||
|
|
||||||
|
$this->context->transport->cacheDaysToLive = 14;
|
||||||
$this->context->transport->customFileName = $fn;
|
$this->context->transport->customFileName = $fn;
|
||||||
$this->context->transport->mimeType = $post->mimeType;
|
$this->context->transport->mimeType = $post->mimeType;
|
||||||
|
$this->context->transport->fileHash = 'post' . $post->file_hash;
|
||||||
$this->context->transport->filePath = $path;
|
$this->context->transport->filePath = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,24 @@ if (!empty($this->context->transport->errorMessage))
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
$lastModified = filemtime($this->context->transport->filePath);
|
||||||
|
$eTag = $this->context->transport->fileHash;
|
||||||
|
$ttl = $this->context->transport->cacheDaysToLive * 24 * 3600;
|
||||||
|
|
||||||
|
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||||
|
? $_SERVER['HTTP_IF_MODIFIED_SINCE']
|
||||||
|
: false;
|
||||||
|
|
||||||
|
$eTagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH'])
|
||||||
|
? trim(trim($_SERVER['HTTP_IF_NONE_MATCH']), '"')
|
||||||
|
: false;
|
||||||
|
|
||||||
|
\Chibi\HeadersHelper::set('ETag', '"' . $eTag . '"');
|
||||||
|
\Chibi\HeadersHelper::set('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $lastModified));
|
||||||
|
\Chibi\HeadersHelper::set('Pragma', 'public');
|
||||||
|
\Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl);
|
||||||
|
\Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
|
||||||
|
|
||||||
if (isset($this->context->transport->customFileName))
|
if (isset($this->context->transport->customFileName))
|
||||||
{
|
{
|
||||||
\Chibi\HeadersHelper::set(
|
\Chibi\HeadersHelper::set(
|
||||||
|
@ -17,6 +35,12 @@ else
|
||||||
'Content-Type',
|
'Content-Type',
|
||||||
$this->context->transport->mimeType);
|
$this->context->transport->mimeType);
|
||||||
|
|
||||||
|
if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag)
|
||||||
|
{
|
||||||
|
header('HTTP/1.1 304 Not Modified');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
readfile($this->context->transport->filePath);
|
readfile($this->context->transport->filePath);
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue