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.
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,
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
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.