简体   繁体   English

如何使用codeigniter正确启用浏览器缓存?

[英]How do I correctly ENABLE browser caching using codeigniter?

Every time I do I search on this I get information about how to disable the browser cache. 每次我搜索这个,我都会得到有关如何禁用浏览器缓存的信息。 Never anything about enabling it. 从来没有任何关于启用它。

How do I get the back button to use the cache and not regenerate the page? 如何让后退按钮使用缓存而不重新生成页面?

As far as I know you can control to force a browser to reload the data by means of these meta tags: 据我所知,你可以控制强制浏览器通过这些元标记重新加载数据:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Expires" content="0">

but you cannot force it to read from cache. 但你不能强迫它从缓存中读取。 The browser itself will do that for you if you don't explicitly specify to ignore the cache, and the page data are in fact cached and not expired. 如果您没有明确指定忽略缓存,则浏览器本身将为您执行此操作,并且页面数据实际上已缓存且未过期。

This does not depend on CodeIgniter because it's client-side, but you might want to use the meta() function included in CI's html helper , which will simply output the corresponding meta tag. 这不依赖于CodeIgniter,因为它是客户端,但您可能希望使用CI的html帮助程序中包含的meta()函数,它只会输出相应的元标记。 eg: 例如:

echo meta('Cache-control', 'no-cache', 'http-equiv');

would generate the second code line above. 将生成上面的第二个代码行。

Note: 注意:

  1. The 1st meta tag is specified for http/1.0 while the 2nd one is for http/1.1 but both are used to allow backwards compatibility. 第一个元标记指定用于http / 1.0,而第二个元标记用于http / 1.1,但两者都用于允许向后兼容。

  2. If you're using xhtml instead of html remember to close the meta tags with /> 如果您使用的是xhtml而不是html,请记得使用/>关闭元标记

Browser caching has nothing to do with codeigniter. 浏览器缓存与codeigniter无关。 You can use html meta tags to instruct the browser specifically not to cache pages or you can set a cache expiry for an individual page like so: 您可以使用html元标记来指示浏览器专门不缓存页面,或者您可以为单个页面设置缓存过期,如下所示:

<meta http-equiv="expires" content="Mon, 10 Dec 2001 00:00:00 GMT" />

You could use a bit of php to drop tomorrows date in there. 您可以使用一些PHP来删除那里的明天日期。 The browser (depending on settings) will usually pull as much as it can from the cache automatically, including when clicking the back button - the cache for the back button will work the same as if you were coming in from any other link. 浏览器(取决于设置)通常会自动从缓存中提取尽可能多的内容,包括单击后退按钮时 - 后退按钮的缓存将与从任何其他链接进入时的工作方式相同。

You could set expires headers through your htaccess using something like the following on an apache server (you would have to ask about how to do this on other server types) to tell the browser that is should cache certain types of content for a given periods of time: 您可以在apache服务器上使用类似下面的内容通过htaccess设置expires头(您必须询问如何在其他服务器类型上执行此操作)告诉浏览器应该缓存特定类型的内容在给定的时间段内时间:

ExpiresByType text/html "access plus 60 seconds"

This will tell the browser to store anything of mime type text/html for 60 seconds (this includes codeigniter output) BUT DONT DO THIS if your dealing with dynamic content It will stop any dynamic page content being loaded and will stop any changes to your content being loaded by returning visitors (Obviously this second part is not such an issue with a 60 second cache). 这将告诉浏览器存储mime type text / html的任何内容60秒(这包括codeigniter输出) 但是如果你处理动态内容它会停止它将停止任何动态页面内容被加载并将停止对你的内容的任何更改由回访者加载(显然这第二部分不是60秒缓存的问题)。

The key thing to realise is that Your page is not one thing, it's made up of lots of parts, some of these parts should be called from cache (js, css, images, etc.) some should not (often html will fall into this category). 要实现的关键是你的页面不是一件事,它是由很多部分组成的,其中一些部分应该从缓存(js,css,图像等)调用,有些不应该(通常html会落入这个类别)。 The browser will automatically call all the parts of your page from the cache where the cache has not expired. 浏览器将自动从缓存尚未过期的缓存中调用页面的所有部分。

Usually you would use .htaccess (or similar method) to cache your css, images, etc. (using versioning in filenames to force a reload when they change). 通常你会使用.htaccess(或类似方法)来缓存你的css,图像等(使用文件名中的版本控制来强制重新加载)。

You should also take advantage of server side caching - codeigniter does this for whole pages but I dont tend to find this very helpful for any kind of dynamic site so I would take a look at for using phil sturgeons partial caching library for ci if you are interested in ss caching: 您还应该利用服务器端缓存 - codeigniter为整个页面执行此操作但我不倾向于发现这对任何类型的动态站点非常有用所以我会看看如果你是使用phil sturgeons部分缓存库的ci对ss缓存感兴趣:

https://github.com/philsturgeon/codeigniter-cache https://github.com/philsturgeon/codeigniter-cache

This wont stop a request being sent to the server but will mean that request requires less processing and can be served as one or several pieces of static content. 这不会停止向服务器发送请求,但意味着请求需要较少的处理,并且可以作为一个或多个静态内容提供。

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

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