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

  • The actions that are performed after authentication/authorization events but prior to request handler execution
  • The essential request requirements to cache the response
  • The actions that are performed after request handler and filters have been applied to response
  • The conditions that need to be met to cache response
  • Cache key generation
  • The methods of calculating period of time of HTTP response stored in cache

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

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

Directives

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 seconds

Default

CacheDefaultExpire 3600

Example

CacheDefaultExpire 86400

CacheDisable

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
CacheDisable

CacheEnable

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

Default

CacheIgnoreCacheControl Off

CacheIgnoreHeaders

Not store specified HTTP header(s) in cache.

Not supported.

Syntax

CacheIgnoreHeaders header-string [header-string] [...]

Default

CacheIgnoreHeaders None

CacheIgnoreNoLastMod

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

Default

CacheIgnoreNoLastMod Off

CacheLastModifiedFactor

CacheLastModifiedFactor directive allows calculate document expiry date based on its last-modified date.

Syntax

CacheLastModifiedFactor float

Default

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 seconds

Default

CacheMaxExpire 86400

CacheStoreNoStore

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

Default

CacheStoreNoStore Off

CacheStorePrivate

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

Default

CacheStorePrivate Off

CacheVaryByParams

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 None

Example

CacheVaryByHeaders Cookie

Note! CacheVaryByHeaders directive has no equivalent in Apache (see Compatibility chart ).