SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Caching strategies in Rails 4

Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - TallinnTwig & D8 - DrupalCamp Baltics 2013 - Tallinn
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Sir-Arturio
 
VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9VisualStudio2012-WhatsNew-TechEd_v3-9
VisualStudio2012-WhatsNew-TechEd_v3-9
SSW
 

Semelhante a 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
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Caching strategies in Rails 4