我的Rails应用程序在VM上运行.静态页面的VM性能很好.事实上,我正在使用Apache虚拟主机运行另一个站点,该站点只提供静态 HTML文件,响应就足够了.但是,我动态生成 XML文件的Rails应用程序响
配置要缓存的这些XML文件的最佳做法是什么?
编辑1:
我应该提到浏览器不会查看这些XML文件.移动应用程序在“字段”中查看它们.因此,遗憾的是发送“HTTP / 1.0 304未修改”将无法正常工作.
编辑2:
如果重要,我正在使用Phusion Passenger来托管我的Rails应用程序.
如果您正在使用rails的静态页面缓存并通过apache提供服务,那么只需在URL上使用显式的xml扩展即可.如果您只提供xml而且没有html,那么在查找缓存文件时,您也可以将apache conf编辑为默认为xml而不是html.
缓存到期对于代码和测试来说是一件相当无聊的事情,但由于您很少重新生成文件,因此您可能只会使整个缓存失效.
这是一个精心挑选的文件和摘录,来自我如何在一个很小的,很少更新的rails网站中处理缓存:
在要缓存的控制器中
class XmlThingController < ApplicationController caches_page :index, :show, :other_actions
在修改可能导致xmls更改的数据的控制器/操作中:
class Admin::SomeCrudController < AppplicationController cache_sweeper :stupid_master_sweeper, :only => [ :save, :destroy ]
在’config / environments / production.rb’中
config.action_controller.page_cache_directory = File.join(RAILS_ROOT, 'public', 'cache')
在您的vhost apache conf中的某个地方:
# 1.4. Handle caching # 1.4.1. prevent direct cache access RewriteRule ^/cache - [F,L] # 1.4.2. for index RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f RewriteRule ^/?$/cache/index.html [L] # 1.4.3. for explicitly specified extensions RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI} -f RewriteRule ^(.*)$/cache$1 [L] # 1.4.4. with html extension implied RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f RewriteRule ^(.*)$/cache$1.html [L] # 1.5. Finally, proxy everything else to mongrel RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$balancer://your-website-proxy%{REQUEST_URI} [P,QSA,L]
愚蠢的清扫工,每当它被触发时清除整个缓存:
class StupidMasterSweeper < ActiveRecord::Observer observe Foo, Bar # All models that affect caching here def after_save(record); end def after_destroy(record); end def filter(controller) # sweep everything. `cd #{RAILS_ROOT} && RAILS_ENV=#{ENV['RAILS_ENV']} rake cache:clear` end end
LIB /任务/ cache.rake
namespace :cache do desc "Remove all cached files" task :clear do puts `rm -rf #{RAILS_ROOT}/public/cache/*` end end
如果您希望将默认隐含扩展名默认为xml,请更改1.4.2索引规则的扩展名,以及以下内容:
# 1.4.4. with html extension implied RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f RewriteRule ^(.*)$/cache$1.html [L]
至:
# 1.4.4. with xml extension implied RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.xml -f RewriteRule ^(.*)$/cache$1.xml [L]