/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/
/* * Author: mod_file_cache by Bill Stoddard <stoddard apache.org> * Based on mod_mmap_static by Dean Gaudet <dgaudet arctic.org> * * v0.01: initial implementation
*/
/* Documentation:
Some sites have a set of static files that are really busy, and change infrequently (or even on a regular schedule). Save time by caching open handles to these files. This module, unlike mod_mmap_static, caches open file handles, not file content. On systems (like Windows) with heavy system call overhead and that have an efficient sendfile implementation, caching file handles offers several advantages over caching content. First, the file system can manage the memory, allowing infrequently hit cached files to be paged out. Second, since caching open handles does not consume significant resources, it will be possible to enable an AutoLoadCache feature where static files are dynamically loaded in the cache as the server runs. On systems that have file change notification, this module can be enhanced to automatically garbage collect cached files that change on disk.
This module should work on Unix systems that have sendfile. Place cachefile directives into your configuration to direct files to be cached.
These files are only cached when the server is restarted, so if you change the list, or if the files are changed, then you'll need to restart the server.
To reiterate that point: if the files are modified *in place* without restarting the server you may end up serving requests that are completely bogus. You should update files by unlinking the old copy and putting a new copy in place.
There's no such thing as inheriting these files across vhosts or whatever... place the directives in the main server only.
Known problems:
Don't use Alias or RewriteRule to move these files around... unless you feel like paying for an extra stat() on each request. This is a deficiency in the Apache API that will hopefully be solved some day. The file will be served out of the file handle cache, but there will be an extra stat() that's a waste.
*/
#include"apr.h"
#if !(APR_HAS_SENDFILE || APR_HAS_MMAP) #error mod_file_cache only works on systems with APR_HAS_SENDFILE or APR_HAS_MMAP #endif
staticconstchar *cachefilehandle(cmd_parms *cmd, void *dummy, constchar *filename)
{ #if APR_HAS_SENDFILE
cache_the_file(cmd, filename, 0); #else /* Sendfile not supported by this OS */
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00800) "unable to cache file: %s. Sendfile is not supported on this OS", filename); #endif return NULL;
} staticconstchar *cachefilemmap(cmd_parms *cmd, void *dummy, constchar *filename)
{ #if APR_HAS_MMAP
cache_the_file(cmd, filename, 1); #else /* MMAP not supported by this OS */
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00801) "unable to cache file: %s. MMAP is not supported by this OS", filename); #endif return NULL;
}
staticint file_cache_post_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{ /* Hummm, anything to do here? */ return OK;
}
/* If it's one of ours, fill in r->finfo now to avoid extra stat()... this is a * bit of a kludge, because we really want to run after core_translate runs.
*/ staticint file_cache_xlat(request_rec *r)
{
a_server_config *sconf;
a_file *match; int res;
staticint file_cache_handler(request_rec *r)
{
a_file *match; int errstatus; int rc = OK;
/* Bail out if r->handler isn't the default value, and doesn't look like a Content-Type * XXX: Even though we made the user explicitly list each path to cache?
*/ if (ap_strcmp_match(r->handler, "*/*") && !AP_IS_DEFAULT_HANDLER_NAME(r->handler)) { return DECLINED;
}
/* we don't handle anything but GET */ if (r->method_number != M_GET) return DECLINED;
/* did xlat phase find the file? */
match = ap_get_module_config(r->request_config, &file_cache_module);
if (match == NULL) { return DECLINED;
}
/* note that we would handle GET on this resource */
r->allowed |= (AP_METHOD_BIT << M_GET);
/* This handler has no use for a request body (yet), but we still * need to read and discard it if the client sent one.
*/ if ((errstatus = ap_discard_request_body(r)) != OK) return errstatus;
ap_update_mtime(r, match->finfo.mtime);
/* ap_set_last_modified() always converts the file mtime to a string * which is slow. Accelerate the common case. * ap_set_last_modified(r);
*/
{
apr_time_t mod_time; char *datestr;
/* ap_set_content_length() always converts the same number and never * returns an error. Accelerate it.
*/
r->clength = match->finfo.size;
apr_table_setn(r->headers_out, "Content-Length", match->sizestr);
staticconst command_rec file_cache_cmds[] =
{
AP_INIT_ITERATE("cachefile", cachefilehandle, NULL, RSRC_CONF, "A space separated list of files to add to the file handle cache at config time"),
AP_INIT_ITERATE("mmapfile", cachefilemmap, NULL, RSRC_CONF, "A space separated list of files to mmap at config time"),
{NULL}
};
staticvoid register_hooks(apr_pool_t *p)
{
ap_hook_handler(file_cache_handler, NULL, NULL, APR_HOOK_LAST);
ap_hook_post_config(file_cache_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_translate_name(file_cache_xlat, NULL, NULL, APR_HOOK_MIDDLE); /* This trick doesn't work apparently because the translate hooks are single shot. If the core_hook returns OK, then our hook is not called. ap_hook_translate_name(file_cache_xlat, aszPre, NULL, APR_HOOK_MIDDLE);
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.