Ruby on Rails页面缓存 实践_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Ruby on Rails页面缓存 实践

Ruby on Rails页面缓存 实践

 2012/12/16 17:21:36  chen_miao  程序员俱乐部  我要评论(0)
  • 摘要:三种方式PageCaching,ActionCaching和FragmentCaching缓存默认只在production环境下启动PageCachingcaches_page:public_content以URL为准expire_page:action=>"public_content"ActionCachingcaches_action:premium_content以URL为准expire_action:action=>"premium_content",:id=>
  • 标签:rails Ruby 缓存

?

三种方式 Page Caching, Action Caching和 Fragment Caching

缓存默认只在production 环境下启动

Page Caching
caches_page :public_content
以URL为准
expire_page :action =>"public_content"

Action Caching
caches_action :premium_content
以URL为准
?expire_action :action => "premium_content", :id => article
?Page Caching 和 Action Caching的实例:
class ContentController < ApplicationController
?before_filter :verify_premium_user, :except => :public_content
?caches_page :public_content
?caches_action :premium_content
?cache_sweeper :article_sweeper,
??????? :only => [ :create_article,
??????? :update_article,
??????? :delete_article ]
?def public_content
? @articles = Article.list_public
?end
?def premium_content
? @articles = Article.list_premium
?end
?private
?def verify_premium_user
? return
?? user = session[:user_id]
?? user = User.find(user)
? if user
?? unless user && user.active?
?? redirect_to :controller =>? "login", :action => "signup_new"
? end
?end
end

删除过期缓存内容:
app/models中加article_sweeper.rb
class ArticleSweeper < ActionController::Caching::Sweeper
?observe Article
?def after_create(article)
? expire_public_page
?end
?def after_update(article)
? expire_article_page(article.id)
?end
?def after_destroy(article)
? expire_public_page
? expire_article_page(article.id)
?end
?private
?def expire_public_page
? expire_page(:controller => "content", :action => 'public_content')
?end
?def expire_article_page(article_id)
? expire_action(:controller =>? "content",
??????? :action => "premium_content",
??????? :id => article_id)
?end
end
app/public/content/show/1默认caches_page文件路径
app/public/content/show/1.html


Fragment Caching
controller:
class Blog1Controller < ApplicationController
?def list
??@dynamic_content = Time.now.to_s
??unless read_fragment(:action => 'list')
???logger.info("Creating fragment")
???@articles = Article.find_recent
??end
?end
end
#read_fragment()查看一个action的fragment缓存是否存在
view:
<%= @dynamic_content %> <!-- 动态内容 -->
<% cache do %> <!-- 缓存开始-->
?<ul>
?<% for article in @articles -%>
??<li><p><%= h(article.body) %></p></li>
?<% end -%>
?</ul>
<% end %> <!-- 结束缓存 -->
<%= @dynamic_content %> <!-- 其它动态内容 -->

使用多个缓存段:
<% cache(:action => 'list', :part => 'articles') do %>
?<ul>
?<% for article in @articles -%>
??<li><p><%= h(article.body) %></p></li>
?<% end -%>
?</ul>
<% end %>
<% cache(:action => 'list', :part => 'counts') do %>
?<p>There are a total of <%= @article_count %> articles.</p>
<% end %>
#使用:part参数区分同一action下的不同缓存段

缓存过期
controller:
class Blog2Controller < ApplicationController
?def list
??@dynamic_content = Time.now.to_s
??@articles = Article.find_recent
??@article_count = @articles.size
?end
?def edit
??# 编辑文章时不更新统计缓存
??expire_fragment(:action => 'list', :part => 'articles')
??redirect_to(:action => 'list')
?end
?def delete
??#删除文章时同时删除两个缓存
??expire_fragment(:action => 'list', :part => 'articles')
??expire_fragment(:action => 'list', :part => 'counts')
??redirect_to(:action => 'list')
?end
end

expire_fragment()可接正则表达式
expire_fragment(%r{/blog2/list. })

Fragment Caching存储选项设置:
ActionController::Base.fragment_cache_store = <下面的选项之一>
ActionController::Caching::Fragments::FileStore.new(path)
ActionController::Caching::Fragments::DRbStore.new(url)
ActionController::Caching::Fragments::MemCachedStore.new(host)

片段缓存还可以这么写哦:

<%cache 'name' ?do %>

?

? ^^^^^^^

<%end%>

http://blog.chinaunix.net/uid-601661-id-2088387.html

发表评论
用户名: 匿名