27
Caching Caching

Caching By Nyros Developer

Embed Size (px)

DESCRIPTION

Caching By Nyros Developer

Citation preview

Page 1: Caching By Nyros Developer

CachingCaching

Page 2: Caching By Nyros Developer

What is Caching ?What is Caching ?

Caching is a process of temporarily storing Caching is a process of temporarily storing a data likely to be used again and again. a data likely to be used again and again.

Its an art of taking a processed webpage Its an art of taking a processed webpage or part of webpage, and storing in a or part of webpage, and storing in a temporary location. If the same webpage temporary location. If the same webpage is requested by user, then we can serve is requested by user, then we can serve the cached version. the cached version.

Caching is a cheep way of speeding up Caching is a cheep way of speeding up slow applications by keeping the results of slow applications by keeping the results of calculations.calculations.

Page 3: Caching By Nyros Developer

(Where & When) – (Where & When) – Caching ?Caching ?

While developing an web application, While developing an web application, we need to take care of caching also. we need to take care of caching also.

When application goes live and more When application goes live and more people are using the application, you people are using the application, you are required to use caching.are required to use caching.

Sometimes web application is slow, and Sometimes web application is slow, and you have heavy traffic and you want to you have heavy traffic and you want to run the site in slow hardware or shared run the site in slow hardware or shared host, then you need caching. host, then you need caching.

Page 4: Caching By Nyros Developer

Caching In RORCaching In ROR

Ruby on rails provides an build in Ruby on rails provides an build in frame work for caching.frame work for caching.

Which helps to make use of the Which helps to make use of the functionality with less code.functionality with less code.

caching in RoR is very powerful caching in RoR is very powerful standard feature. standard feature.

Page 5: Caching By Nyros Developer

Caching TypesCaching Types

Page cachingPage caching Action cachingAction caching Fragment caching Fragment caching

Page 6: Caching By Nyros Developer

Page Caching Page Caching

Page Caching is an approach to Page Caching is an approach to caching where entire action output caching where entire action output is stored in HTML file, That the is stored in HTML file, That the webserver can serve without going webserver can serve without going through Action pack(Rails stack i.e through Action pack(Rails stack i.e framework)framework)

Obviously this is super fast.Obviously this is super fast.

Page 7: Caching By Nyros Developer

Implementation Implementation

Class AboutusController < Class AboutusController < ActionControllerActionController

cache_page :indexcache_page :index

def indexdef index

endend

endend

Page 8: Caching By Nyros Developer

Application flowApplication flow

http://abc.com/aboutushttp://abc.com/aboutus

Mongrel can handle 20-50 requests/second. So on an average it can Mongrel can handle 20-50 requests/second. So on an average it can handle handle

2 million hit per a day. Which is suitable for application. 2 million hit per a day. Which is suitable for application.

Client

Apache

Mongrel/Webrick

Page 9: Caching By Nyros Developer

With page caching With page caching turned onturned on

http://abc.com/aboutushttp://abc.com/aboutus

Rails will create a cache file in public folderRails will create a cache file in public folder

Client

Apache

Mongrel/Webrick

Public/aboutus/index.html

Page 10: Caching By Nyros Developer

Serving the cached file Serving the cached file

http://abc.com/aboutushttp://abc.com/aboutus

Apache can handle +1000 requests/second. So on an average 86 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 million hits a day. So it can handle lot of traffic just by single apache instance instance

Client

Apache Mongrel/Webrick

Public/aboutus/index.html

Page 11: Caching By Nyros Developer

Page caching - Page caching - limitationslimitations

This incredible speed up is only This incredible speed up is only available to stateless pages. Where all available to stateless pages. Where all visitors are treated as same.visitors are treated as same.

Page caching ignores all parameters. Page caching ignores all parameters. i.e in the url if we have pagination i.e in the url if we have pagination options there will not be considered by options there will not be considered by page caching. page caching.

Page caching is limited to areas where Page caching is limited to areas where site that will not require authentication. site that will not require authentication.

Page 12: Caching By Nyros Developer

Action caching Action caching

Action caching works same like page Action caching works same like page caching. But adds additional feature to caching. But adds additional feature to run filters before the caching is served.run filters before the caching is served.

In action caching the incoming web In action caching the incoming web request goes from the web server to request goes from the web server to rails stack and action pack and do some rails stack and action pack and do some operations (i.e filters), before running operations (i.e filters), before running the cache page.the cache page.

So we need action caching, when we So we need action caching, when we need filters to run. need filters to run.

Page 13: Caching By Nyros Developer

ImplementationImplementation

