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

Similar to Caching strategies in Rails 4

elasticsearch basics workshop
elasticsearch basics workshopelasticsearch basics workshop
elasticsearch basics workshop
Mathieu Elie
 
Data Modeling with Neo4j
Data Modeling with Neo4jData Modeling with Neo4j
Data Modeling with Neo4j
Neo4j
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshop
Arjen Miedema
 
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
Anna Dahlström
 
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Alessandro Nadalin
 
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - TallinnTwig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - TallinnSir-Arturio
 
VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9SSW
 
Angular JS
Angular JSAngular JS
Angular JS
John Temoty Roca
 
TYPO3 ViewHelper Workshop
TYPO3 ViewHelper WorkshopTYPO3 ViewHelper Workshop
TYPO3 ViewHelper Workshop
schmutt
 

Similar to Caching strategies in Rails 4 (11)

iWebkit
iWebkitiWebkit
iWebkit
 
elasticsearch basics workshop
elasticsearch basics workshopelasticsearch basics workshop
elasticsearch basics workshop
 
Data Modeling with Neo4j
Data Modeling with Neo4jData Modeling with Neo4j
Data Modeling with Neo4j
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshop
 
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
Bulding Device Agnostic UX Systems - Generate London, 23 Sep 2016
 
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
 
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - TallinnTwig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
 
VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9
 
Empezando con Twig
Empezando con TwigEmpezando con Twig
Empezando con Twig
 
Angular JS
Angular JSAngular JS
Angular JS
 
TYPO3 ViewHelper Workshop
TYPO3 ViewHelper WorkshopTYPO3 ViewHelper Workshop
TYPO3 ViewHelper Workshop
 

Recently uploaded

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 

Recently uploaded (20)

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 

Caching strategies in Rails 4