简体   繁体   English

带有查询字符串的HTTP缓存URL

[英]HTTP Caching URLs with Query String

I am currently storing svg code in arrays in a php file that, in short, I am then referencing to throughout the site to retrieve the desired image. 我目前正在一个php文件中将svg代码存储在数组中,简而言之,我正在引用整个站点来检索所需的图像。

The file looks something like this: 该文件看起来像这样:

<?php
$svg_ticons = array(...);
$svg_icons = array(...);
$svg_inputs = array(
    'checkbox_0' => '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 60 60" enable-background="new 0 0 60 60" xml:space="preserve"><symbol id="Checkbox_Blank" viewBox="-7.5 -7.5 15 15"><path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" d="M-4.81 6.97h9.63c1.19 0 2.16-0.97 2.16-2.16v-9.63c0-1.19-0.97-2.16-2.16-2.16H-4.81c-1.19 0-2.16 0.97-2.16 2.16v9.63C-6.98 6-6 6.97-4.81 6.97L-4.81 6.97z"/><path fill="#333333" d="M4.82-7.5h-9.63c-1.48 0-2.68 1.21-2.68 2.69v9.63c0 1.48 1.2 2.69 2.68 2.69h9.63c1.48 0 2.68-1.21 2.68-2.69V-4.81C7.5-6.29 6.3-7.5 4.82-7.5zM-4.82 6.45c-0.9 0-1.64-0.73-1.64-1.64V-4.81c0-0.9 0.74-1.64 1.64-1.64h9.63c0.9 0 1.64 0.73 1.64 1.64v9.63c0 0.9-0.74 1.64-1.64 1.64H-4.82z"/></symbol><use xlink:href="#Checkbox_Blank" width="15" height="15" x="-7.5" y="-7.5" transform="matrix(4 0 0 -4 29.9997253 29.9989338)" overflow="visible"/></svg>'
);

$svgall = $svg_ticons+$svg_icons+$svg_inputs;
if(array_pop((explode("/", $_SERVER['SCRIPT_FILENAME']))) == array_pop((explode("/", __FILE__)))){
    if($reqsvg = $_GET["r"]?:false){header("Content-type: image/svg+xml"); echo $svgall[$reqsvg]; exit();}
}
?>

So the when I call to these images, let us say, as a background-image, it looks something like this: 所以,当我调用这些图像时,让我们说,作为背景图像,它看起来像这样:

background-image:url(DOMAIN/images/svg.php?r=checkbox_0);

Unfortunately, these 'images' are not being cached, which is causing a very undesirable delay time upon page load. 不幸的是,这些“图像”没有被缓存,这在页面加载时导致非常不希望的延迟时间。 So my question is: How can I cache from urls with query string? 所以我的问题是: 如何从带有查询字符串的网址缓存? (.htaccess solution is prefered.) (.htaccess解决方案是首选。)

Side Note: I am aware of this post . 旁注:我知道这篇文章 However, the answer marked is not my solution. 但是,标记的答案不是我的解决方案。

Edit : I am not simply asking how to enable caching. 编辑 :我不是简单地问如何启用缓存。 I am already aware of how to do that. 我已经知道如何做到这一点。 Parts of my site are caching beautifully. 我网站的部分内容都很漂亮。 I am looking to specifically cache from urls with a query string. 我期待从具有查询字符串的URL专门缓存。

What you want to do, if I understand correctly, is not possible without some awful hacks I can't even think of and would advise against anyway. 你想做什么,如果我理解正确的话,如果没有一些我甚至无法想到的糟糕的黑客行为是不可能的,并且无论如何都会提出建议。

The way caching works is, I believe, in a key - value storage system, where the key is the URI + the query string. 我相信,缓存的工作方式是在键值存储系统中,其中键是URI +查询字符串。 So the browser will save your image with a certain key, when there is an image requested with the same key the browser will use the cached version if it matches expiration criteria or will send a request to see if the file has been modified. 因此,浏览器将使用特定密钥保存您的图像,当使用相同密钥请求图像时,如果浏览器符合过期标准,浏览器将使用缓存版本,或者发送请求以查看文件是否已被修改。

