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}”
Wednes...
BuildingBlocks
(ActionView::Helpers::CacheHelper)
- cache @product do
%h1= @product.name
= image_tag @product.image.url
We...
Write fragment
views/products/1-20130728191246157664000
(“views/#{cache_key}”)
- cache @product do
%h1= @product.name
= im...
Youcancomposefragmentkeys,too...
- cache [@product, “reviews”] do
%h1= @product.name
= image_tag @product.image.url
Write ...
“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)
Wednesd...
Write fragment
views/products/1-20130728191246157664000/3b2b93d56d331aa7a1bf9267a656d3c9
(“views/#{cache_key}/#{template_d...
Cache digest for products/_product.html: 42eb891316f21e68798de5d5aa8021d5
Read fragment
views/products/1-20130728191246157...
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: tru...
@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
- @p...
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 ge...
View the template dependency tree...
$ rake cache_digests:nested_dependencies TEMPLATE=your/template/name
ex:
and rake tas...
Explicit dependency
If Rails just can’t seem to figure out your dependency you can declare an explicit
dependency in a tem...
OtherResources
https://github.com/rails/cache_digests
http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-w...
Upcoming SlideShare
Loading in...5
×

Caching strategies in Rails 4

1,575

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,575
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Caching strategies in Rails 4

  1. 1. Key-basedcacheexpirationin Rails4 ...or Rails 3 with a dependency Wednesday, July 31, 13
  2. 2. BuildingBlocks(ActiveRecord::Base) @product.cache_key =>“products/1281-20130717091609” “#{name}/#{id}-#{timestamp}” Wednesday, July 31, 13
  3. 3. BuildingBlocks (ActionView::Helpers::CacheHelper) - cache @product do %h1= @product.name = image_tag @product.image.url Wednesday, July 31, 13
  4. 4. Write fragment views/products/1-20130728191246157664000 (“views/#{cache_key}”) - cache @product do %h1= @product.name = image_tag @product.image.url Wednesday, July 31, 13
  5. 5. 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
  6. 6. “ButwhatifI change atemplate?” Wednesday, July 31, 13
  7. 7. Rails.cache.clear Wednesday, July 31, 13
  8. 8. “ButwhatifI deploy5times aday” Wednesday, July 31, 13
  9. 9. sucks Wednesday, July 31, 13
  10. 10. Justkidding!™ Wednesday, July 31, 13
  11. 11. # Gemfile gem ‘pg’ gem‘cache_digests’ gem ‘haml-rails’ gem ‘cancan’ # ... (Rails 3 only – built into actionpack 4) Wednesday, July 31, 13
  12. 12. 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
  13. 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
  14. 14. templatechange ->newdigest ->newfragmentkey ->bustedcache! Wednesday, July 31, 13
  15. 15. Andwedidn’t havetodo anything! Wednesday, July 31, 13
  16. 16. “Russiandoll”cachingstrategy thenotorious Wednesday, July 31, 13
  17. 17. 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
  18. 18. @review.update!(rating: 3) @review.product’s key gets bumped as well Wednesday, July 31, 13
  19. 19. 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
  20. 20. the “russian doll”... index product reviews list review review review Wednesday, July 31, 13
  21. 21. @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
  22. 22. 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
  23. 23. 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
  24. 24. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×