php - PHP 中的 Laravel header 和缓存

标签 php http-headers laravel laravel-4 http-caching

我有一个小型图像生成器作为我的 laravel4 应用程序的一部分。生成图像大约需要 700 毫秒,因此我开始在我的服务器上缓存生成的结果并将其返回给浏览器,这样可以节省一些时间。

由于图像一旦生成就永远不会改变,我想告诉浏览器在本地缓存图像,我使用以下代码完成了此操作:

$path = $cacheFolderPath . $cacheFileName;

if (File::exists( $path )){
    $response = Response::make(File::get($path));
    $response->header('Content-Type', 'image/png');
    $response->header('Content-Disposition', 'inline; filename="'.$cacheFileName.'"');
    $response->header('Content-Transfer-Encoding', 'binary');
    $response->header('Cache-Control', 'public, max-age=10800, pre-check=10800');
    $response->header('Pragma', 'public');
    $response->header('Expires', date(DATE_RFC822,strtotime(" 2 day")) );
    $response->header('Last-Modified', date(DATE_RFC822, File::lastModified($path)) );
    $response->header('Content-Length', filesize($path));
    return $response;
}

这会将状态代码为 200 OK 的图像发送到具有以下 header 的浏览器:

Cache-Control:max-age=10800, pre-check=10800, public
Connection:Keep-Alive
Content-Disposition:inline; filename="pie_0_normal.png"
Content-Length:2129
Content-Transfer-Encoding:binary
Content-Type:image/png
Date:Wed, 07 Aug 2013 10:29:20 GMT
Expires:Fri, 09 Aug 13 10:29:20 +0000
Keep-Alive:timeout=5, max=93
Last-Modified:Wed, 07 Aug 13 10:14:42 +0000
Pragma:public
Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
Set-Cookie:laravel_session=767487mhf6j2btv3k01vu56174; expires=Wed, 07-Aug-2013 12:29:20 GMT; path=/; httponly
X-Powered-By:PHP/5.4.7

我的问题是我的浏览器(chrome,未在其他浏览器中测试)仍然拒绝简单地获取本地缓存版本,而是再次访问服务器。

我花了大约半小时搜索关于这个主题的其他问题,所有这些问题都给了我答案,我已将这些答案合并到上面的代码中。所以虽然我知道有类似的问题,但这个问题是上述源代码所独有的。

我的问题是,我做错了什么会导致浏览器不缓存文件?

最佳答案

另一种方法是检查“If-Modified-Since”请求 header ,因为它仅在浏览器已有文件时才会出现。

如果它存在,那么您就知道该文件已经创建并且可以使用指向它的链接进行响应,否则运行上面的代码。像这样的……

// check if the client validating cache and if it is current
if ( isset( $headers['If-Modified-Since'] ) && ( strtotime( $headers['If-Modified-Since'] ) == filemtime( $image->get_full_path() ) ) ) {

    // cache IS current, respond 304
    header( 'Last-Modified: ' . $image->get_last_modified(), true, 304 );

} else {

    // not cached or client cache is older than server, respond 200 and output

    header( 'Last-Modified: ' . $image->get_last_modified(), true, 200 );
    header( 'Content-Length: ' . $image->get_filesize() );
    header( 'Cache-Control: max-age=' . $image->get_expires() );
    header( 'Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + $image->get_expires() ) );
    header( 'Content-Type: image/jpeg');

    print file_get_contents( $image->get_full_path() ); 
}

关于php - PHP 中的 Laravel header 和缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101176/

相关文章:

php - Laravel 5.4 - php artisan 缓存 :clear does not clear cache files when using 'file' cache driver

php - 请求的授权 token 是什么? (Youtube API)

javascript - 使用 PHP、javascript 和 HTML 的 MySQL 安全性?

ios - ios中的状态码400错误

php - XAMPP 正在删除带有下划线的 HTTP header

php - Laravel 字符串验证以允许空字符串

javascript - 我无法将发布的数据添加到从 PHP 中执行的 javascript 发送的数据库中

angular - 将来自 IIS 的传入请求 header 获取到我的 Angular 应用程序中

拉维尔 5.6。如何测试 JSON/JSONb 列

php - Laravel 标签带有 和 查询