Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Caching in Rails
Vysakh Sreenivasan
Only 2 hard
Problems in CS
● Naming
● Cache invalidation
Caching in a web app
● Prevent hitting databases.
● Cache the html responses.
● Help browser implement caching.
Before implementing caching
we need a cache server
Memcached is the popular cache server
sudo apt-get install memcached
Before implementing caching in
Rails we need to configure
config.action_controller.perform_caching = true
Using memcached with Rails 4.0
● gem ‘dalli’
● config.cache_store = :mem_cache_store
Lets get to the implementation
#1 Model level caching
Rails.cache.fetch
● First time, it can’t fetch a cache, so it will write a cache.
● Then it reads those caches.
Rails.cache.delete
● When passed an argument, it deletes the specific cache.
● You can clear caches in hooks like after_sa...
You can also use this to cache API results
#2 Fragment caching
<% cache @post do %>
<h3> <%= @post.title %> </h3>
<% end %>
cache_key: views/posts/1-
201505056193031061005000/bea6710809...
Cache key changes when
● the record is updated -> a new cache_key is formed
● the view is changed -> a new md5 key is gene...
#3 http caching
e-tag & 304 not modified
fresh_when(@post)
# or
fresh_when(etag: @post , last_modified: @post.updated_at)
based on @post.cache_key
which is based o...
if stale?(@post)
respond_to do |f|
…
end
end
Alternate syntax. Especially when using respond_to
def show
@post = Post.first
expires_in 5.minutes
end
Cache control: max_age
Recap
1 Model level
● Rails.cache.fetch
● Rails.cache.delete
2 Fragment
<% cache @post do %>
<% end %>
3. etags
if stale?(...
Resources
● Docs: http://guides.rubyonrails.org/caching_with_rails.html
● Railscasts episodes on http caching, low level c...
Upcoming SlideShare
Loading in …5
×

Caching in rails

568 views

Published on

1. Low level caching
2. Fragment caching
3. Http caching

Published in: Software
  • Be the first to comment

  • Be the first to like this

Caching in rails

  1. 1. Caching in Rails Vysakh Sreenivasan
  2. 2. Only 2 hard Problems in CS ● Naming ● Cache invalidation
  3. 3. Caching in a web app ● Prevent hitting databases. ● Cache the html responses. ● Help browser implement caching.
  4. 4. Before implementing caching we need a cache server
  5. 5. Memcached is the popular cache server sudo apt-get install memcached
  6. 6. Before implementing caching in Rails we need to configure
  7. 7. config.action_controller.perform_caching = true
  8. 8. Using memcached with Rails 4.0 ● gem ‘dalli’ ● config.cache_store = :mem_cache_store
  9. 9. Lets get to the implementation
  10. 10. #1 Model level caching
  11. 11. Rails.cache.fetch ● First time, it can’t fetch a cache, so it will write a cache. ● Then it reads those caches.
  12. 12. Rails.cache.delete ● When passed an argument, it deletes the specific cache. ● You can clear caches in hooks like after_save
  13. 13. You can also use this to cache API results
  14. 14. #2 Fragment caching
  15. 15. <% cache @post do %> <h3> <%= @post.title %> </h3> <% end %> cache_key: views/posts/1- 201505056193031061005000/bea67108094918eeba42cd4a6e786901 based on @post.cache_key which is based on updated_at md5 hash based on the view’s contents
  16. 16. Cache key changes when ● the record is updated -> a new cache_key is formed ● the view is changed -> a new md5 key is generated The old keys will be there, which will be removed automatically by memcached
  17. 17. #3 http caching
  18. 18. e-tag & 304 not modified
  19. 19. fresh_when(@post) # or fresh_when(etag: @post , last_modified: @post.updated_at) based on @post.cache_key which is based on updated_at
  20. 20. if stale?(@post) respond_to do |f| … end end Alternate syntax. Especially when using respond_to
  21. 21. def show @post = Post.first expires_in 5.minutes end Cache control: max_age
  22. 22. Recap 1 Model level ● Rails.cache.fetch ● Rails.cache.delete 2 Fragment <% cache @post do %> <% end %> 3. etags if stale?(@post)
  23. 23. Resources ● Docs: http://guides.rubyonrails.org/caching_with_rails.html ● Railscasts episodes on http caching, low level caching, cache digests, dalli gem.

×