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

  1. Caching must be enabled forthis URL. See the CacheEnable and CacheDisable directives.

  2. The response must have a HTTP status code of 200, 203, 300, 301 or 410.

  3. The request must be a HTTP GET request.

  4. 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.

  5. If the URL included a query string (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.

  6. If the response has a status of 200 (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 CacheIgnoreNoLastMod directive has been used to require otherwise.

  7. If the response includes the"private" option in a "Cache-Control:" header, it will not be stored unless the CacheStorePrivate has been used to require otherwise.

  8. Likewise, if the responseincludes the "no-store" option in a "Cache-Control:"header, it will not be stored unless the CacheStoreNoStore has been used.

  9. 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 it by single thread, we are not seeing this problem though. This is related to cache 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 will see 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 srwd92lcx001.srwd92.com

X-Cache-Detail

"cache miss: attempting entity  save" from srwd92lcx001.srwd92.com

 

This let you have a clear picture ofwhether cache hits the reason if not hit

 

Enable logging

[httpd.conf]

LogLevel debug

 

Custom Log logs/cached-requests.log commonenv=cache-hit

Custom Log logs/uncached-requests.log commonenv=cache-miss

Custom Log logs/revalidated-requests.logcommon env=cache-revalidate

Custom Log logs/invalidated-requests.logcommon env=cache-invalidate

 

LogFormat "%{cache-status}e "cachelog

 

Custom Log 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 /solr/

CacheDirLevels 5

CacheDirLength 3

CacheMaxFileSize 1000000

CacheMinFileSize 64

#CacheLock on

#CacheLockPath /tmp/mod_cache-lock

 

SetEnvIf REQUEST_URI admin no-cache