Enable agressive caching with MemCached
PRECAUTIONS
- These instructions are useful for high-load, high-traffic websites. There will be no visible advantage for a usual website
- Dedicated webserver with root access is required
- These changes must be applied only by a person with Linux and PHP knowledge. It is easy to harm aMember installation without a full understanding of internals.
0 - Install MemCached server to your webserver if that is not installed
sudo apt-get install memcached
1 - Make sure you have memcached extension enabled in your PHP
Check the output of aMember CP -> Version Info page. Make sure memcached extension is listed. If that is not listed, install the PHP extension.
2 - Switch PHP to use MemCached for sessions storage
This change makes a huge 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 appropriate php.ini
file and set
session.save_handler=memcached
session.save_path="localhost:12111"
Restart the webserver and try to login into aMember admin area.
3 - Set aMember to cache config and other items in memcache
Backup file amember/application/configs/config.php !!
Edit file amember/application/configs/config.php Normally that is an auto-generated file and it looks like
define('AM_USE_NEW_CSS', 1);
return [
'db' => [
'mysql' => [
'db' => 'mysite_amember',
'user' => 'mysite_user',
'pass' => 'mysite_password',
'host' => 'mysql_host',
'prefix' => 'am_',
'port' => '',
],
],
];
Convert it to a file like the one shown below:
define('MEMC_CONFIG_KEY', 'amc_' . sha1(__FILE__));
define('MEMC_HOST', 'localhost'); // <-- modify if necessary
define('MEMC_PORT', 11211); // <-- modify if necessary
// read config from memcached
function am_read_config(callable $cb, $name)
{
$mc = new Memcached;
$mc->addServer(MEMC_HOST, MEMC_PORT);
return $mc->get(MEMC_CONFIG_KEY . $name, function($memc, $key, &$value) use ($cb) {
$value = call_user_func($cb);
return true;
});
}
// reset config in memcached on write
function am_write_config($cfg, $name)
{
$mc = new Memcached;
$mc->addServer(MEMC_HOST, MEMC_PORT);
$mc->delete(MEMC_CONFIG_KEY . $name);
}
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, ]]
]
]
];
We added 2 functions named am_read_config and am_write_config , we keep database settings untouched, and we added cache section to returned config.
After applying these settings, aMember will load much faster, because it will not read config from the database on each run, and additionally it will utilize fast memcache caching engine instead of file-based default cache.
If something goes wrong, restore config.php from backup.
4 - Enable agressive DB queries caching
Usually, it is quite enough to apply steps above to get your aMember loading and working much faster. However, if you need more speed, edit file config.php again and add the following line before return [...
define('AM_HUGE_DB', true);
This will enable caching of query results and it will decrease MySQL server load.