- Overview
- Installation & Uninstallation
- Local and remote management using IIS 7
- Context and processing order
- Apache compatibility
- Modules
- core functions
- mod_antibot
- mod_asis
- mod_auth_basic
- mod_auth_digest
- mod_authn_anon
- mod_authn_dbd
- mod_authn_default
- mod_authn_file
- mod_authz_default
- mod_authz_groupfile
- mod_authz_host
- mod_authz_user
- mod_cache
- mod_dbd
- mod_deflate
- mod_developer
- mod_dir
- mod_disk_cache
- mod_env
- mod_evasive
- mod_expires
- mod_filter
- mod_gzip
- mod_headers
- mod_hotlink
- mod_linkfreeze
- mod_log_config
- mod_logio
- mod_mem_cache
- mod_mime
- mod_proxy
- mod_replace
- mod_rewrite
- mod_seo
- mod_setenvif
- mod_so
- mod_speling
- mod_usertrack
- mod_xsendfile
 
- Articles
- Release history
- Troubleshooting
- License agreement
mod_cache
Overview
mod_cache module provides HTTP content cache for local and proxied content. Cached content is stored/retrieved using URI-based keys.
The way it works
Quick start
Sample .htaccess configuration to enable memory-based cache for php files
<Files *.php>
  # setup expiration header in response (if app does not setup it)
  ExpiresActive On
  # response expire in a hour
  ExpiresByType text/html "access 1 hour"
  # enable memory-based cache
  CacheEnable mem
</Files>Sample httpd.conf configuration to enable disk-based cache for some blog application. Assumed that the HTTP responses contain expire time
<VirtualHost www.myblog.com>
  CacheRoot c:\inetpub\cache
  CacheEnable disk /blog/
</VirtualHost>Related articles and topics
- Web Caching: what is it?
- How mod_cache works?
- Guide: Example of mod_cache application
- Disk-based caching for IIS7
- mod_disk_cache
- mod_mem_cache
- mod_env
- mod_setenvif (extended syntax supported)
- mod_expires
- mod_headers
Enviroment variables
| Name | Context | Description | 
|---|---|---|
| cache-enable | S V D .h | enables or disables cache to current configuration or context | 
| cache-clear | S V D .h | clears cache related to the current http request | 
Examples
# enable memory-based cache
SetEnv cache-enable mem
# enable disk-based cache
SetEnv cache-enable disk
# disable cache
SetEnv cache-enable none
# clear cache if query string contains 'clear_cache_request' and client ip is 11.22.33.44
SetEnvIf (Query_String clear_cache_request) and (Remote_Addr 11\.22\.33\.44) cache-clear=1Directives
| Name | Context | Description | 
|---|---|---|
| CacheDefaultExpire | S V D .h | default cache duration when no expiry date is specified | 
| CacheDisable | S V D .h | disables caching of specified URLs | 
| CacheEnable | S V D .h | enables caching of specified URLs | 
| CacheIgnoreCacheControl | S V D .h | ignores request to not serve cached content to client | 
| CacheIgnoreHeaders | S V D .h | doesn't store specified HTTP header(s) in cache | 
| CacheIgnoreNoLastMod | S V D .h | ignores the fact that a response has no Last Modified header | 
| CacheLastModifiedFactor | S V D .h | sets the factor used to compute an expiry date based on the LastModified date | 
| CacheMaxExpire | S V D .h | sets maximum time in seconds to cache a document | 
| CacheStoreNoStore | S V D .h | attempts to cache requests or responses that have been marked as no-store | 
| CacheStorePrivate | S V D .h | attempts to cache responses that the server marked as private | 
| CacheVaryByParams | S V D .h | defines request parameters to be included into cache key | 
| CacheVaryByHeaders | S V D .h | defines request headers to be included into cache key | 
CacheDefaultExpire
   
    CacheDefaultExpire
   
   directive specifies a default time (in 
  seconds) during which the document will be stored in cache if expiry date and 
  last-modified date are not provided. The value specified in
   
    CacheMaxExpire
   
   directive 
  does not override this setting.
  
Syntax
CacheDefaultExpire secondsDefault
CacheDefaultExpire 3600Example
CacheDefaultExpire 86400CacheDisable
   
    CacheDisable
   
   directive stops caching for and below
   
    url-string
   
   .
   
    Url-string
   
   should be specified only for server config and must be omitted in all other 
  locations.
  
Syntax
CacheDisable [url-string]Example
# in server config
CacheDisable /some_url
# in .htaccess or directory config
CacheDisableCacheEnable
   
    CacheEnable
   
   directive allows caching for and below
   
    url-string
   
   .
   
    Url-string
   
   should be specified only for server config and must be omitted in all other 
  locations.
  
Syntax:
CacheEnable mem|disk [url-string]In order to use memory- or disk-based storage engine, the module must be loaded and configured if necessary. See mod_mem_cache and mod_disk_cache for details.
Example:
#in server config
CacheEnable mem /app1
CacheEnable disk /app2
# in .htaccess or directory config
CacheEnable mem
# or
CacheEnable disk
   Cache functionality can also be enabled/disabled by
   
    cache-enable
   
   environment variable.
  
