Caching By Nyros Developer


Published on

Caching By Nyros Developer

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Caching By Nyros Developer

  1. 1. <ul><li>Caching </li></ul>
  2. 2. What is Caching ? <ul><li>Caching is a process of temporarily storing a data likely to be used again and again. </li></ul><ul><li>Its an art of taking a processed webpage or part of webpage, and storing in a temporary location. If the same webpage is requested by user, then we can serve the cached version. </li></ul><ul><li>Caching is a cheep way of speeding up slow applications by keeping the results of calculations. </li></ul>
  3. 3. (Where & When) – Caching ? <ul><li>While developing an web application, we need to take care of caching also. </li></ul><ul><li>When application goes live and more people are using the application, you are required to use caching. </li></ul><ul><li>Sometimes web application is slow, and you have heavy traffic and you want to run the site in slow hardware or shared host, then you need caching. </li></ul>
  4. 4. Caching In ROR <ul><li>Ruby on rails provides an build in frame work for caching. </li></ul><ul><li>Which helps to make use of the functionality with less code. </li></ul><ul><li>caching in RoR is very powerful standard feature. </li></ul>
  5. 5. Caching Types <ul><li>Page caching </li></ul><ul><li>Action caching </li></ul><ul><li>Fragment caching </li></ul>
  6. 6. Page Caching <ul><li>Page Caching is an approach to caching where entire action output is stored in HTML file, That the webserver can serve without going through Action pack(Rails stack i.e framework) </li></ul><ul><li>Obviously this is super fast. </li></ul>
  7. 7. Implementation <ul><li>Class AboutusController < ActionController </li></ul><ul><li>cache_page :index </li></ul><ul><li>def index </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  8. 8. Application flow <ul><li> </li></ul><ul><li>Mongrel can handle 20-50 requests/second. So on an average it can handle </li></ul><ul><li>2 million hit per a day. Which is suitable for application. </li></ul>Client Apache Mongrel/Webrick
  9. 9. With page caching turned on <ul><li> </li></ul><ul><li>Rails will create a cache file in public folder </li></ul>Client Apache Mongrel/Webrick Public/aboutus/index.html
  10. 10. Serving the cached file <ul><li> </li></ul><ul><li>Apache can handle +1000 requests/second. So on an average 86 million hits a day. So it can handle lot of traffic just by single apache instance </li></ul>Client Apache Mongrel/Webrick Public/aboutus/index.html
  11. 11. Page caching - limitations <ul><li>This incredible speed up is only available to stateless pages. Where all visitors are treated as same. </li></ul><ul><li>Page caching ignores all parameters. i.e in the url if we have pagination options there will not be considered by page caching. </li></ul><ul><li>Page caching is limited to areas where site that will not require authentication. </li></ul>
  12. 12. Action caching <ul><li>Action caching works same like page caching. But adds additional feature to run filters before the caching is served. </li></ul><ul><li>In action caching the incoming web request goes from the web server to rails stack and action pack and do some operations (i.e filters), before running the cache page. </li></ul><ul><li>So we need action caching, when we need filters to run. </li></ul>
  13. 13. Implementation <ul><li>Class ProductsController < ActionController </li></ul><ul><li>before_filter :authentication </li></ul><ul><li>caches_action :index </li></ul><ul><li>def index </li></ul><ul><li>@products = Product.find(:all) </li></ul><ul><li>end </li></ul><ul><li>def create </li></ul><ul><li>expire_action :action => :index </li></ul><ul><li>end </li></ul>
  14. 14. Application flow <ul><li> </li></ul>Client Apache Mongrel/Webrick
  15. 15. With action caching turned on <ul><li> </li></ul><ul><li>Rails will create a cache file in public folder </li></ul>Client Apache Mongrel/Webrick page in cache
  16. 16. Serving the cached file <ul><li> </li></ul><ul><li>run filters(authentication) </li></ul>Client Apache Mongrel/Webrick Page in caching
  17. 17. Fragment caching <ul><li>Fragment caching is used to cache various blocks with templates, without caching the entire action as whole. </li></ul><ul><li>Mainly used when a page has more parts regularly change and other parts doesn’t change regularly. </li></ul><ul><li>Fragment caching allows a fragment of view logic to be wrapped in cache block and served out of cache store when next request comes in. </li></ul>
  18. 18. Implementation <ul><li>In app/views: </li></ul><ul><li><% cache do %> </li></ul><ul><li>Your code ….. </li></ul><ul><li><% end %> </li></ul><ul><li><% cache(:cache_key ) do %> </li></ul><ul><li>Your code …. </li></ul><ul><li><% end %> </li></ul>
  19. 19. Implementation <ul><li>Class ProductsController < ActionController </li></ul><ul><li>def index </li></ul><ul><li>@products = Product.find(:all) </li></ul><ul><li>if !fragment_exist? :recent_posts </li></ul><ul><li>@recent_products = Product.find(:all, :order => “created_at”, :limit => 3) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>def create </li></ul><ul><li>expire_fragment :recent_posts </li></ul><ul><li>end </li></ul>
  20. 20. Implementation <ul><li><% cache(: recent_posts ) do %> </li></ul><ul><li><% for product in @recent_products %> </li></ul><ul><li><%= %> </li></ul><ul><li><% end %> </li></ul><ul><li><% end %> </li></ul>
  21. 21. Application flow <ul><li> </li></ul>Client Apache Mongrel/Webrick
  22. 22. With fragment caching turned on <ul><li> </li></ul>Client Apache Mongrel/Webrick Header box #1 box #2 dashboard
  23. 23. Serving the cached file <ul><li> </li></ul>Client Apache Mongrel/Webrick Header box #1 box #2 dashboard
  24. 24. Few points <ul><li>The cache helper identifies the fragment to cache. The first parameter is unique name identifier a cache fragment. The second parameter contain a code block. </li></ul><ul><li>By this way slowest parts of the page are cached and make the web application response time to low….. </li></ul>
  25. 25. When to do it? <ul><li>When you can’t use page or action caching. </li></ul><ul><li>Certain parts of the page are unique to each other. </li></ul>Common parts to all pages Common parts to all pages Common parts to all pages
  26. 26. configuration <ul><li>To get started make sure config.action_controller.perform_caching is set to true for your environment </li></ul><ul><li>  By default, caching is disabled for development and test, and enabled for production. </li></ul><ul><li>config.action_controller.perform_caching = true </li></ul>
  27. 27. <ul><li>Thank You </li></ul><ul><li>Uma Mahesh </li></ul>