Your app becomes faster for the users.
The cost is lower than non-caching system.
HTTP Caching
Page Caching
Fragment Caching
Low-level Caching
class HomeController < ApplicationController
def index
@schedules = Schedule.all
if stale? @schedules
respond_to do |format|
format.html
end
end
end
end
class HomeController < ApplicationController
caches_page :about
def about; end
end
/feedback?search=hello => /feedback
class BlogController < ApplicationController
def index
@articles = Article.publish
end
end
%h2 Blog
.grid_11
- cache @articles do
- @articles do |article|
%article
.date
.month= article.month
.year= article.year
%div
.title= article.title
.content= article.content
%div
- cache @article.comments do |comment|
%span= comment.name
.grid_4
%h4 Tìm kiếm
class Article < ActiveRecord::Base
def self.cached_published
Rails.cache.fetch [name, "publish"] do
published.to_a
end
end
end
class Article < ActiveRecord::Base
def self.cached_published
Rails.cache.fetch [name, "publish"] do
published.to_a
end
end
def flush_cache
Rails.cache.delete([self.class.name, "publish"])
end
end
class Article < ActiveRecord::Base
def self.cached_published
Rails.cache.fetch [name, "publish"] do
published.to_a
end
end
def flush_cache
Rails.cache.delete([self.class.name, "publish"])
end
end
Acticle.cached.published
Article.delete_cached.published
Built on top of Rails.cache
Support cache analytics
Clear cache without effect on other rails caches.
class User < ActionRecord::Base
def self.list_vip_users
cached {
where(:type => 'vip')
.order('rank')
}
end
end
HTTP Caching
Page Caching
Fragment Caching
Low-level Caching
Use HTTP caching as much as you can
Use Russian doll approach when rendering complex view
Use Rails.cache or Munna inside model to improve performance
Set expiring cache key for everything