简体   繁体   English

php http标头

[英]php http headers

Was wondering a couple of things. 想知道几件事。

Does http headers cache everything on the page. http标头是否缓存页面上的所有内容。 And if i have some javascript files will it cache them as well for subsequent pages, or is it more complicated then that. 如果我有一些javascript文件,它会缓存它们以及后续页面,或者它更复杂。 Example: If I cache all javascript files on page1.php will the files still be cached on page2.php or does it cache files for page1.php only apply to page1.php. 示例:如果我在page1.php上缓存所有javascript文件,文件仍然会缓存在page2.php上,或者它是否缓存了page1.php的文件,只适用于page1.php。

The other question is... 另一个问题是......

Should I scrap http headers and just use APC and if so how complicated is it, or in fact is it possible to use both(asking cuz yslow says to use http headers). 我应该废弃http标头并只使用APC,如果是这样,它有多复杂,或者实际上可以同时使用它们(要求cuz yslow说使用http标头)。 Thanks for any info, Ive been reading but these questions weren't really answered in the text. 感谢任何信息,我一直在阅读,但这些问题在文中没有真正回答。

Your web server will take care of caching for you if you're just serving up regular .js files. 如果您只是提供常规.js文件,您的Web服务器将为您处理缓存。 The .js files will be downloaded the first time they are linked from one of your pages. .js文件将在第一次从您的某个页面链接时下载。 When the user re-loads that page, or goes to another page entirely that uses the same .js file, the browser will used the cached copy. 当用户重新加载该页面,或者完全转到另一个使用相同.js文件的页面时,浏览器将使用缓存副本。 This applies when you load scripts via <script src="code.js"></script> tags. 当您通过<script src="code.js"></script>标记加载脚本时,这适用。

That's if you have standalone, separate .js files. 如果您有独立的.js文件,那就是这样。 If, on the other hand, you JavaScript code buried in the HTML your PHP scripts generate, for example: 另一方面,如果您的PHP代码生成的HTML中隐藏了JavaScript代码,例如:

<script type="text/javascript">
  alert("Hello world!");
</script>

...these scripts will be re-generated each time your .php file is loaded. ...每次加载.php文件时都会重新生成这些脚本。 If you're looking to cache the output of your PHP scripts then you will need to manage caching yourself by setting the appropriate HTTP headers from your PHP scripts, be that via the Cache-Control family of headers or the If-Modified-Since and ETag style of headers. 如果您要缓存PHP脚本的输出,那么您需要通过从PHP脚本设置适当的HTTP标头来自己管理缓存,通过Cache-Control系列标头或If-Modified-SinceETag样式的标题。

Caching and PHP files don't generally go together, though, since you're usually generating dynamic content that changes based on user input, the time of day, cookies, etc. As caching is purely an optimization the general programming warning against premature optimization applies. 但是,缓存和PHP文件通常不会一起使用,因为您通常会生成基于用户输入,时间,Cookie等更改的动态内容。因为缓存纯粹是一种优化,因此针对过早优化的一般编程警告适用。 If you mess up your HTTP headers you can cause yourself a lot of headaches (believe me on that!). 如果你搞砸了你的HTTP标题,你可能会让自己感到很头疼(相信我!)。 As a rule of thumb, you can probably just let Apache or IIS take care of advanced HTTP things like this and only muck around with HTTP headers if you have a specific need to do so. 根据经验,您可以让Apache或IIS处理类似这样的高级HTTP事务,如果您有特殊需要 ,只能使用HTTP标头。

I think you're confusing the different types of caching. 我认为你混淆了不同类型的缓存。 You've talked about 3 or 4 very different things here. 你在这里谈到了3到4个非常不同的东西。

  1. browser caching -- any normal browser will cache images, JS files, and CSS files between pages. 浏览器缓存 - 任何普通的浏览器都会在页面之间缓存图像,JS文件和CSS文件。 Meaning, the second time a browser wants to display any particular image from your site, it will load it from it's local disk cache instead of going back to your server for it. 这意味着,浏览器第二次想要显示您站点中的任何特定图像时,它会从其本地磁盘缓存中加载它而不是返回到您的服务器。 All this stuff just happens -- don't mess around with it, and it just works. 所有这些都发生了 - 不要乱用它,它只是有效。 (exceptions: browsing user has turned off caching, you've changed headers to avoid caching, your mime.types aren't set up correctly so the browser doesn't treat these files correctly.) (例外:浏览用户已关闭缓存,您已更改标头以避免缓存,您的mime.types未正确设置,因此浏览器不会正确处理这些文件。)

  2. server-side content caching -- if your pages are rendering slowly ON THE SERVER, you can use various disk-and-RAM caching schemes to keep the output around, and prevent the server from having to render each page each time. 服务器端内容缓存 - 如果您的页面在服务器上缓慢呈现,您可以使用各种磁盘和RAM缓存方案来保持输出,并防止服务器每次都必须呈现每个页面。 This only works for fairly static sites or static parts of pages. 这仅适用于相当静态的站点或页面的静态部分。

  3. APC content caching -- APC has commands that let you stuff arbitrary content into a server-side RAM cache. APC内容缓存 - APC具有允许您将任意内容填充到服务器端RAM缓存中的命令。 If a piece of your system takes a long time to render, but can be reused by many server hits, this is a good choice. 如果你的系统需要花费很长时间来渲染,但可以被许多服务器命中重用,这是一个不错的选择。

  4. APC code caching -- Your text PHP scripts are "pseudo-compiled", then sent to the PHP runtime for execution. APC代码缓存 - 您的文本PHP脚本是“伪编译”,然后发送到PHP运行时执行。 This "pseudo-compile" stage can be very slow and is redundant, so APC caches the "psuedo-compiled" PHP stage in RAM. 这个“伪编译”阶段可能非常慢并且是冗余的,因此APC将“psuedo-compiled”PHP阶段缓存在RAM中。 It can speed up a whole website quite handily. 它可以非常方便地加速整个网站。

Sorry if this is TMI. 对不起,如果这是TMI。

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

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