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, by contrast, 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access