In your case it seems like the r=checkbox_0 part of the query string is dynamic, meaning it changes it's value every time you refresh the page. 在您的情况下,查询字符串的r=checkbox_0部分似乎是动态的,这意味着每次刷新页面时它都会更改它的值。 You cannot cache this for the reason I mentioned above. 由于我上面提到的原因,您无法缓存此内容。 If you're getting a different query string each time, the browser will eventually end up caching a separate image every time. 如果每次都获得不同的查询字符串,浏览器最终每次都会缓存一个单独的图像。

IMO the best you can do is to use a static value for the r parameter, if that is possible. 你可以做的最好的事情就是为r参数使用静态值,如果可能的话。

Additionally, if you could cache images regardless of the query string then how would cache busting work :P 此外,如果您可以缓存图像而不管查询字符串,那么缓存破坏的工作方式如何:P

EDIT : 编辑

Here's the caching function, it takes in the time you want the cache to be active before expiring in seconds. 这是缓存功能,它需要您希望缓存在到期之前以秒为单位的时间。

function cache($sec) {
    if ($sec > 0) {
        header('Cache-Control: must-revalidate, max-age=' . (int)$sec);
        header('Pragma: cache');
        header('Expires: ' . str_replace('+0000', 'GMT', gmdate('r', time() + $sec)));
    } else {
        header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
        header('Pragma: no-cache'); // HTTP 1.0.
        header('Expires: 0'); // Proxies.
    }
}

It was recommended that I put headers in my php file ( php_nub_qq recommended a function & SuperJer recommended header('Expires: ' . gmdate('D, d MYH:i:s', strtotime('+1 hours')) . ' GMT'); ). 建议我把标题放在我的php文件中( php_nub_qq推荐一个函数SuperJer推荐标题('Expires:'。gmdate('D,d MYH:i:s',strtotime('+ 1 hours'))。 GMT'); )。

I was fairly certain that putting headers in this file was doing nothing for me & SuperJer 's code was breaking my images, though after messing with it the next morning with a fresh mind I finally figured out why. 我相当肯定在这个文件中放置标题对我来说什么都没做, SuperJer的代码破坏了我的图像,虽然第二天早上用新鲜的头脑弄乱了它后我终于明白了原因。

This is the code I entered: 这是我输入的代码:

header("Expires: ".gmdate("%D, %d %M %Y %H:%i:%s", strtotime("+30 days"))."GMT");

However, I forgot in order to call strtotime in my site, I must first set date_default_timezone_set(); 但是,我忘了为了在我的网站上调用strtotime ,我必须先设置date_default_timezone_set();

So the end result was: 所以最终的结果是:

date_default_timezone_set("America/Los_Angeles");
header("Expires: ".gmdate("%D, %d %M %Y %H:%i:%s", strtotime("+30 days"))."GMT");

However , upon further examination of Chrome's Network Developer Tool, I realized that SuperJer 's initial suspicion of it being my browser setting was correct. 但是 ,在进一步检查Chrome的网络开发者工具后,我意识到SuperJer 最初怀疑它是我的浏览器设置是正确的。 My file had been caching all along, I'd just had "Disable Cache" checked in my developer tools. 我的文件一直在缓存,我只是在我的开发人员工具中检查了“禁用缓存”。 (Guess I was a bit more exhausted than I thought, because I could have sworn I checked that.) (猜猜我比我想象的更疲惫,因为我可以发誓我检查过了。)

Summary : If your query string is not dynamic & other things (such as images) cache on your site; 摘要 :如果您的查询字符串不是动态的,那么您网站上的其他内容(如图像)就会缓存; close your eyes, take a nice long relaxing breath, & check all of your browser setting & do not forget your Dev Tool settings. 闭上眼睛,来一次长时间悠闲的气息,并检查所有浏览器设置: 不要忘记你的开发工具设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM