📄 mod_disk_cache.c
字号:
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed 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. */#include "apr_file_io.h"#include "apr_strings.h"#include "mod_cache.h"#include "ap_provider.h"#include "util_filter.h"#include "util_script.h"#if APR_HAVE_UNISTD_H#include <unistd.h> /* needed for unlink/link */#endif/* Our on-disk header format is: * * disk_cache_info_t * entity name (dobj->name) [length is in disk_cache_info_t->name_len] * r->headers_out (delimited by CRLF) * CRLF * r->headers_in (delimited by CRLF) * CRLF */#define DISK_FORMAT_VERSION 0typedef struct { /* Indicates the format of the header struct stored on-disk. */ int format; /* The HTTP status code returned for this response. */ int status; /* The size of the entity name that follows. */ apr_size_t name_len; /* The number of times we've cached this entity. */ apr_size_t entity_version; /* Miscellaneous time values. */ apr_time_t date; apr_time_t expire; apr_time_t request_time; apr_time_t response_time;} disk_cache_info_t;/* * disk_cache_object_t * Pointed to by cache_object_t::vobj */typedef struct disk_cache_object { const char *root; /* the location of the cache directory */ char *tempfile; /* temp file tohold the content */#if 0 int dirlevels; /* Number of levels of subdirectories */ int dirlength; /* Length of subdirectory names */#endif char *datafile; /* name of file where the data will go */ char *hdrsfile; /* name of file where the hdrs will go */ char *hashfile; /* Computed hash key for this URI */ char *name; apr_file_t *fd; /* data file */ apr_file_t *hfd; /* headers file */ apr_file_t *tfd; /* temporary file for data */ apr_off_t file_size; /* File size of the cached data file */ disk_cache_info_t disk_info; /* Header information. */} disk_cache_object_t;/* * mod_disk_cache configuration *//* TODO: Make defaults OS specific */#define CACHEFILE_LEN 20 /* must be less than HASH_LEN/2 */#define DEFAULT_DIRLEVELS 3#define DEFAULT_DIRLENGTH 2#define DEFAULT_MIN_FILE_SIZE 1#define DEFAULT_MAX_FILE_SIZE 1000000#define DEFAULT_CACHE_SIZE 1000000typedef struct { const char* cache_root; apr_size_t cache_root_len; off_t space; /* Maximum cache size (in 1024 bytes) */ apr_time_t maxexpire; /* Maximum time to keep cached files in msecs */ apr_time_t defaultexpire; /* default time to keep cached file in msecs */ double lmfactor; /* factor for estimating expires date */ apr_time_t gcinterval; /* garbage collection interval, in msec */ int dirlevels; /* Number of levels of subdirectories */ int dirlength; /* Length of subdirectory names */ int expirychk; /* true if expiry time is observed for cached files */ apr_size_t minfs; /* minumum file size for cached files */ apr_size_t maxfs; /* maximum file size for cached files */ apr_time_t mintm; /* minimum time margin for caching files */ /* dgc_time_t gcdt; time of day for daily garbage collection */ apr_array_header_t *gcclnun; /* gc_retain_t entries for unused files */ apr_array_header_t *gcclean; /* gc_retain_t entries for all files */ int maxgcmem; /* maximum memory used by garbage collection */} disk_cache_conf;module AP_MODULE_DECLARE_DATA disk_cache_module;/* Forward declarations */static int remove_entity(cache_handle_t *h);static apr_status_t store_headers(cache_handle_t *h, request_rec *r, cache_info *i);static apr_status_t store_body(cache_handle_t *h, request_rec *r, apr_bucket_brigade *b);static apr_status_t recall_headers(cache_handle_t *h, request_rec *r);static apr_status_t recall_body(cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);/* * Local static functions */#define CACHE_HEADER_SUFFIX ".header"#define CACHE_DATA_SUFFIX ".data"static char *header_file(apr_pool_t *p, disk_cache_conf *conf, disk_cache_object_t *dobj, const char *name){ if (!dobj->hashfile) { dobj->hashfile = generate_name(p, conf->dirlevels, conf->dirlength, name); } return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile, CACHE_HEADER_SUFFIX, NULL);}static char *data_file(apr_pool_t *p, disk_cache_conf *conf, disk_cache_object_t *dobj, const char *name){ if (!dobj->hashfile) { dobj->hashfile = generate_name(p, conf->dirlevels, conf->dirlength, name); } return apr_pstrcat(p, conf->cache_root, "/", dobj->hashfile, CACHE_DATA_SUFFIX, NULL);}static void mkdir_structure(disk_cache_conf *conf, char *file, apr_pool_t *pool){ apr_status_t rv; char *p; for (p = file + conf->cache_root_len + 1;;) { p = strchr(p, '/'); if (!p) break; *p = '\0'; rv = apr_dir_make(file, APR_UREAD|APR_UWRITE|APR_UEXECUTE, pool); if (rv != APR_SUCCESS && !APR_STATUS_IS_EEXIST(rv)) { /* XXX */ } *p = '/'; ++p; }}static apr_status_t file_cache_el_final(disk_cache_object_t *dobj, request_rec *r){ /* move the data over */ if (dobj->tfd) { apr_status_t rv; apr_file_close(dobj->tfd); /* This assumes that the tempfile is on the same file system * as the cache_root. If not, then we need a file copy/move * rather than a rename. */ rv = apr_file_rename(dobj->tempfile, dobj->datafile, r->pool); if (rv != APR_SUCCESS) { /* XXX log */ } dobj->tfd = NULL; } return APR_SUCCESS;}static apr_status_t file_cache_errorcleanup(disk_cache_object_t *dobj, request_rec *r){ /* Remove the header file and the body file. */ apr_file_remove(dobj->hdrsfile, r->pool); apr_file_remove(dobj->datafile, r->pool); /* If we opened the temporary data file, close and remove it. */ if (dobj->tfd) { apr_file_close(dobj->tfd); apr_file_remove(dobj->tempfile, r->pool); dobj->tfd = NULL; } return APR_SUCCESS;}/* These two functions get and put state information into the data * file for an ap_cache_el, this state information will be read * and written transparent to clients of this module */static int file_cache_recall_mydata(apr_file_t *fd, cache_info *info, disk_cache_object_t *dobj, request_rec *r){ apr_status_t rv; char *urlbuff; disk_cache_info_t disk_info; apr_size_t len; /* read the data from the cache file */ len = sizeof(disk_cache_info_t); rv = apr_file_read_full(fd, &disk_info, len, &len); if (rv != APR_SUCCESS) { return rv; } if (disk_info.format != DISK_FORMAT_VERSION) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "cache_disk: URL %s had a on-disk version mismatch", r->uri); return APR_EGENERAL; } /* Store it away so we can get it later. */ dobj->disk_info = disk_info; info->date = disk_info.date; info->expire = disk_info.expire; info->request_time = disk_info.request_time; info->response_time = disk_info.response_time; /* Note that we could optimize this by conditionally doing the palloc * depending upon the size. */ urlbuff = apr_palloc(r->pool, disk_info.name_len + 1); len = disk_info.name_len; rv = apr_file_read_full(fd, urlbuff, len, &len); if (rv != APR_SUCCESS) { return rv; } urlbuff[disk_info.name_len] = '\0'; /* check that we have the same URL */ /* Would strncmp be correct? */ if (strcmp(urlbuff, dobj->name) != 0) { return APR_EGENERAL; } return APR_SUCCESS;}/* * Hook and mod_cache callback functions */#define AP_TEMPFILE "/aptmpXXXXXX"static int create_entity(cache_handle_t *h, request_rec *r, const char *key, apr_off_t len){ disk_cache_conf *conf = ap_get_module_config(r->server->module_config, &disk_cache_module); apr_status_t rv; cache_object_t *obj; disk_cache_object_t *dobj; apr_file_t *tmpfile; if (conf->cache_root == NULL) { return DECLINED; } /* If the Content-Length is still unknown, cache anyway */ if (len != -1 && (len < conf->minfs || len > conf->maxfs)) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "cache_disk: URL %s failed the size check, " "or is incomplete", key); return DECLINED; } /* Allocate and initialize cache_object_t and disk_cache_object_t */ h->cache_obj = obj = apr_pcalloc(r->pool, sizeof(*obj)); obj->vobj = dobj = apr_pcalloc(r->pool, sizeof(*dobj)); obj->key = apr_pstrdup(r->pool, key); /* XXX Bad Temporary Cast - see cache_object_t notes */ obj->info.len = (apr_size_t) len; obj->complete = 0; /* Cache object is not complete */ dobj->name = obj->key; dobj->datafile = data_file(r->pool, conf, dobj, key); dobj->hdrsfile = header_file(r->pool, conf, dobj, key); dobj->tempfile = apr_pstrcat(r->pool, conf->cache_root, AP_TEMPFILE, NULL); return OK;}static int open_entity(cache_handle_t *h, request_rec *r, const char *key){ apr_status_t rc; static int error_logged = 0; disk_cache_conf *conf = ap_get_module_config(r->server->module_config, &disk_cache_module); apr_finfo_t finfo; cache_object_t *obj; cache_info *info; disk_cache_object_t *dobj; int flags; h->cache_obj = NULL; /* Look up entity keyed to 'url' */ if (conf->cache_root == NULL) { if (!error_logged) { error_logged = 1; ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -