Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Caching in Apache

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 690
    Comment on it

    Caching is an important feature to improve the performance of Apache server. By creating the caching rules effectively, we can conserve a good amount of resources and the content access by the user is significantly fast. Apache offers several methods for caching but the two most common of them are mod_file_cache and mod_cache.

    mod_file_cache : mod_file_cache is simpler of the two methods to implement. It works by caching the frequently requested and infrequently changed content. When the server is started, it performs some of the file access operations on the frequently requested files.

    Using mod_file_cache : This caching mechanism is not suitable and should not be used for caching dynamically generated content. This module provides two directives to implement caching in two different ways.

    MmapFile : This directive creates a list of files on server start and then maps those files into memory. This type of caching is set up in the server configuration file by specifying the files to be cached in a space separated list.
    Example :
    MMapFile /var/www/index.html /var/www/about-us.html

    Server needs to be restarted if any of these files changed.

    CacheFile : It opens the handle to the files listed. It cuts time to open the cached files by maintaining a table of these opened files. This type of caching is set up in the server configuration file by specifying the files to be cached in a space separated list.

    CacheFile /var/www/index.html /var/www/about-us.html

    mod_cache : The mod_cache module provides HTTP-aware caching schemes. This means that the files will be cached according to an instruction specifying how long a page can be considered "fresh". It is far more flexible than mod_file_cache. It handles changing content by configuring how long a file is valid for caching. It uses either of the two modules to implement caching namely, mod_mem_cache or mod_disk_cache.

    These modules are more complex than mod_file_cache but at the same time are more useful in most circumstances. The difference between these two are the storage of cached files. mod_mem_cache stores cached files in memory while mod_disk_cache stores on disk. These are stored and retrieved using URI based keys.

    Configuring mod_mem_cache : If we look into the configuration of mod_mem_cache,

    <IfModule mod_mem_cache.c>
            CacheEnable mem /
            MCacheSize 4096
            MCacheMaxObjectCount 100
            MCacheMinObjectSize 1
            MCacheMaxObjectSize 2048
    </IfModule>
    

    These directives works only if the mod_mem_cache module is loaded.

    In order to enable the mod_mem_cache module and mod_cache as well, open the terminal and type the following :

    sudo a2enmod mem_cache
    

    Now, restart Apache

    sudo service apache2 restart
    

    We also need to tell apache what to cache

    CacheEnable mem /css
    

    This will tell apache to create a memory cache for contents stored in css directory.

    To specify a cache size, we have two directives. The McacheSize specifies the maximum size of cache in terms of memory usage and the McacheMaxOjectCount specifies in terms of number of objects.

    MCacheSize 4096
    MCacheMaxObjectCount 100
    

    Configuring mod_disk_cache : Lets examine the mod_disk_cache configuration file,

    <IfModule mod_disk_cache.c>
            CacheRoot /var/cache/apache2/mod_disk_cache
            #CacheEnable disk /
            CacheDirLevels 5
            CacheDirLength 3
    </IfModule>
    

    In order to enable mod_disk_cache as well as mod_cache, open the terminal and type the following :

    sudo a2enmod disk_cache
    

    Now, restart Apache

    sudo service apache2 restart
    

    To specify the storage of cached content, we can use CacheRoot directive

    CacheRoot /var/cache/apache2/mod_disk_cache
    

    Each cached element is hashed by the its URL, and then the hash is used as a filename and directory path. To determine the caching structure within the cache root, we have two more directives namely CacheDirLevel and CacheDirLength. The CacheDirLevel decides how many directories to create from the hash string and the CacheDirLength decides how many characters are in each directory name.

    CacheDirLevels 5
    CacheDirLength 3
    

    References

    1. http://httpd.apache.org/docs/2.2/caching.html
    2. https://www.digitalocean.com/community/tutorials/how-to-configure-content-caching-using-apache-modules-on-a-vps

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: