Apache Mod cache usage and configuration

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 HTTPstatus code of 200, 203, 300, 301 or 410.

  3. The request must be a HTTP GETrequest.

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

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

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

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


转载于:https://my.oschina.net/stubhub/blog/285869

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值