# enable disk-based caching
SetEnv cache-enable mem
# disable caching
SetEnv cache-enable none
# or like this
UnSetEnv cache-enable
   
    Note!
   
   Value of
   
    cache-enable
   
   variable has higher priority than that of
   
    CacheEnable
   
   and
   
    CacheDisable
   
   directives.
  
CacheIgnoreCacheControl
   
    CacheIgnoreCacheControl
   
   directive allows to override behavior 
  required by
   
    Cache-Control: no-cache
   
   or
   
    Pragma: no-cache
   
   header values.
   
    CacheIgnoreCacheControl On
   
   tells the server to try serve 
  the resource from cache even if the request contains no-cache header values. 
  Resources requiring authorization will not be cached.
  
Syntax
CacheIgnoreCacheControl On|OffDefault
CacheIgnoreCacheControl OffCacheIgnoreHeaders
Not store specified HTTP header(s) in cache.
Not supported.
Syntax
CacheIgnoreHeaders header-string [header-string] [...]Default
CacheIgnoreHeaders NoneCacheIgnoreNoLastMod
   
    CacheIgnoreNoLastMod
   
   directive allows to say that documents 
  without last-modified dates should be cached. If neither last-modified date 
  nor expiry date are provided, the value specified by the
   
    CacheDefaultExpire
   
   directive will be used to generate an expiration date.
  
Syntax
CacheIgnoreNoLastMod On|OffDefault
CacheIgnoreNoLastMod OffCacheLastModifiedFactor
   
    CacheLastModifiedFactor
   
   directive allows calculate document 
  expiry date based on its last-modified date.
  
Syntax
CacheLastModifiedFactor floatDefault
CacheLastModifiedFactor 0.1
   
    CacheLastModifiedFactor
   
   directive specifies a factor to be used in the generation of expiry date 
  according to the following formula:
  
   
    expiry-period = time-since-last-modified-date * factor
   
  
   
    expiry-date = current-date + expiry-period
   
  
Example
   If the document was last modified 10 hours 
  ago, and factor is 0.1 then the expiry-period will be 10*0.1 = 1 hour. If the 
  current time is 3:00pm, the computed expiry-date is 3:00pm + 1hour = 4:00pm. 
  If the
   
    expiry-period
   
   is longer than that set by
   
    CacheMaxExpire
   
   , 
  the latter prevails.
  
CacheMaxExpire
   
    CacheMaxExpire
   
   directive specifies the maximum number of 
  seconds for which cached documents will be retrieved from cache rather than 
  taken from the origin server.
   
    CacheMaxExpire
   
   value is used only if expiration time is
  generated using
   
    CacheLastModifiedFactor
   
   algorithm.
  
Syntax
CacheMaxExpire secondsDefault
CacheMaxExpire 86400CacheStoreNoStore
   
    CacheStoreNoStore
   
   directive allows to enable caching for 
  requests or responses with
   
    Cache-Control: no-store
   
   header value 
  that normally aren't cached. Resources requiring authorization will never be 
  cached.
  
Syntax
CacheStoreNoStore On|OffDefault
CacheStoreNoStore OffCacheStorePrivate
   
    CacheStorePrivate
   
   directive allows to enable caching for 
  responses with
   
    Cache-Control: private
   
   header value that normally 
  aren't cached. Resources requiring authorization will never be cached.
  
Syntax
CacheStorePrivate On|OffDefault
CacheStorePrivate OffCacheVaryByParams
   
    CacheVaryByParams
   
   directive allows to define request parameters 
  that will be included into the cache key. The following requests
   
    /index.php?category_id=1&mode=1
   
   and
   
    /index.php?category_id=1&mode=2
   
   will have the same cache key 
  despite the difference in
   
    mode
   
   parameter. In its turn this means 
  that the second request
   
    /index.php?category_id=1&mode=2
   
   will use 
  the response cached for the first request
   
    /index.php?category_id=1&mode=1
   
   .
  
Syntax
CacheVaryByParams None|param-string [param-string] [...]Default
By default all query parameters are included into cache key
Example
CacheVaryByParams category_id product_id
   
    Note!
   
   
    CacheVaryByParams
   
   directive 
  has no equivalent in Apache (see
   
    Compatibility 
  chart
   
   ).
  
CacheVaryByHeaders
   
    CacheVaryByHeaders
   
   directive allows to define request headers 
  that will be included into the cache key. The following identical requests with 
  different headers
   
    Cookie: auth=1
   
   and
   
    Cookie: auth=2
   
   will have different caching keys and consequently different response versions 
  in cache.
  
Syntax
CacheVaryByHeaders None|header-string [header-string] [...]Default
CacheVaryByHeaders NoneExample
CacheVaryByHeaders CookieNote! CacheVaryByHeaders directive has no equivalent in Apache (see Compatibility chart ).