SlideShare a Scribd company logo
Key-basedcacheexpirationin
Rails4
...or Rails 3 with a dependency
Wednesday, July 31, 13
BuildingBlocks(ActiveRecord::Base)
@product.cache_key
=>“products/1281-20130717091609”
“#{name}/#{id}-#{timestamp}”
Wednesday, July 31, 13
BuildingBlocks
(ActionView::Helpers::CacheHelper)
- cache @product do
%h1= @product.name
= image_tag @product.image.url
Wednesday, July 31, 13
Write fragment
views/products/1-20130728191246157664000
(“views/#{cache_key}”)
- cache @product do
%h1= @product.name
= image_tag @product.image.url
Wednesday, July 31, 13
Youcancomposefragmentkeys,too...
- cache [@product, “reviews”] do
%h1= @product.name
= image_tag @product.image.url
Write fragment
views/products/1-20130728191246157664000/reviews
“views/#{cache_key}/#{extra_args.join(‘/’)}”
Wednesday, July 31, 13
“ButwhatifI
change
atemplate?”
Wednesday, July 31, 13
Rails.cache.clear
Wednesday, July 31, 13
“ButwhatifI
deploy5times
aday”
Wednesday, July 31, 13
sucks
Wednesday, July 31, 13
Justkidding!™
Wednesday, July 31, 13
# Gemfile
gem ‘pg’
gem‘cache_digests’
gem ‘haml-rails’
gem ‘cancan’
# ...
(Rails 3 only – built into actionpack 4)
Wednesday, July 31, 13
Write fragment
views/products/1-20130728191246157664000/3b2b93d56d331aa7a1bf9267a656d3c9
(“views/#{cache_key}/#{template_digest}”)
- cache @product do
%h1= @product.name
= image_tag @product.image.url
Withcache_digests...
note: template_digest is a digest of the current template and all of it’s dependencies (more on that later)
Wednesday, July 31, 13
Cache digest for products/_product.html: 42eb891316f21e68798de5d5aa8021d5
Read fragment
views/products/1-20130728191246157664000/42eb891316f21e68798de5d5aa8021d5
Write fragment
views/products/1-20130728191246157664000/42eb891316f21e68798de5d5aa8021d5
- cache @product do
%h1= @product.name
- # image_tag @product.image.url
...changethetemplate
Wednesday, July 31, 13
templatechange
->newdigest
->newfragmentkey
->bustedcache!
Wednesday, July 31, 13
Andwedidn’t
havetodo
anything!
Wednesday, July 31, 13
“Russiandoll”cachingstrategy
thenotorious
Wednesday, July 31, 13
class Product < ActiveRecord::Base
has_many :reviews
end
class Review < ActiveRecord::Base
belongs_to :product, touch: true
end
Product’s cache key now dependent on its reviews
Wednesday, July 31, 13
@review.update!(rating: 3)
@review.product’s key gets bumped as well
Wednesday, July 31, 13
the “russian doll”...
# products/index.haml
- cache [@products.order(“updated_at ASC”).last, “index”] do
%ul.products
- @products.each do |product|
= render product
# products/_product.haml
- cache product do
%li.product
%h1= product.name
%h2 Reviews!
%ul.reviews
- product.reviews.each do |review|
= render review
# reviews/_review.haml
- cache review do
%li.review
%h4= “#{review.rating} stars!”
%p.content= review.content
Wednesday, July 31, 13
the “russian doll”...
index
product
reviews list
review
review
review
Wednesday, July 31, 13
@product.reviews.last.update!(rating: 5)
Last review will get re-rendered (and cached)
and container (review list) will get busted -
but when list re-renders all but this last
review can be fetched from cache.
Wednesday, July 31, 13
View the template dependency tree...
$ rake cache_digests:nested_dependencies TEMPLATE=your/template/name
ex:
and rake task for Rails 4: https://gist.github.com/nzaillian/6127613
Wednesday, July 31, 13
Explicit dependency
If Rails just can’t seem to figure out your dependency you can declare an explicit
dependency in a template by including a comment with a special form:
- # Template Dependency: reviews/summary
(...but you shouldn’t ever have to do this)
Wednesday, July 31, 13
OtherResources
https://github.com/rails/cache_digests
http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works
http://www.youtube.com/watch?v=yhseQP52yIY&t=0:39:39
mustwatch!(DHH’sRailsconf2013outlineofkey-basedcaching)
http://api.rubyonrails.org/classes/ActionView/Helpers/CacheHelper.html
Wednesday, July 31, 13

More Related Content

PDF
PPSX
XQDT - XQuery Getting Momentum in Eclipse
PDF
Ujs in rails
PPTX
MapReduce in Simple Terms
PDF
Real World Caching with Ruby on Rails
PPTX
Ror caching
PDF
Ride on the Fast Track of Web with Ruby on Rails- Part 2
PDF
Caching in rails
XQDT - XQuery Getting Momentum in Eclipse
Ujs in rails
MapReduce in Simple Terms
Real World Caching with Ruby on Rails
Ror caching
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Caching in rails

Similar to Caching strategies in Rails 4 (20)

PPTX
Rails caching
PDF
The Rails Way
PDF
Rails 4.0
PDF
RubyOnRails-Cheatsheet-BlaineKendall
PDF
RubyOnRails-Cheatsheet-BlaineKendall
ODP
Introduction to rails 4 v1
PDF
Rails Caching: Secrets From the Edge
PDF
Rails Caching Secrets from the Edge
PDF
Rails for Beginners - Le Wagon
PDF
Rupicon 2014 caching
PDF
Rupicon 2014 caching
PPTX
Accelerating Rails with edge caching
PPTX
Catalog display
PDF
Introduction to Ruby On Rails
PPT
Jasig rubyon rails
PPT
Jasig rubyon rails
PDF
What's new in Rails 4
PDF
Resource and view
PDF
Caching/Expiring in Rails
PDF
Migrating Legacy Rails Apps to Rails 3
Rails caching
The Rails Way
Rails 4.0
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
Introduction to rails 4 v1
Rails Caching: Secrets From the Edge
Rails Caching Secrets from the Edge
Rails for Beginners - Le Wagon
Rupicon 2014 caching
Rupicon 2014 caching
Accelerating Rails with edge caching
Catalog display
Introduction to Ruby On Rails
Jasig rubyon rails
Jasig rubyon rails
What's new in Rails 4
Resource and view
Caching/Expiring in Rails
Migrating Legacy Rails Apps to Rails 3
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Ad

Caching strategies in Rails 4