DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 11.11 Caching Frequently Viewed Files

Problem

You want to cache files that are viewed frequently, such as your site's front page, so that they don't have to be loaded from the filesystem every time.

Solution

Use mod_mmap_static or mod_file_cache (for Apache 1.3 and 2.0, respectively) to cache these files in memory:

MMapFile /www/htdocs/index.html
MMapFile /www/htdocs/other_page.html

For Apache 2.0, you can use either module or the CacheFile directive. MMapFile caches the file contents in memory, while CacheFile caches the file handle instead, which gives slightly poorer performance but uses less memory:

CacheFile /www/htdocs/index.html
CacheFile /www/htdocs/other_page.html

Discussion

For files that are frequently accessed, it is desirable to cache that file in some fashion to save disk access time. The MMapFile directive loads a file into RAM, and subsequent requests for that file are served directly out of RAM, rather than from the filesystem. The CacheFile directive, on the other hand, opens the file and caches the file handle, saving time on subsequent file opens.

In Apache 1.3, this functionality is available with the mod_mmap_static module, which is labelled as experimental and is not built into Apache by default. To enable this module, you need to specify the —enable-module=mmap_static flag to configure when building Apache. mod_mmap_static provides only the MMapFile directive.

In Apache 2.0, this functionality is provided by the mod_file_cache module, which is labelled as experimental, and is not built into Apache by default. To enable this module, you need to specify the —enable-file_cache flag to configure when building Apache. mod_file_cache provides both the MMapFile and CacheFile directives.

These directives take a single file as an argument, and there is not a provision for specifying a directory or set of directories. If you wish to have the entire contents of a directory mapped into memory, the documentation provides the following suggestion. For the directory in question, you would run the following command:

% find /www/htdocs -type f -print \
> | sed -e 's/.*/mmapfile &/' > /www/conf/mmap.conf

In your main server configuration file, you would then load the file created by that command, using the Include directive:

Include /www/conf/mmap.conf

This would cause every file contained in that directory to have the MMapFile directive invoked on it.

Note that when files are cached using one of these two directives, any changes to the file will require a server restart before they become visible.

See Also

    [ Team LiB ] Previous Section Next Section