上一部分介绍了页面缓存.这部分住要介绍Action缓存,Fragment缓存,ActiveRecord缓存
这几种缓存的效果为:
1. Page Caching - Fastest
2. Action Caching - Next Fastest
3. Fragment Caching - Least Fast
1Action缓存
Action缓存和页面缓存很相似,但会被过滤拦截.
class BlogController < ApplicationController
layout 'base'
before_filter :authenticate # <--- Check out my authentication
caches_action :list, :show
执行list action请求后,打出的日志为:
Processing BlogController#list (for 127.0.0.1 at 2007-03-04 12:51:24) [GET]
Parameters: {"action"=>"list", "controller"=>"blog"}
Checking Authentication
Post Load (0.000000) SELECT * FROM posts ORDER BY created_on LIMIT 10
Rendering blog/list
Cached fragment: localhost:3000/blog/list (0.00000)
Completed in 0.07800 (12 reqs/sec) | Rendering: 0.01600 (20%) | DB: 0.00000 (0%) | 200 OK [http://localhost/blog/list]
Action缓存生成的缓存文件存放在/tmp/cache/目录
同时注意before_filters必须要放在caches_action前面
2Action缓存清除
像页面缓存那样,只需要把"expire_page"该为"expire_action"在/app/sweepers/blog_sweeper.rb.文件中
一次清楚Action Cache 和 Fragment cache的命令:rake tmp:cache:clear
3Fragment缓存
Fragment缓存允许你在view中缓存部分代码片断.
<strong>My Blog Posts</strong>
<% cache do %>
<ul>
<% for post in @posts %>
<li><%= link_to post.title, :controller => 'blog', :action => 'show', :id => post %></li>
<% end %>
</ul>
<% end %>
The "cache do" will create a fragment cache:
/tmp/cache/localhost:3000/blog/list.cache
从下的日志输出可以看出.缓存前后的执行的时间:
Processing BlogController#list (for 127.0.0.1 at 2007-03-17 22:02:16) [GET]
Authenticating User
Post Load (0.000230) SELECT * FROM posts
Rendering blog/list
Cached fragment: localhost:3000/blog/list (0.00267)
Completed in 0.02353 (42 reqs/sec) | Rendering: 0.01286 (54%) | DB: 0.00248 (10%) | 200 OK [http://localhost/blog/list]
Processing BlogController#list (for 127.0.0.1 at 2007-03-17 22:02:17) [GET]
Authenticating User
Post Load (0.000219) SELECT * FROM posts
Rendering blog/list
Fragment read: localhost:3000/blog/list (0.00024)
Completed in 0.01530 (65 reqs/sec) | Rendering: 0.00545 (35%) | DB: 0.00360 (23%) | 200 OK [http://localhost/blog/list]
不过从上面的日志可以看出执行了两次.需要去改变一下代码:
def list
unless read_fragment({})
@post = Post.find(:all, :order => 'created_on desc', :limit => 10) %>
end
end
4Fragment缓存清除.和前面一样:
expire_fragment(:controller => 'blog', :action => 'list')
清楚多个页面缓存可以像下面那样:
expire_fragment(:controller => 'blog', :action => 'list', :page => 1)
expire_fragment(:controller => 'blog', :action => 'list', :page => 2)
expire_fragment(:controller => 'blog', :action => 'list', :page => 3)
更简单的方法为:
expire_fragment(%r{blog/list.*})
5分页的片断缓存:
在默认的情况下分页缓存的文件名像/localhost:3000/blog/list.cache.这样就导致覆盖了前面叶面的内容,这就需要去改变缓存文件的名字:
def list
unless read_fragment({:page => params[:page] || 1}) # Add the page param to the cache naming
@post_pages, @posts = paginate :posts, :per_page => 10
end
end
我们可以这样写"read_fragment({:controller => 'blog', :action => 'list', :page => params[:page] || 1})" 但是默认情况下自动添加了这两个参数,就不再需要我门去添加了.
在/views/blog/list.rhtml中
<% cache ({:page => params[:page] || 1}) do %>
... All of the html to display the posts ...
<% end %>
运行后缓存的文件名就是/localhost:3000/blog/list.page=1.cache等
另外的命名例子
<% cache (:controller => "base", :action => "user_tasks", :user_id => session[:user_id]) do %>
<ul>
<% for task in Task.find_by_member_id(session[:user_id]) %>
<li><%= task.name %></li>
<% end %>
</ul>
<% end %>
生成的缓存文件为:/localhost:3000/base/user_tasks.user_id=1.cache.
cache ("turkey") => "/tmp/cache/turkey.cache"
cache (:controller => 'blog', :action => 'show', :id => 1) => "/tmp/cache/localhost:3000/blog/show/1.cache"
cache ("blog/recent_posts") => "/tmp/cache/blog/recent_posts.cache"
cache ("#{request.host_with_port}/blog/recent_posts") => "/tmp/cache/localhost:3000/blog/recent_posts.cache"
6Action/Fragment Cache缓存存储方式:
页面缓存只能存储在文件中.Action/Fragment Cache有多中存储方式:
1:文件存储
2:内存存储
3:DRb存储
4:MemCache存储
在config/environment.rb:中更改存储方式
ActionController::Base.fragment_cache_store = :file_store,