]> granicus.if.org Git - apache/blob - docs/manual/caching.xml
update revision references
[apache] / docs / manual / caching.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <manualpage metafile="caching.xml.meta">
24
25   <title>Caching Guide</title>
26
27   <summary>
28     <p>This document supplements the <module>mod_cache</module>,
29     <module>mod_disk_cache</module>, <module>mod_mem_cache</module>,
30     <module>mod_file_cache</module> and <a 
31     href="programs/htcacheclean.html">htcacheclean</a> reference documentation.
32     It describes how to use Apache's caching features to accelerate web and 
33     proxy serving, while avoiding common problems and misconfigurations.</p>
34   </summary>
35
36   <section id="introduction">
37     <title>Introduction</title>
38
39     <p>As of Apache HTTP server version 2.2 <module>mod_cache</module>
40     and <module>mod_file_cache</module> are no longer marked
41     experimental and are considered suitable for production use. These
42     caching architectures provide a powerful means to accelerate HTTP
43     handling, both as an origin webserver and as a proxy.</p>
44
45     <p><module>mod_cache</module> and its provider modules 
46     <module>mod_mem_cache</module> and <module>mod_disk_cache</module> 
47     provide intelligent, HTTP-aware caching. The content itself is stored
48     in the cache, and mod_cache aims to honour all of the various HTTP
49     headers and options that control the cachability of content. It can
50     handle both local and proxied content. <module>mod_cache</module>
51     is aimed at both simple and complex caching configurations, where
52     you are dealing with proxied content, dynamic local content or 
53     have a need to speed up access to local files which change with 
54     time.</p>
55
56     <p><module>mod_file_cache</module> on the other hand presents a more
57     basic, but sometimes useful, form of caching. Rather than maintain
58     the complexity of actively ensuring the cachability of URLs,
59     <module>mod_file_cache</module> offers file-handle and memory-mapping 
60     tricks to keep a cache of files as they were when Apache was last 
61     started. As such, <module>mod_file_cache</module> is aimed at improving 
62     the access time to local static files which do not change very
63     often.</p>
64
65     <p>As <module>mod_file_cache</module> presents a relatively simple
66     caching implementation, apart from the specific sections on <directive 
67     module="mod_file_cache">CacheFile</directive> and <directive 
68     module="mod_file_cache">MMapStatic</directive>, the explanations
69     in this guide cover the <module>mod_cache</module> caching 
70     architecture.</p>
71
72     <p>To get the most from this document, you should be familiar with 
73     the basics of HTTP, and have read the Users' Guides to 
74     <a href="urlmapping.html">Mapping URLs to the Filesystem</a> and 
75     <a href="content-negotiation.html">Content negotiation</a>.</p>
76
77   </section>
78    
79   <section id="overview">
80
81     <title>Caching Overview</title>
82     
83     <related>
84       <modulelist>
85         <module>mod_cache</module>
86         <module>mod_mem_cache</module>
87         <module>mod_disk_cache</module>
88         <module>mod_file_cache</module>
89       </modulelist>
90       <directivelist>
91         <directive module="mod_cache">CacheEnable</directive>
92         <directive module="mod_cache">CacheDisable</directive>
93         <directive module="mod_file_cache">MMapStatic</directive>
94         <directive module="mod_file_cache">CacheFile</directive>
95         <directive module="mod_file_cache">CacheFile</directive>
96         <directive module="core">UseCanonicalName</directive>
97         <directive module="mod_negotiation">CacheNegotiatedDocs</directive>
98       </directivelist>
99     </related>
100
101     <p>There are two main stages in <module>mod_cache</module> that can
102     occur in the lifetime of a request. First, <module>mod_cache</module>
103     is a URL mapping module, which means that if a URL has been cached,
104     and the cached version of that URL has not expired, the request will 
105     be served directly by <module>mod_cache</module>.</p>
106
107     <p>This means that any other stages that might ordinarily happen
108     in the process of serving a request -- for example being handled
109     by <module>mod_proxy</module>, or <module>mod_rewrite</module> --
110     won't happen.  But then this is the point of caching content in
111     the first place.</p>
112
113     <p>If the URL is not found within the cache, <module>mod_cache</module>
114     will add a <a href="filter.html">filter</a> to the request handling. After
115     Apache has located the content by the usual means, the filter will be run
116     as the content is served. If the content is determined to be cacheable, 
117     the content will be saved to the cache for future serving.</p>
118
119     <p>If the URL is found within the cache, but also found to have expired,
120     the filter is added anyway, but <module>mod_cache</module> will create
121     a conditional request to the backend, to determine if the cached version
122     is still current. If the cached version is still current, its
123     meta-information will be updated and the request will be served from the
124     cache. If the cached version is no longer current, the cached version
125     will be deleted and the filter will save the updated content to the cache
126     as it is served.</p>
127
128     <section>
129       <title>Improving Cache Hits</title>
130
131       <p>When caching locally generated content, ensuring that  
132       <directive module="core">UseCanonicalName</directive> is set to 
133       <code>On</code> can dramatically improve the ratio of cache hits. This
134       is because the hostname of the virtual-host serving the content forms
135       a part of the cache key. With the setting set to <code>On</code>
136       virtual-hosts with multiple server names or aliases will not produce
137       differently cached entities, and instead content will be cached as
138       per the canonical hostname.</p>
139
140       <p>Because caching is performed within the URL to filename translation 
141       phase, cached documents will only be served in response to URL requests.
142       Ordinarily this is of little consequence, but there is one circumstance
143       in which it matters: If you are using <a href="howto/ssi.html">Server 
144       Side Includes</a>;</p>
145
146       <example>
147       <pre>
148 &lt;!-- The following include can be cached --&gt;
149 &lt;!--#include virtual="/footer.html" --&gt; 
150
151 &lt;!-- The following include can not be cached --&gt;
152 &lt;!--#include file="/path/to/footer.html" --&gt;</pre>
153       </example>
154
155       <p>If you are using Server Side Includes, and want the benefit of speedy
156       serves from the cache, you should use <code>virtual</code> include
157       types.</p>
158     </section>
159     
160     <section>
161       <title>Expiry Periods</title>
162     
163       <p>The default expiry period for cached entities is one hour, however 
164       this can be easily over-ridden by using the <directive 
165       module="mod_cache">CacheDefaultExpire</directive> directive. This
166       default is only used when the original source of the content does not
167       specify an expire time or time of last modification.</p>
168
169       <p>If a response does not include an <code>Expires</code> header but does
170       include a <code>Last-Modified</code> header, <module>mod_cache</module>
171       can infer an expiry period based on the use of the <directive 
172       module="mod_cache">CacheLastModifiedFactor</directive> directive.</p>
173
174       <p>For local content, <module>mod_expires</module> may be used to
175       fine-tune the expiry period.</p>
176
177       <p>The maximum expiry period may also be controlled by using the
178       <directive module="mod_cache">CacheMaxExpire</directive>.</p>
179
180     </section>
181
182     <section>
183       <title>A Brief Guide to Conditional Requests</title>
184
185       <p>When content expires from the cache and is re-requested from the 
186       backend or content provider, rather than pass on the original request,
187       Apache will use a conditional request instead.</p>
188
189       <p>HTTP offers a number of headers which allow a client, or cache
190       to discern between different versions of the same content. For
191       example if a resource was served with an "Etag:" header, it is
192       possible to make a conditional request with an "If-Match:" 
193       header. If a resource was served with a "Last-Modified:" header
194       it is possible to make a conditional request with an 
195       "If-Modified-Since:" header, and so on.</p>
196
197       <p>When such a conditional request is made, the response differs
198       depending on whether the content matches the conditions. If a request is 
199       made with an "If-Modified-Since:" header, and the content has not been 
200       modified since the time indicated in the request then a terse "304 Not 
201       Modified" response is issued.</p>
202
203       <p>If the content has changed, then it is served as if the request were
204       not conditional to begin with.</p>
205
206       <p>The benefits of conditional requests in relation to caching are 
207       twofold. Firstly, when making such a request to the backend, if the 
208       content from the backend matches the content in the store, this can be
209       determined easily and without the overhead of transferring the entire
210       resource.</p>
211
212       <p>Secondly, conditional requests are usually less strenuous on the
213       backend. For static files, typically all that is involved is a call
214       to <code>stat()</code> or similar system call, to see if the file has
215       changed in size or modification time. As such, even if Apache is
216       caching local content, even expired content may still be served faster
217       from the cache if it has not changed. As long as reading from the cache
218       store is faster than reading from the backend (e.g. an in-memory cache 
219       compared to reading from disk).</p> 
220     </section>
221
222     <section>
223       <title>What Can be Cached?</title>
224
225       <p>As mentioned already, the two styles of caching in Apache work 
226       differently, <module>mod_file_cache</module> caching maintains file 
227       contents as they were when Apache was started. When a request is 
228       made for a file that is cached by this module, it is intercepted 
229       and the cached file is served.</p>
230
231       <p><module>mod_cache</module> caching on the other hand is more
232       complex. When serving a request, if it has not been cached
233       previously, the caching module will determine if the content
234       is cacheable. The conditions for determining cachability of 
235       a response are;</p>
236
237       <ol>
238         <li>Caching must be enabled for this URL. See the <directive 
239         module="mod_cache">CacheEnable</directive> and <directive
240         module="mod_cache">CacheDisable</directive> directives.</li>
241
242         <li>The response must have a HTTP status code of 200, 203, 300, 301 or 
243         410.</li>
244
245         <li>The request must be a HTTP GET request.</li>
246
247         <li>If the request contains an "Authorization:" header, the response
248         will not be cached.</li>
249
250         <li>If the response contains an "Authorization:" header, it must
251         also contain an "s-maxage", "must-revalidate" or "public" option
252         in the "Cache-Control:" header.</li>
253
254         <li>If the URL included a query string (e.g. from a HTML form GET
255         method) it will not be cached unless the response includes an
256         "Expires:" header, as per RFC2616 section 13.9.</li>
257
258         <li>If the response has a status of 200 (OK), the response must
259         also include at least one of the "Etag", "Last-Modified" or
260         the "Expires" headers, unless the 
261         <directive module="mod_cache">CacheIgnoreNoLastMod</directive> 
262         directive has been used to require otherwise.</li>
263
264         <li>If the response includes the "private" option in a "Cache-Control:"
265         header, it will not be stored unless the 
266         <directive module="mod_cache">CacheStorePrivate</directive> has been
267         used to require otherwise.</li>
268
269         <li>Likewise, if the response includes the "no-store" option in a 
270         "Cache-Control:" header, it will not be stored unless the 
271         <directive module="mod_cache">CacheStoreNoStore</directive> has been
272         used.</li>
273
274         <li>A response will not be stored if it includes a "Vary:" header
275         containing the match-all "*".</li>
276       </ol>
277     </section>
278
279     <section>
280       <title>What Should Not be Cached?</title>
281     
282       <p>In short, any content which is highly time-sensitive, or which varies
283       depending on the particulars of the request that are not covered by
284       HTTP negotiation, should not be cached.</p>
285
286       <p>If you have dynamic content which changes depending on the IP address
287       of the requester, or changes every 5 minutes, it should almost certainly
288       not be cached.</p>
289
290       <p>If on the other hand, the content served differs depending on the
291       values of various HTTP headers, it is possible that it might be possible
292       to cache it intelligently through the use of a "Vary" header.</p>
293     </section>
294
295     <section>
296       <title>Variable/Negotiated Content</title>
297
298       <p>If a response with a "Vary" header is received by 
299       <module>mod_cache</module> when requesting content by the backend it
300       will attempt to handle it intelligently. If possible, 
301       <module>mod_cache</module> will detect the headers attributed in the
302       "Vary" response in future requests and serve the correct cached 
303       response.</p>
304
305       <p>If for example, a response is received with a vary header such as;</p>
306
307       <example>
308 Vary: negotiate,accept-language,accept-charset
309       </example>
310
311       <p><module>mod_cache</module> will only serve the cached content to
312       requesters with matching accept-language and accept-charset headers
313       matching those of the original request.</p>
314     </section>
315  
316   </section>
317
318   <section id="security">
319     <title>Security Considerations</title>
320
321     <section>
322       <title>Authentication, Authorization and Access Control</title>
323
324       <p>Using <module>mod_cache</module> is very much like having a built
325       in reverse-proxy. Requests will be served by the caching module unless
326       it determines that the backend should be queried. When caching local
327       resources, this drastically changes the security model of Apache.</p>
328
329       <p>As traversing a filesystem hierarchy to examine potential
330       <code>.htaccess</code> files would be a very expensive operation,
331       partially defeating the point of caching (to speed up requests),
332       <module>mod_cache</module> makes no decision about whether a cached
333       entity is authorised for serving. In other words; if
334       <module>mod_cache</module> has cached some content, it will be served
335       from the cache as long as that content has not expired.</p>
336
337       <p>If, for example, your configuration permits access to a resource by IP
338       address you should ensure that this content is not cached. You can do this
339       by using the <directive module="mod_cache">CacheDisable</directive>
340       directive, or <module>mod_expires</module>. Left unchecked,
341       <module>mod_cache</module> - very much like a reverse proxy - would cache
342       the content when served and then serve it to any client, on any IP
343       address.</p>        
344     </section>
345
346     <section>
347       <title>Local exploits</title>
348
349       <p>As requests to end-users can be served from the cache, the cache
350       itself can become a target for those wishing to deface or interfere with
351       content. It is important to bear in mind that the cache must at all
352       times be writable by the user which Apache is running as. This is in 
353       stark contrast to the usually recommended situation of maintaining
354       all content unwritable by the Apache user.</p>
355
356       <p>If the Apache user is compromised, for example through a flaw in
357       a CGI process, it is possible that the cache may be targeted. When
358       using <module>mod_disk_cache</module>, it is relatively easy to 
359       insert or modify a cached entity.</p>
360
361       <p>This presents a somewhat elevated risk in comparison to the other 
362       types of attack it is possible to make as the Apache user. If you are 
363       using <module>mod_disk_cache</module> you should bear this in mind - 
364       ensure you upgrade Apache when security upgrades are announced and 
365       run CGI processes as a non-Apache user using <a 
366       href="suexec.html">suEXEC</a> if possible.</p>
367
368     </section>
369
370     <section>
371       <title>Cache Poisoning</title>
372
373       <p>When running Apache as a caching proxy server, there is also the
374       potential for so-called cache poisoning. Cache Poisoning is a broad 
375       term for attacks in which an attacker causes the proxy server to 
376       retrieve incorrect (and usually undesirable) content from the backend.
377       </p>
378
379       <p>For example if the DNS servers used by your system running Apache
380       are vulnerable to DNS cache poisoning, an attacker may be able to control
381       where Apache connects to when requesting content from the origin server.
382       Another example is so-called HTTP request-smuggling attacks.</p>
383
384       <p>This document is not the correct place for an in-depth discussion
385       of HTTP request smuggling (instead, try your favourite search engine)
386       however it is important to be aware that it is possible to make
387       a series of requests, and to exploit a vulnerability on an origin
388       webserver such that the attacker can entirely control the content
389       retrieved by the proxy.</p>
390     </section>
391   </section>
392
393   <section id="filehandle">
394     <title>File-Handle Caching</title>
395
396     <related>
397       <modulelist>
398         <module>mod_file_cache</module>
399         <module>mod_mem_cache</module>
400       </modulelist>
401       <directivelist>
402         <directive module="mod_file_cache">CacheFile</directive>
403         <directive module="mod_cache">CacheEnable</directive>
404         <directive module="mod_cache">CacheDisable</directive>
405       </directivelist>
406     </related>
407
408     <p>The act of opening a file can itself be a source of delay, particularly 
409     on network filesystems. By maintaining a cache of open file descriptors
410     for commonly served files, Apache can avoid this delay. Currently Apache
411     provides two different implementations of File-Handle Caching.</p> 
412
413     <section>
414       <title>CacheFile</title>
415
416       <p>The most basic form of caching present in Apache is the file-handle
417       caching provided by <module>mod_file_cache</module>. Rather than caching 
418       file-contents, this cache maintains a table of open file descriptors. Files 
419       to be cached in this manner are specified in the configuration file using
420       the <directive module="mod_file_cache">CacheFile</directive> 
421       directive.</p>
422
423       <p>The 
424       <directive module="mod_file_cache">CacheFile</directive> directive 
425       instructs Apache to open the file when Apache is started and to re-use 
426       this file-handle for all subsequent access to this file.</p>
427
428       <example>
429       <pre>CacheFile /usr/local/apache2/htdocs/index.html</pre>
430       </example>
431
432       <p>If you intend to cache a large number of files in this manner, you 
433       must ensure that your operating system's limit for the number of open 
434       files is set appropriately.</p>
435
436       <p>Although using <directive module="mod_file_cache">CacheFile</directive>
437       does not cause the file-contents to be cached per-se, it does mean
438       that if the file changes while Apache is running these changes will
439       not be picked up. The file will be consistently served as it was
440       when Apache was started.</p>
441
442       <p>If the file is removed while Apache is running, Apache will continue
443       to maintain an open file descriptor and serve the file as it was when
444       Apache was started. This usually also means that although the file
445       will have been deleted, and not show up on the filesystem, extra free
446       space will not be recovered until Apache is stopped and the file
447       descriptor closed.</p>
448     </section>
449
450     <section>
451       <title>CacheEnable fd</title>
452
453       <p><module>mod_mem_cache</module> also provides its own file-handle 
454       caching scheme, which can be enabled via the 
455       <directive module="mod_cache">CacheEnable</directive> directive.</p>
456
457       <example>
458       <pre>CacheEnable fd /</pre>
459       </example>
460
461       <p>As with all of <module>mod_cache</module> this type of file-handle
462       caching is intelligent, and handles will not be maintained beyond
463       the expiry time of the cached content.</p>
464     </section>
465   </section>
466   
467   <section id="inmemory">
468     <title>In-Memory Caching</title>
469
470      <related>
471       <modulelist>
472         <module>mod_mem_cache</module>
473         <module>mod_file_cache</module>
474       </modulelist>
475       <directivelist>
476         <directive module="mod_cache">CacheEnable</directive>
477         <directive module="mod_cache">CacheDisable</directive>
478         <directive module="mod_file_cache">MMapStatic</directive>
479       </directivelist>
480     </related>
481        
482     <p>Serving directly from system memory is universally the fastest method
483     of serving content. Reading files from a disk controller or, even worse,
484     from a remote network is orders of magnitude slower. Disk controllers
485     usually involve physical processes, and network access is limited by
486     your available bandwidth. Memory access on the other hand can take mere
487     nano-seconds.</p>
488
489     <p>System memory isn't cheap though, byte for byte it's by far the most
490     expensive type of storage and it's important to ensure that it is used
491     efficiently. By caching files in memory you decrease the amount of 
492     memory available on the system. As we'll see, in the case of operating
493     system caching, this is not so much of an issue, but when using
494     Apache's own in-memory caching it is important to make sure that you
495     do not allocate too much memory to a cache. Otherwise the system
496     will be forced to swap out memory, which will likely degrade 
497     performance.</p>
498
499     <section>
500       <title>Operating System Caching</title>
501
502       <p>Almost all modern operating systems cache file-data in memory managed
503       directly by the kernel. This is a powerful feature, and for the most
504       part operating systems get it right. For example, on Linux, let's look at
505       the difference in the time it takes to read a file for the first time
506       and the second time;</p>
507
508       <example><pre>
509 colm@coroebus:~$ time cat testfile &gt; /dev/null
510 real    0m0.065s
511 user    0m0.000s
512 sys     0m0.001s
513 colm@coroebus:~$ time cat testfile &gt; /dev/null
514 real    0m0.003s
515 user    0m0.003s
516 sys     0m0.000s</pre>
517       </example>
518
519       <p>Even for this small file, there is a huge difference in the amount
520       of time it takes to read the file. This is because the kernel has cached
521       the file contents in memory.</p>
522
523       <p>By ensuring there is "spare" memory on your system, you can ensure 
524       that more and more file-contents will be stored in this cache. This 
525       can be a very efficient means of in-memory caching, and involves no 
526       extra configuration of Apache at all.</p>
527
528       <p>Additionally, because the operating system knows when files are 
529       deleted or modified, it can automatically remove file contents from the 
530       cache when neccessary. This is a big advantage over Apache's in-memory 
531       caching which has no way of knowing when a file has changed.</p>
532     </section>
533
534     <p>Despite the performance and advantages of automatic operating system
535     caching there are some circumstances in which in-memory caching may be 
536     better performed by Apache.</p>
537
538     <p>Firstly, an operating system can only cache files it knows about. If 
539     you are running Apache as a proxy server, the files you are caching are
540     not locally stored but remotely served. If you still want the unbeatable
541     speed of in-memory caching, Apache's own memory caching is needed.</p>
542
543     <section>
544       <title>MMapStatic Caching</title>
545
546       <p><module>mod_file_cache</module> provides the 
547       <directive module="mod_file_cache">MMapStatic</directive> directive, which
548       allows you to have Apache map a static file's contents into memory at
549       start time (using the mmap system call). Apache will use the in-memory 
550       contents for all subsequent accesses to this file.</p>
551
552       <example>
553       <pre>MMapStatic /usr/local/apache2/htdocs/index.html</pre>
554       </example>
555
556       <p>As with the
557       <directive module="mod_file_cache">CacheFile</directive> directive, any
558       changes in these files will not be picked up by Apache after it has
559       started.</p>
560
561       <p> The <directive module="mod_file_cache">MMapStatic</directive> 
562       directive does not keep track of how much memory it allocates, so
563       you must ensure not to over-use the directive. Each Apache child
564       process will replicate this memory, so it is critically important
565       to ensure that the files mapped are not so large as to cause the
566       system to swap memory.</p>
567     </section>
568
569     <section>
570       <title>mod_mem_cache Caching</title>
571
572       <p><module>mod_mem_cache</module> provides a HTTP-aware intelligent
573       in-memory cache. It also uses heap memory directly, which means that
574       even if <var>MMap</var> is not supported on your system, 
575       <module>mod_mem_cache</module> may still be able to perform caching.</p>
576
577       <p>Caching of this type is enabled via;</p>
578
579       <example><pre>
580 # Enable memory caching
581 CacheEnable mem /
582
583 # Limit the size of the cache to 1 Megabyte
584 MCacheSize 1024</pre>
585       </example>
586     </section>
587   </section>
588              
589   <section id="disk">
590     <title>Disk-based Caching</title>
591
592      <related>
593       <modulelist>
594         <module>mod_disk_cache</module>
595       </modulelist>
596       <directivelist>
597         <directive module="mod_cache">CacheEnable</directive>
598         <directive module="mod_cache">CacheDisable</directive>
599       </directivelist>
600     </related>
601        
602     <p><module>mod_disk_cache</module> provides a disk-based caching mechanism 
603     for <module>mod_cache</module>. As with <module>mod_mem_cache</module>
604     this cache is intelligent and content will be served from the cache only
605     as long as it is considered valid.</p>
606
607     <p>Typically the module will be configured as so;</p>
608
609     <example>  
610     <pre>
611 CacheRoot   /var/cache/apache/
612 CacheEnable disk /
613 CacheDirLevels 2
614 CacheDirLength 1</pre>
615     </example>
616
617     <p>Importantly, as the cached files are locally stored, operating system
618     in-memory caching will typically be applied to their access also. So 
619     although the files are stored on disk, if they are frequently accessed 
620     it is likely the operating system will ensure that they are actually
621     served from memory.</p>
622
623     <section>
624       <title>Understanding the Cache-Store</title>
625
626       <p>To store items in the cache, <module>mod_disk_cache</module> creates
627       a 22 character hash of the url being requested. Thie hash incorporates
628       the hostname, protocol, port, path and any CGI arguments to the URL,
629       to ensure that multiple URLs do not collide.</p>
630
631       <p>Each character may be any one of 64-different characters, which mean
632       that overall there are 22^64 possible hashes. For example, a URL might
633       be hashed to <code>xyTGxSMO2b68mBCykqkp1w</code>. This hash is used
634       as a prefix for the naming of the files specific to that url within
635       the cache, however first it is split up into directories as per
636       the <directive module="mod_disk_cache">CacheDirLevels</directive> and
637       <directive module="mod_disk_cache">CacheDirLength</directive> 
638       directives.</p>
639
640       <p><directive module="mod_disk_cache">CacheDirLevels</directive> 
641       specifies how many levels of subdirectory there should be, and
642       <directive module="mod_disk_cache">CacheDirLength</directive>
643       specifies how many characters should be in each directory. With
644       the example settings given above, the hash would be turned into
645       a filename prefix as 
646       <code>/var/cache/apache/x/y/TGxSMO2b68mBCykqkp1w</code>.</p>
647
648       <p>The overall aim of this technique is to reduce the number of
649       subdirectories or files that may be in a particular directory,
650       as most file-systems slow down as this number increases. With
651       setting of "1" for 
652       <directive module="mod_disk_cache">CacheDirLength</directive>
653       there can at most be 64 subdirectories at any particular level. 
654       With a setting of 2 there can be 64 * 64 subdirectories, and so on.
655       Unless you have a good reason not to, using a setting of "1"
656       for <directive module="mod_disk_cache">CacheDirLength</directive>
657       is recommended.</p>
658
659       <p>Setting 
660       <directive module="mod_disk_cache">CacheDirLevels</directive>
661       depends on how many files you anticipate to store in the cache.
662       With the setting of "2" used in the above example, a grand
663       total of 4096 subdirectories can ultimately be created. With
664       1 million files cached, this works out at roughly 245 cached 
665       urls per directory.</p>
666
667       <p>Each url uses at least two files in the cache-store. Typically
668       there is a ".header" file, which includes meta-information about 
669       the url, such as when it is due to expire and a ".data" file
670       which is a verbatim copy of the content to be served.</p>
671
672       <p>In the case of a content negotiated via the "Vary" header, a
673       ".vary" directory will be created for the url in question. This 
674       directory will have multiple ".data" files corresponding to the
675       differently negotiated content.</p>
676     </section>
677
678     <section>
679       <title>Maintaining the Disk Cache</title>
680     
681       <p>Although <module>mod_disk_cache</module> will remove cached content
682       as it is expired, it does not maintain any information on the total
683       size of the cache or how little free space may be left.</p>
684
685       <p>Instead, provided with Apache is the <a 
686       href="programs/htcacheclean.html">htcacheclean</a> tool which, as the name
687       suggests, allows you to clean the cache periodically. Determining 
688       how frequently to run <a 
689       href="programs/htcacheclean.html">htcacheclean</a> and what target size to 
690       use for the cache is somewhat complex and trial and error may be needed to
691       select optimal values.</p>
692
693       <p><a href="programs/htcacheclean.html">htcacheclean</a> has two modes of 
694       operation. It can be run as persistent daemon, or periodically from 
695       cron. <a 
696       href="programs/htcacheclean.html">htcacheclean</a> can take up to an hour 
697       or more to process very large (tens of gigabytes) caches and if you are 
698       running it from cron it is recommended that you determine how long a typical 
699       run takes, to avoid running more than one instance at a time.</p>
700
701       <p class="figure">
702       <img src="images/caching_fig1.gif" alt="" width="600"
703           height="406" /><br />
704       <a id="figure1" name="figure1"><dfn>Figure 1</dfn></a>: Typical
705       cache growth / clean sequence.</p>
706
707       <p>Because <module>mod_disk_cache</module> does not itself pay attention
708       to how much space is used you should ensure that 
709       <a href="programs/htcacheclean.html">htcacheclean</a> is configured to 
710       leave enough "grow room" following a clean.</p>
711     </section>
712
713   </section>
714
715 </manualpage>