2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Copyright 2002-2004 The Apache Software Foundation
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
22 <modulesynopsis metafile="mod_file_cache.xml.meta">
24 <name>mod_file_cache</name>
25 <description>Caches a static list of files in memory</description>
26 <status>Experimental</status>
27 <sourcefile>mod_file_cache.c</sourcefile>
28 <identifier>file_cache_module</identifier>
33 This module should be used with care. You can easily create a broken
34 site using <module>mod_file_cache</module>, so read this document
38 <p><em>Caching</em> frequently requested files that change very
39 infrequently is a technique for reducing server load.
40 <module>mod_file_cache</module> provides two techniques for caching
41 frequently requested <em>static</em> files. Through configuration
42 directives, you can direct <module>mod_file_cache</module> to either
43 open then <code>mmap()</code> a file, or to pre-open a file and save
44 the file's open <em>file handle</em>. Both techniques reduce server
45 load when processing requests for these files by doing part of the work
46 (specifically, the file I/O) for serving the file when the
47 server is started rather than during each request.</p>
49 <p>Notice: You cannot use this for speeding up CGI programs or
50 other files which are served by special content handlers. It
51 can only be used for regular files which are usually served by
52 the Apache core content handler.</p>
54 <p>This module is an extension of and borrows heavily from the
55 <code>mod_mmap_static</code> module in Apache 1.3.</p>
58 <section id="using"><title>Using mod_file_cache</title>
60 <p><module>mod_file_cache</module> caches a list of statically
61 configured files via <directive module="mod_file_cache"
62 >MMapFile</directive> or <directive module="mod_file_cache"
63 >CacheFile</directive> directives in the main server configuration.</p>
65 <p>Not all platforms support both directives. For example, Apache
66 on Windows does not currently support the <directive
67 module="mod_file_cache">MMapStatic</directive> directive, while
68 other platforms, like AIX, support both. You will receive an error
69 message in the server error log if you attempt to use an
70 unsupported directive. If given an unsupported directive, the
71 server will start but the file will not be cached. On platforms
72 that support both directives, you should experiment with both to
73 see which works best for you.</p>
75 <section><title>MMapFile Directive</title>
77 <p>The <directive module="mod_file_cache">MMapFile</directive>
78 directive of <module>mod_file_cache</module> maps a list of
79 statically configured files into memory through the system call
80 <code>mmap()</code>. This system call is available on most modern
81 Unix derivates, but not on all. There are sometimes system-specific
82 limits on the size and number of files that can be
83 <code>mmap()</code>ed, experimentation is probably the easiest way
86 <p>This <code>mmap()</code>ing is done once at server start or
87 restart, only. So whenever one of the mapped files changes on the
88 filesystem you <em>have</em> to restart the server (see the <a
89 href="../stopping.html">Stopping and Restarting</a> documentation).
90 To reiterate that point: if the files are modified <em>in place</em>
91 without restarting the server you may end up serving requests that
92 are completely bogus. You should update files by unlinking the old
93 copy and putting a new copy in place. Most tools such as
94 <code>rdist</code> and <code>mv</code> do this. The reason why this
95 modules doesn't take care of changes to the files is that this check
96 would need an extra <code>stat()</code> every time which is a waste
97 and against the intent of I/O reduction.</p>
100 <section><title>CacheFile Directive</title>
102 <p>The <directive module="mod_file_cache">CacheFile</directive>
103 directive of <module>mod_file_cache</module> opens an active
104 <em>handle</em> or <em>file descriptor</em> to the file (or files)
105 listed in the configuration directive and places these open file
106 handles in the cache. When the file is requested, the server
107 retrieves the handle from the cache and passes it to the
108 <code>sendfile()</code> (or <code>TransmitFile()</code> on Windows),
112 <p>Insert more details about sendfile API...</p>
115 <p>This file handle caching is done once at server start or
116 restart, only. So whenever one of the cached files changes on
117 the filesystem you <em>have</em> to restart the server (see the
118 <a href="../stopping.html">Stopping and Restarting</a>
119 documentation). To reiterate that point: if the files are
120 modified <em>in place</em> without restarting the server you
121 may end up serving requests that are completely bogus. You
122 should update files by unlinking the old copy and putting a new
123 copy in place. Most tools such as <code>rdist</code> and
124 <code>mv</code> do this.</p>
127 <note><title>Note</title>
128 <p>Don't bother asking for a directive which recursively
129 caches all the files in a directory. Try this instead... See the
130 <directive module="core">Include</directive> directive, and consider
134 find /www/htdocs -type f -print \<br />
135 | sed -e 's/.*/mmapfile &/' > /www/conf/mmap.conf
141 <name>MMapFile</name>
142 <description>Map a list of files into memory at startup time</description>
143 <syntax>MMapFile <var>file-path</var> [<var>file-path</var>] ...</syntax>
144 <contextlist><context>server config</context></contextlist>
147 <p>The <directive>MMapFile</directive> directive maps one or more files
148 (given as whitespace separated arguments) into memory at server
149 startup time. They are automatically unmapped on a server
150 shutdown. When the files have changed on the filesystem at
151 least a <code>HUP</code> or <code>USR1</code> signal should be send to
152 the server to re-<code>mmap()</code> them.</p>
154 <p>Be careful with the <var>file-path</var> arguments: They have
155 to literally match the filesystem path Apache's URL-to-filename
156 translation handlers create. We cannot compare inodes or other
157 stuff to match paths through symbolic links <em>etc.</em>
158 because that again would cost extra <code>stat()</code> system
159 calls which is not acceptable. This module may or may not work
160 with filenames rewritten by <module>mod_alias</module> or
161 <module>mod_rewrite</module>.</p>
163 <example><title>Example</title>
164 MMapFile /usr/local/apache/htdocs/index.html
170 <name>CacheFile</name>
171 <description>Cache a list of file handles at startup time</description>
172 <syntax>CacheFile <var>file-path</var> [<var>file-path</var>] ...</syntax>
173 <contextlist><context>server config</context></contextlist>
176 <p>The <directive>CacheFile</directive> directive opens handles to
177 one or more files (given as whitespace separated arguments) and
178 places these handles into the cache at server startup
179 time. Handles to cached files are automatically closed on a server
180 shutdown. When the files have changed on the filesystem, the
181 server should be restarted to to re-cache them.</p>
183 <p>Be careful with the <var>file-path</var> arguments: They have
184 to literally match the filesystem path Apache's URL-to-filename
185 translation handlers create. We cannot compare inodes or other
186 stuff to match paths through symbolic links <em>etc.</em>
187 because that again would cost extra <code>stat()</code> system
188 calls which is not acceptable. This module may or may not work
189 with filenames rewritten by <module>mod_alias</module> or
190 <module>mod_rewrite</module>.</p>
192 <example><title>Example</title>
193 CacheFile /usr/local/apache/htdocs/index.html