rails缓存-part2

上一部分介绍了页面缓存.这部分住要介绍Action缓存,Fragment缓存,ActiveRecord缓存
这几种缓存的效果为:
[code] 1. Page Caching - Fastest
2. Action Caching - Next Fastest
3. Fragment Caching - Least Fast[/code]

1Action缓存
 Action缓存和页面缓存很相似,但会被过滤拦截.
[code] class BlogController < ApplicationController
layout 'base'
before_filter :authenticate # <--- Check out my authentication
caches_action :list, :show[/code]
 执行list action请求后,打出的日志为:
[code] 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][/code]
 
 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中缓存部分代码片断.
[code] <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: [/code]/tmp/cache/localhost:3000/blog/list.cache
 从下的日志输出可以看出.缓存前后的执行的时间:
[code] 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][/code]
 不过从上面的日志可以看出执行了两次.需要去改变一下代码:
[code] def list
  unless read_fragment({})
  @post = Post.find(:all, :order => 'created_on desc', :limit => 10) %>
 end
 end[/code]

4Fragment缓存清除.和前面一样:
[code] expire_fragment(:controller => 'blog', :action => 'list')[/code]
 清楚多个页面缓存可以像下面那样:
[code] expire_fragment(:controller => 'blog', :action => 'list', :page => 1)
 expire_fragment(:controller => 'blog', :action => 'list', :page => 2)
 expire_fragment(:controller => 'blog', :action => 'list', :page => 3)[/code]
 更简单的方法为:
 expire_fragment(%r{blog/list.*})

5分页的片断缓存:
 在默认的情况下分页缓存的文件名像/localhost:3000/blog/list.cache.这样就导致覆盖了前面叶面的内容,这就需要去改变缓存文件的名字:
[code]  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[/code]
 我们可以这样写"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等

另外的命名例子
[code]  <% 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 %> [/code]
生成的缓存文件为:/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:中更改存储方式
[code]ActionController::Base.fragment_cache_store = :file_store, [/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值