dfe*_*ens 7 ruby caching ruby-on-rails ruby-on-rails-3
假设我有控制器home_controller.rb
有动作index
.
我想缓存索引页面,所以我正在做:
caches_page :index
Run Code Online (Sandbox Code Playgroud)
但是希望它只为未登录的用户缓存.如果我要做出有条件的:
caches_page :index, :if => :user_not_signed_in?
Run Code Online (Sandbox Code Playgroud)
页面将在首次未登录用户时缓存.现在,每个登录用户也会看到未登录的内容.有没有办法在不更改网址的情况下分离此操作?
你想要的东西无法实现;
页面已缓存或未缓存.该过程检查html文件的存在或处理它.
你还有两种选择:
使用动作缓存或片段缓存(不推荐)
更推荐:使用ajax加载用户特定的部分,这样你只需要一个页面总是被缓存并且动态插入特定的数据(比如stackoverflow)
以下是最先进的解释:http://railslab.newrelic.com/scaling-rails
cache_if和cache_unless似乎是现在执行此操作的正确方法:
cache_if(condition, name = {}, options = nil, &block)
cache_unless(condition, name = {}, options = nil, &block)
Run Code Online (Sandbox Code Playgroud)
你的代码:
<% cache_unless @user.changed?, [ @user, 'form' ] do %>