By default, Site Factory sets the maximum age for items included
in the Varnish® page cache (page_cache_maximum_age
) to 3600 seconds
(60 minutes). It is possible to customize this value based on your
website’s needs:
Increasing all cache lifetimes on all websites
To increase the cache lifetime value from the default for all websites
in your Factory that run Drupal 7 or the current Drupal version,
create a post-settings-php
hook as described in
Hooks triggered after website actions,
setting a new value (in seconds) for page_cache_maximum_age
:
// For Drupal 7, uncomment this line to set a new cache lifetime:
// $conf['page_cache_maximum_age'] = 21600;
// For the current Drupal version, uncomment this line to set a new cache lifetime:
// $config['system.performance']['cache']['page']['max_age'] = 21600;
Items in the page cache will now persist for 6 hours (21,600 seconds).
Per-environment cache lifetime increases
To increase the cache lifetime value from the default for all websites
in your Factory that run Drupal 7 or the current Drupal version,
create a post-settings-php
hook as described in
Hooks triggered after website actions,
altering the code to match your website’s installed version of Drupal:
// Verify we're on an Acquia-hosted environment.
if (file_exists('/var/www/site-php')) {
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
// Alter '01dev,' '01test', and '01live' to match
// your website's environment names.
// Alter the variable names to match your version of Drupal:
// The current Drupal version uses $config['system.performance']['cache']['page']['max_age']
// Drupal 7 uses $conf['page_cache_maximum_age']
switch ($_ENV['AH_SITE_ENVIRONMENT']) {
case '01dev':
// Cache settings.
$config['page_cache_maximum_age'] = 0;
break;
case '01test':
// Cache settings.
$config['page_cache_maximum_age'] = 3600;
break;
case '01live':
// Cache settings.
$config['page_cache_maximum_age'] = 21600;
break;
}
}
}
Items in the page cache on the testing environment will now persist for 60 minutes (3,600 seconds), and items in the production environment will persist for 6 hours (21,600 seconds). Items in the development environment will not be cached.
Increasing cache lifetimes with Drush in Drupal 7
You can also use Drush to update your website’s settings in Drupal 7.
First, create a post-settings-php
hook as described in
Hooks triggered after website actions:
<?php
/**
* @file
*
* This post-settings-php hook is created to conditionally set the cache
* lifetime of Drupal to be a value that is greater than 300 (5 minutes).
* It also does not let you set it to be lower than 5 minutes.
*
* This does not fire on Drush requests, as it interferes with site
* creation. It also means that drush will report back incorrect values for
* the cache lifetime, so using a real browser is the easiest way to
* validate what the current settings are.
*
* How to enable this for a site:
* - drush vset acsf_allow_override_page_cache 1
* - drush vset page_cache_maximum_age 3600
*/
if (!drupal_is_cli()) {
$result = db_query("SELECT value FROM {variable} WHERE name = 'acsf_allow_override_page_cache';")->fetchField();
if ($result) {
$acsf_allow_override_page_cache = unserialize($result);
if ($acsf_allow_override_page_cache) {
$result = db_query("SELECT value FROM {variable} WHERE name = 'page_cache_maximum_age';")->fetchField();
// An empty array indicates no value was set in the database, so we ignore
// the site.
if ($result) {
$page_cache_maximum_age = (int) unserialize($result);
if ($page_cache_maximum_age > 300) {
$conf['page_cache_maximum_age'] = $page_cache_maximum_age;
}
}
}
}
}
After creating and uploading the post-settings-php
hook, use one of
the following methods to implement the change:
- From your local command line, run the following Drush commands, substituting in your website’s details for the text in brackets:
drush @[sitename].[env] --uri=[site.host.com] vset acsf_allow_override_page_cache 1 drush @[sitename].[env] --uri=[site.host.com] vset page_cache_maximum_age 21600
where:
[sitename]
- Your website’s name. For more information about your sitename, see the Sitename definition page.[env]
- Your website’s environment (such asprod
or01live
)[site.host.com]
- Your website’s URL
- Update directly on your website’s serverFrom the command line, SSH into your environment, and then run the following Drush commands, where
[site.host.com]
is your website’s URL:drush --uri=[site.host.com] vset acsf_allow_override_page_cache 1 drush --uri=[site.host.com] vset page_cache_maximum_age 21600
Items in the page cache will now persist for 6 hours (21,600 seconds).