Better caching

This commit is contained in:
Marcin Kurczewski 2013-10-19 13:00:03 +02:00
parent 4b5c13519c
commit 315c5d074e
2 changed files with 29 additions and 9 deletions

View file

@ -529,12 +529,9 @@ class PostController
if (!is_readable($path))
throw new SimpleException('Thumbnail file is not readable');
$ttl = 60 * 60 * 24 * 3;
\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 = 30;
$this->context->transport->mimeType = 'image/png';
$this->context->transport->fileHash = 'thumb' . $post->file_hash;
$this->context->transport->filePath = $path;
}
@ -568,13 +565,12 @@ class PostController
$ext);
$fn = preg_replace('/[[:^print:]]/', '', $fn);
$ttl = 60 * 60 * 24;
\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));
$ttl = 60 * 60 * 24 * 14;
$this->context->transport->cacheDaysToLive = 14;
$this->context->transport->customFileName = $fn;
$this->context->transport->mimeType = $post->mimeType;
$this->context->transport->fileHash = 'post' . $post->file_hash;
$this->context->transport->filePath = $path;
}

View file

@ -6,6 +6,24 @@ if (!empty($this->context->transport->errorMessage))
}
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))
{
\Chibi\HeadersHelper::set(
@ -17,6 +35,12 @@ else
'Content-Type',
$this->context->transport->mimeType);
if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag)
{
header('HTTP/1.1 304 Not Modified');
exit;
}
readfile($this->context->transport->filePath);
flush();
}