📄 caching.html.en
字号:
usually involve physical processes, and network access is limited by your available bandwidth. Memory access on the other hand can take mere nano-seconds.</p> <p>System memory isn't cheap though, byte for byte it's by far the most expensive type of storage and it's important to ensure that it is used efficiently. By caching files in memory you decrease the amount of memory available on the system. As we'll see, in the case of operating system caching, this is not so much of an issue, but when using Apache's own in-memory caching it is important to make sure that you do not allocate too much memory to a cache. Otherwise the system will be forced to swap out memory, which will likely degrade performance.</p> <h3>Operating System Caching</h3> <p>Almost all modern operating systems cache file-data in memory managed directly by the kernel. This is a powerful feature, and for the most part operating systems get it right. For example, on Linux, let's look at the difference in the time it takes to read a file for the first time and the second time;</p> <div class="example"><pre>colm@coroebus:~$ time cat testfile > /dev/nullreal 0m0.065suser 0m0.000ssys 0m0.001scolm@coroebus:~$ time cat testfile > /dev/nullreal 0m0.003suser 0m0.003ssys 0m0.000s</pre></div> <p>Even for this small file, there is a huge difference in the amount of time it takes to read the file. This is because the kernel has cached the file contents in memory.</p> <p>By ensuring there is "spare" memory on your system, you can ensure that more and more file-contents will be stored in this cache. This can be a very efficient means of in-memory caching, and involves no extra configuration of Apache at all.</p> <p>Additionally, because the operating system knows when files are deleted or modified, it can automatically remove file contents from the cache when neccessary. This is a big advantage over Apache's in-memory caching which has no way of knowing when a file has changed.</p> <p>Despite the performance and advantages of automatic operating system caching there are some circumstances in which in-memory caching may be better performed by Apache.</p> <p>Firstly, an operating system can only cache files it knows about. If you are running Apache as a proxy server, the files you are caching are not locally stored but remotely served. If you still want the unbeatable speed of in-memory caching, Apache's own memory caching is needed.</p> <h3>MMapStatic Caching</h3> <p><code class="module"><a href="./mod/mod_file_cache.html">mod_file_cache</a></code> provides the <code class="directive"><a href="./mod/mod_file_cache.html#mmapstatic">MMapStatic</a></code> directive, which allows you to have Apache map a static file's contents into memory at start time (using the mmap system call). Apache will use the in-memory contents for all subsequent accesses to this file.</p> <div class="example"><pre>MMapStatic /usr/local/apache2/htdocs/index.html</pre></div> <p>As with the <code class="directive"><a href="./mod/mod_file_cache.html#cachefile">CacheFile</a></code> directive, any changes in these files will not be picked up by Apache after it has started.</p> <p> The <code class="directive"><a href="./mod/mod_file_cache.html#mmapstatic">MMapStatic</a></code> directive does not keep track of how much memory it allocates, so you must ensure not to over-use the directive. Each Apache child process will replicate this memory, so it is critically important to ensure that the files mapped are not so large as to cause the system to swap memory.</p> <h3>mod_mem_cache Caching</h3> <p><code class="module"><a href="./mod/mod_mem_cache.html">mod_mem_cache</a></code> provides a HTTP-aware intelligent in-memory cache. It also uses heap memory directly, which means that even if <var>MMap</var> is not supported on your system, <code class="module"><a href="./mod/mod_mem_cache.html">mod_mem_cache</a></code> may still be able to perform caching.</p> <p>Caching of this type is enabled via;</p> <div class="example"><pre># Enable memory cachingCacheEnable mem /# Limit the size of the cache to 1 MegabyteMCacheSize 1024</pre></div> </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="disk" id="disk">Disk-based Caching</a></h2> <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_disk_cache.html">mod_disk_cache</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_cache.html#cacheenable">CacheEnable</a></code></li><li><code class="directive"><a href="./mod/mod_cache.html#cachedisable">CacheDisable</a></code></li></ul></td></tr></table> <p><code class="module"><a href="./mod/mod_disk_cache.html">mod_disk_cache</a></code> provides a disk-based caching mechanism for <code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code>. As with <code class="module"><a href="./mod/mod_mem_cache.html">mod_mem_cache</a></code> this cache is intelligent and content will be served from the cache only as long as it is considered valid.</p> <p>Typically the module will be configured as so;</p> <div class="example"><pre>CacheRoot /var/cache/apache/CacheEnable disk /CacheDirLevels 2CacheDirLength 1</pre></div> <p>Importantly, as the cached files are locally stored, operating system in-memory caching will typically be applied to their access also. So although the files are stored on disk, if they are frequently accessed it is likely the operating system will ensure that they are actually served from memory.</p> <h3>Understanding the Cache-Store</h3> <p>To store items in the cache, <code class="module"><a href="./mod/mod_disk_cache.html">mod_disk_cache</a></code> creates a 22 character hash of the URL being requested. This hash incorporates the hostname, protocol, port, path and any CGI arguments to the URL, to ensure that multiple URLs do not collide.</p> <p>Each character may be any one of 64-different characters, which mean that overall there are 64^22 possible hashes. For example, a URL might be hashed to <code>xyTGxSMO2b68mBCykqkp1w</code>. This hash is used as a prefix for the naming of the files specific to that URL within the cache, however first it is split up into directories as per the <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlevels">CacheDirLevels</a></code> and <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlength">CacheDirLength</a></code> directives.</p> <p><code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlevels">CacheDirLevels</a></code> specifies how many levels of subdirectory there should be, and <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlength">CacheDirLength</a></code> specifies how many characters should be in each directory. With the example settings given above, the hash would be turned into a filename prefix as <code>/var/cache/apache/x/y/TGxSMO2b68mBCykqkp1w</code>.</p> <p>The overall aim of this technique is to reduce the number of subdirectories or files that may be in a particular directory, as most file-systems slow down as this number increases. With setting of "1" for <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlength">CacheDirLength</a></code> there can at most be 64 subdirectories at any particular level. With a setting of 2 there can be 64 * 64 subdirectories, and so on. Unless you have a good reason not to, using a setting of "1" for <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlength">CacheDirLength</a></code> is recommended.</p> <p>Setting <code class="directive"><a href="./mod/mod_disk_cache.html#cachedirlevels">CacheDirLevels</a></code> depends on how many files you anticipate to store in the cache. With the setting of "2" used in the above example, a grand total of 4096 subdirectories can ultimately be created. With 1 million files cached, this works out at roughly 245 cached URLs per directory.</p> <p>Each URL uses at least two files in the cache-store. Typically there is a ".header" file, which includes meta-information about the URL, such as when it is due to expire and a ".data" file which is a verbatim copy of the content to be served.</p> <p>In the case of a content negotiated via the "Vary" header, a ".vary" directory will be created for the URL in question. This directory will have multiple ".data" files corresponding to the differently negotiated content.</p> <h3>Maintaining the Disk Cache</h3> <p>Although <code class="module"><a href="./mod/mod_disk_cache.html">mod_disk_cache</a></code> will remove cached content as it is expired, it does not maintain any information on the total size of the cache or how little free space may be left.</p> <p>Instead, provided with Apache is the <a href="programs/htcacheclean.html">htcacheclean</a> tool which, as the name suggests, allows you to clean the cache periodically. Determining how frequently to run <a href="programs/htcacheclean.html">htcacheclean</a> and what target size to use for the cache is somewhat complex and trial and error may be needed to select optimal values.</p> <p><a href="programs/htcacheclean.html">htcacheclean</a> has two modes of operation. It can be run as persistent daemon, or periodically from cron. <a href="programs/htcacheclean.html">htcacheclean</a> can take up to an hour or more to process very large (tens of gigabytes) caches and if you are running it from cron it is recommended that you determine how long a typical run takes, to avoid running more than one instance at a time.</p> <p class="figure"> <img src="images/caching_fig1.gif" alt="" width="600" height="406" /><br /> <a id="figure1" name="figure1"><dfn>Figure 1</dfn></a>: Typical cache growth / clean sequence.</p> <p>Because <code class="module"><a href="./mod/mod_disk_cache.html">mod_disk_cache</a></code> does not itself pay attention to how much space is used you should ensure that <a href="programs/htcacheclean.html">htcacheclean</a> is configured to leave enough "grow room" following a clean.</p> </div></div><div class="bottomlang"><p><span>Available Languages: </span><a href="./en/caching.html" title="English"> en </a> |<a href="./fr/caching.html" hreflang="fr" rel="alternate" title="Fran鏰is"> fr </a> |<a href="./tr/caching.html" hreflang="tr" rel="alternate" title="T黵k鏴"> tr </a></p></div><div id="footer"><p class="apache">Copyright 2008 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p><p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -