我有一个脚本,它耗费了太多资源来提供每分钟只能检索的数据。有没有办法通过.htaccess文件配置Apache 2来指定标题告诉客户端将脚本结果保留在其缓存中一分钟?
我知道这可以通过脚本本身完成,但我想通过网络服务器的配置来完成。
答案 0 :(得分:1)
mod_expires是一个很好的解决方案。
如果您使用的是类Unix系统:
a2enmod expires
apache2ctl restart
然后,您将能够定义给定文件的到期条件,或者通过.htaccess文件根据mimetype定义缓存策略。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A3600
<Files scriptToCache.php>
ExpiresDefault A60
</Files>
</IfModule>
此处“A3600”表示文件在访问后3600秒到期。
此处提供更多信息:http://httpd.apache.org/docs/2.0/mod/mod_expires.html
答案 1 :(得分:0)
简短回答是否定的 - 你需要从脚本中返回缓存标题。
答案 2 :(得分:0)
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FilesMatch>
</IfModule>