Class ProductsController < Class ProductsController < ActionControllerActionController

before_filter :authenticationbefore_filter :authentication

caches_action :indexcaches_action :index

def indexdef index

@products = Product.find(:all)@products = Product.find(:all)

endend

def createdef create

expire_action :action => :indexexpire_action :action => :index

endend

Page 14: Caching By Nyros Developer

Application flowApplication flow

http://abc.com/aboutushttp://abc.com/aboutus

Client

Apache

Mongrel/Webrick

Page 15: Caching By Nyros Developer

With action caching With action caching turned onturned on

http://abc.com/aboutushttp://abc.com/aboutus

Rails will create a cache file in public folderRails will create a cache file in public folder

Client

Apache

Mongrel/Webrick

page in cache

Page 16: Caching By Nyros Developer

Serving the cached file Serving the cached file

http://abc.com/aboutushttp://abc.com/aboutus

run run filters(authentication) filters(authentication)

Client

Apache Mongrel/Webrick

Page in caching

Page 17: Caching By Nyros Developer

Fragment caching Fragment caching

Fragment caching is used to cache Fragment caching is used to cache various blocks with templates, without various blocks with templates, without caching the entire action as whole.caching the entire action as whole.

Mainly used when a page has more Mainly used when a page has more parts regularly change and other parts parts regularly change and other parts doesn’t change regularly. doesn’t change regularly.

Fragment caching allows a fragment of Fragment caching allows a fragment of view logic to be wrapped in cache block view logic to be wrapped in cache block and served out of cache store when next and served out of cache store when next request comes in.request comes in.

Page 18: Caching By Nyros Developer

ImplementationImplementation

In app/views:In app/views:

<% cache do %><% cache do %>

Your code …..Your code …..

<% end %><% end %>

<% cache(:cache_key ) do %><% cache(:cache_key ) do %>

Your code ….Your code ….

<% end %><% end %>

Page 19: Caching By Nyros Developer

ImplementationImplementation

Class ProductsController < ActionControllerClass ProductsController < ActionController

def indexdef index

@products = Product.find(:all)@products = Product.find(:all)

if !fragment_exist? :recent_postsif !fragment_exist? :recent_posts

@recent_products = Product.find(:all, :order => @recent_products = Product.find(:all, :order => “created_at”, :limit => 3)“created_at”, :limit => 3)

endend

endend

def createdef create

expire_fragment :recent_postsexpire_fragment :recent_posts

endend

Page 20: Caching By Nyros Developer

ImplementationImplementation

<% cache(: recent_posts ) do %><% cache(: recent_posts ) do %>

<% for product in <% for product in @recent_products %>@recent_products %>

<%= product.name %><%= product.name %>

<% end %><% end %>

<% end %><% end %>

Page 21: Caching By Nyros Developer

Application flowApplication flow

http://abc.com/aboutushttp://abc.com/aboutus

Client

Apache

Mongrel/Webrick

Page 22: Caching By Nyros Developer

With fragment caching With fragment caching turned onturned on

http://abc.com/aboutushttp://abc.com/aboutus

Client

Apache

Mongrel/Webrick

Header

box #1

box #2

dashboard

Page 23: Caching By Nyros Developer

Serving the cached file Serving the cached file

http://abc.com/aboutushttp://abc.com/aboutus

Client

Apache

Mongrel/Webrick

Header

box #1

box #2

dashboard

Page 24: Caching By Nyros Developer

Few pointsFew points

The cache helper identifies the The cache helper identifies the fragment to cache. The first fragment to cache. The first parameter is unique name identifier parameter is unique name identifier a cache fragment. The second a cache fragment. The second parameter contain a code block.parameter contain a code block.

By this way slowest parts of the page By this way slowest parts of the page are cached and make the web are cached and make the web application response time to low…..application response time to low…..

Page 25: Caching By Nyros Developer

When to do it?When to do it?

When you can’t use page or action When you can’t use page or action caching. caching.

Certain parts of the page are unique Certain parts of the page are unique to each other.to each other.

Common parts to all pages

Common parts to all pages

Common parts to all pages

Page 26: Caching By Nyros Developer

configurationconfiguration

To get started make To get started make sure config.action_controller.perform_casure config.action_controller.perform_caching is set to true for your environment ching is set to true for your environment

  By default, caching is disabled for By default, caching is disabled for development and test, and enabled for development and test, and enabled for production. production.

config.action_controller.perform_cachinconfig.action_controller.perform_caching = true g = true

Page 27: Caching By Nyros Developer

Thank YouThank You

Uma MaheshUma Mahesh