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

Rich Snippets in Magento product page
Rich Snippets in Magento product pageRich Snippets in Magento product page
Rich Snippets in Magento product pageHans Kuijpers
 
04 jQuery Mobile
04 jQuery Mobile04 jQuery Mobile
04 jQuery MobileYnon Perek
 
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...Alessandro Nadalin
 
elasticsearch basics workshop
elasticsearch basics workshopelasticsearch basics workshop
elasticsearch basics workshopMathieu Elie
 
Data Modeling with Neo4j
Data Modeling with Neo4jData Modeling with Neo4j
Data Modeling with Neo4jNeo4j
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshopArjen 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 2016Anna 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
 
TYPO3 ViewHelper Workshop
TYPO3 ViewHelper WorkshopTYPO3 ViewHelper Workshop
TYPO3 ViewHelper Workshopschmutt
 

Similar to Caching strategies in rails 4 (14)

Rich Snippets in Magento product page
Rich Snippets in Magento product pageRich Snippets in Magento product page
Rich Snippets in Magento product page
 
04 jQuery Mobile
04 jQuery Mobile04 jQuery Mobile
04 jQuery Mobile
 
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
Hey, I just met AngularJS, and this is crazy, so here’s my JavaScript, let’s ...
 
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

IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka DoktorováCzechDreamin
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomCzechDreamin
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationZilliz
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 

Recently uploaded (20)

IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 

Caching strategies in rails 4