SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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, ...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Caching strategies in Rails 4