Cache handling in apache

A cache is a mechanism for the temporary storage (caching) of web documents, such as HTML pages Js , Css files , flash files and images, to reduce bandwidth usage, server load,. A web cache stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met.

Web objects can be cached locally on the user’s computer or on a server on the Web. There are several types of caches for Web objects

  • Browser cache
  • Transparent proxy cache
  • Proxy cache
  • Reverse (inverse) proxy cache 

Advantages of Web Caching

  • Faster delivery of Web objects to the end user.
  • Reduces bandwidth needs and cost. It benefits the user, the service provider and the website owner.
  • Reduce load on Web Services/ Database
  • Increase Performance 

The Mechanics of Web Caching

Suppose that a user’s browser needs an image for a Web page. The browser is caching, all its requests are funneled through a transparent proxy cache, and the website has a reverse proxy cache sitting in front of it:

The browser checks to see if the image is cached locally. If yes, and the image is not stale, the browser uses the image from its cache. Otherwise, the browser sends the request for the image to the website.

The transparent proxy cache checks to see if it has the image. If yes, and the image is not stale, the proxy cache sends the image to the browser, which in addition to using caches it. Otherwise, the proxy cache sends the request for the image to the website where it is intercepted by the reverse proxy cache. When the transparent proxy cache gets the image, it sends it to the browser and also caches it.

The reverse proxy cache checks to see if it has the image. If yes, and the object is not stale, the reverse proxy cache sends the image to the requesting transparent proxy cache. Otherwise, the reverse proxy cache gets the image from the website, sends it to the requesting proxy cache, and caches the image.

Note that in each case, if the cache size is exceeded, the cache will have to throw out one or more cached objects so as to cache a new object. Typically the objects discarded are the ones that are used infrequently or ones that have not been used for a long time.

How to Enable Cache in Apache Modules and Setting the Expiry time for Cache contents

  1. Create a .htaccess file in your Root Directory
  2. Enable expires_module in Apache Extensions


ExpiresDefault "base [plus num type] [num type] ..."
ExpiresByType type/encoding "base [plus num type] [num type] ..."

where base is one of:

  • access
  • now (equivalent to ‘access‘)
  • modification

The plus keyword is optional. num should be an integer value [acceptable to atoi()], and type is one of:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Sample Htaccess Files for handling Cache

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html “access plus 1 seconds”
ExpiresByType image/gif “access plus 1 minutes”
ExpiresByType image/jpeg “access plus 1 years”
ExpiresByType image/png “access plus 1 days”
ExpiresByType image/x-icon “access plus 1 months”
ExpiresByType text/css “access plus 1 years”
ExpiresByType text/javascript “access plus 1 years”
ExpiresByType application/x-javascript “access plus 1 years”
ExpiresByType application/x-shockwave-flash “access plus 1 years”


This site uses Akismet to reduce spam. Learn how your comment data is processed.