Enable aggressive caching with Memcached
Precautions
- These instructions are useful for high-load, high-traffic websites. There will be no visible advantage for a usual website.
- A dedicated web server with root access is required.
- These changes must be applied only by a person with Linux and PHP knowledge. It is easy to harm an aMember installation without a full understanding of its internals.
1. Install Memcached on your web server
sudo apt-get install memcached
2. Make sure the PHP memcached extension is enabled
Check the output of the aMember CP -> Version Info page. Make sure the memcached extension is listed. If it is not listed, install the PHP extension.
3. Switch PHP to use Memcached for session storage
This change provides a significant performance boost by itself, and you probably do not need to apply the following steps after it.
Go to aMember CP -> Setup/Configuration -> Advanced and switch Session Storage to Standard PHP Sessions.
Edit the appropriate php.ini file and set:
session.save_handler = memcached
session.save_path = "localhost:11211"
Restart the web server and try to log in to the aMember admin area.
4. Cache aMember configuration and other items in Memcached
Back up amember/application/configs/config.php before editing it.
Normally, this is an auto-generated file that looks like:
const AM_USE_NEW_CSS = 1;
define('AM_SESSION_NAME', @session_name() ?: 'PHPSESSID');
return [
'db' => [
'mysql' => [
'db' => 'mysite_amember',
'user' => 'mysite_user',
'pass' => 'mysite_password',
'host' => 'mysql_host',
'prefix' => 'am_',
'port' => '',
],
],
];
Add the Memcached constants and functions before the returned array, preserve your existing database settings,
and add the cache section:
define('MEMC_CONFIG_KEY', 'amc_' . sha1(__FILE__));
define('MEMC_HOST', 'localhost');
define('MEMC_PORT', 11211);
// Read configuration from Memcached, or load it from the database and cache it.
function am_read_config(callable $readFromDb, $configName)
{
$memcached = new Memcached();
$memcached->addServer(MEMC_HOST, MEMC_PORT);
$key = MEMC_CONFIG_KEY . $configName;
$config = $memcached->get($key);
if ($memcached->getResultCode() === Memcached::RES_SUCCESS) {
return $config;
}
$config = $readFromDb();
if ($config !== null) {
$memcached->set($key, $config);
}
return $config;
}
// Update the cached configuration after aMember saves it to the database.
function am_write_config($config, $configName)
{
$memcached = new Memcached();
$memcached->addServer(MEMC_HOST, MEMC_PORT);
$memcached->set(MEMC_CONFIG_KEY . $configName, $config);
}
const AM_USE_NEW_CSS = 1;
define('AM_SESSION_NAME', @session_name() ?: 'PHPSESSID');
return [
'db' => [
'mysql' => [
'db' => 'mysite_amember',
'user' => 'mysite_user',
'pass' => 'mysite_password',
'host' => 'mysql_host',
'prefix' => 'am_',
'port' => '',
],
],
'cache' => [
'type' => 'memcached',
'options' => [
'servers' => [
['host' => MEMC_HOST, 'port' => MEMC_PORT],
],
],
],
];
The am_read_config() and am_write_config() functions cache the configuration that aMember normally
reads from the database. The cache section makes aMember use its Memcached cache backend instead of
the default file-based backend.
After applying these settings, aMember will load much faster, because it will not read configuration from the database on each run and will use Memcached for other cached data.
If something goes wrong, restore config.php from backup.
5. Enable caching for large database queries
Usually, the steps above are enough to make aMember load and work faster. For a very large database,
edit config.php again and add the following line before return [:
const AM_HUGE_DB = true;
This optimizes record-count queries and caches large totals to decrease MySQL server load. Cached totals can temporarily be out of date, so enable this mode only when it is necessary for a large database.