Laravel标题和缓存在php中 [英] Laravel Headers and Caching in php

查看:107
本文介绍了Laravel标题和缓存在php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a small image generator as part of my laravel4 application. It takes about 700ms to generate the image and so I have started caching the generated result on my server and returning that to the browser instead which saves some time.

作为图像生成后我永远不会改变我想告诉浏览器在本地缓存图像,我用以下代码完成了这个:

As the image will never change once generated I wanted to tell the browser to cache the image locally and I have done this with the following code:

$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的图像带有以下标题的浏览器:

This sends an image with status code 200 OK to the browser with the following headers:

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,未经过其他测试)仍然拒绝简单地抓取本地缓存版本,而是再次点击服务器。

My issue is that my browser (chrome, not tested in others) still refuses to simply grab the local cached version and instead hits the server again.

我花了大约半个小时搜索其他关于这个问题的问题以及所有问题都给了我答案,我已将其纳入上述代码中。因此,虽然我知道有类似的问题,但这个问题对于上述源代码是独一无二的。

I have spent about half an hour searching for other questions on this subject and all of them have given me answers which I have incorporated into the above code. So while I know that there are similar questions, this one is unique to the above source code.

我的问题是,我做错了会导致文件没有被浏览器缓存?

My question is, what am I doing wrong that would result in the file not being cached by the browser?

推荐答案

另一种方法是检查'If-Modified-Since'请求标头,因为只有在浏览器已有的情况下它才会出现文件。

An alternative method to this would be to check for the 'If-Modified-Since' request header as it will only be present if the browser already has the file.

如果它存在,那么您知道该文件已经创建并且可以通过链接进行响应,否则请运行上面的代码。这样的东西...

If it is present, then you know the file is already created and can respond with a link to it, otherwise run your code above. Something like this...

// 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() ); 
}

这篇关于Laravel标题和缓存在php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