Apache mod cache
Two files will be created, containing cache-data and cache-header
To store items in the cache, mod_cache_disk 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, as well aselements defined by the Vary header to ensure that multiple URLs do not collide with one another. This hash is used as a prefix for the naming of the files specific to that URL within the cache
Apache will check cache freshness by comparing current request headers and cached headers
Cache or not cache
Caching must be enabled forthis URL. See the CacheEnable and CacheDisable directives.
The response must have a HTTPstatus code of 200, 203, 300, 301 or 410.
The request must be a HTTP GETrequest.
If the response contains an"Authorization:" header, it must also contain an"s-maxage", "must-revalidate" or "public" optionin the "Cache-Control:" header, or it won't be cached.
If the URL included a querystring (e.g. from a HTML form GET method) it will not be cached unless the response specifies an explicit expiration by including an "Expires:"header or the max-age or s-maxage directive of the "Cache-Control:"header, as per RFC2616 sections 13.9 and 13.2.1.
If the response has a status of200 (OK), the response must also include at least one of the "Etag","Last-Modified" or the "Expires" headers, or the max-age ors-maxage directive of the "Cache-Control:" header, unless the CacheIgnoreNoLastModdirective has been used to require otherwise.
If the response includes the"private" option in a "Cache-Control:" header, it will notbe stored unless the CacheStorePrivate has been used to require otherwise.
Likewise, if the responseincludes the "no-store" option in a "Cache-Control:"header, it will not be stored unless the CacheStoreNoStore has been used.
A response will not be storedif it includes a "Vary:" header containing the match-all"*".
See http://httpd.apache.org/docs/trunk/caching.html
Concurrency issue:
We found that when multi thread isaccessing apache cache, there are lot of 304 response returned. When we test itby single thread, we are not seeing this problem though. This is related tocache lock.
The cache lock is a mechanism to avoid overwhelming the back end. When cache expires, a lot of requests will hit backend concurrently, which creates load spike. Cache lock is an indicate that cache is being updated and request should not go to backend. The parameterCacheLockMaxAge determines how long the cache should be locked. Its value should be governed by how fast cache reload can be performed.
The frequency of seeing 304 is highly dependson the parameter CacheLockMaxAge. The larger value, the more frequent we willsee 304. If we disable cacheLock, we will not see 304 anymore.
Normally we want different cache strategyto be applied to different url
We allow a path string
Eg: CacheEnable disk /solr/
Note that apache cache does not support pattern match in url, so any * in the url is not interpreted correctly. So if we are to disable caching for certain url pattern
SetEnvIf REQUEST_URI admin no-cache
Debug:
CacheHeader on
CacheDetailHeader on
Response contains header: X-Cache andX-Cache-detail
Eg:
X-Cache | MISS from www.mydomain.com |
X-Cache-Detail | "cache miss: attempting entity save" from www.mydomain.com |
This let you have a clear picture ofwhether cache hits the reason if not hit
Enable logging
[httpd.conf]
LogLevel debug
CustomLog logs/cached-requests.log commonenv=cache-hit
CustomLog logs/uncached-requests.log commonenv=cache-miss
CustomLog logs/revalidated-requests.logcommon env=cache-revalidate
CustomLog logs/invalidated-requests.logcommon env=cache-invalidate
LogFormat "%{cache-status}e "cachelog
CustomLog logs/cache.log cachelog
Sample configuration
CacheRoot /tmp/cache
CacheLastModifiedFactor 0.5
CacheMaxExpire 300
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod on
CacheDefaultExpire 300
CacheIgnoreCacheControl on
CacheEnable disk /test/
CacheDirLevels 5
CacheDirLength 3
CacheMaxFileSize 1000000
CacheMinFileSize 64
#CacheLock on
#CacheLockPath /tmp/mod_cache-lock
SetEnvIf REQUEST_URI admin no-cache