Home » Php » php – How to use memcached with Joomla

php – How to use memcached with Joomla

Posted by: admin July 12, 2020 Leave a comment

Questions:

How can I use memcache in Joomla? I’m a newbie in this field so please be descriptive with your answer.

How to&Answers:

You will need to install memcached on your server and will probably need root access to do so. You can get memcached from http://www.danga.com/memcached/. It requires libevent, which can be downloaded here: http://www.monkey.org/~provos/libevent/

Finally, you’ll need to get the PHP PECL extension for memcache. To install this, you need to go to the server where PHP is installed and run this command:

pecl install memcache

Again, you will most likely need root access to your server to do this. After you have libevent, memcached, and the PECL extensions installed, go to the Global Configuration in Joomla and choose Memory Cache as the Cache Handler under Cache Settings. After you save the Global Configuration, open it again and more inputs should appear underneath the Cache Handler input. Set Memory Chache Server to localhost and the port to 11211. This should match the parameters you use to run memcached from the command line.

EDIT: You can also use XCache not only to store data in a way similar to Memcache, but it will also cache the opcode generated by PHP. This way, instead of reading the PHP code from disk and parsing it each time, it will hold the code in memory for the next request.

Be sure to select XCache as the Cache Handler in Global Configuration. Read this for information on installing XCache: http://xcache.lighttpd.net/wiki/InstallFromSource

Answer:

In order to make Joomla to use memcache for session caching you need to manually edit the configuration.php and change this line:

public $session_handler = 'database';

to this one:

public $session_handler = 'memcache';

And this is what is missing everywhere, you need to add a new option memcache_settings:

  public $memcache_settings = 'a:3:{s:10:"persistent";s:1:"0";s:11:"compression";s:1:"0";s:7:"servers";a:1:{i:0;a:2:{s:4:"host";s:9:"127.0.0.1";s:4:"port";s:5:"11211";}}}';

This is a serialized multy-dimentianal array. I use this code to generate the above string:

 $a = array(
   "persistent" => "0", 
   "compression" => "0", 
   "servers" => array(
     "0" => array(
       "host" => "127.0.0.1", "port" => "11211")
     )
   );

echo(serialize($a));

If you don’t add the memcache_settings option your sessions will never work with memcache.

Answer:

These settings in configuration.php work for memcached in Joomla 3.3+

public $cache_handler = 'memcached';
public $memcached_server_host = '127.0.0.1';
public $memcached_server_port = '11211';
public $memcached_persist = '1';
public $memcached_compress = '1';
public $cachetime = '15';
public $session_handler = 'memcached';
public $session_memcached_server_host = '127.0.0.1';
public $session_memcached_server_port = '11211';

To install memcached on a Debian system:

apt-get install memcached php5-memcached

(you can also use memcache in place of each occurence of memcached in the above public variables & install the older php5-memcache extension)

Upgrading to php5.5 will give you a builtin Zend Opcode Cache – this could be used with APCu (APCu is the Alternative PHP Cache with the Opcode Cache removed) – to cache userland locally if you do not need a distributed memory cache (memcached)

For a single VPS APCu has a lower memory footprint & is a more suitable cache (especially with php5.5) & can be set in configuration.php with:

public $cache_handler = 'apc';
public $session_handler = 'apc';

Answer:

This how to might also offer some help for Joomla 2.5 as it points to the Joomla Admin Screen to use memcache http://www.siteground.com/tutorials/supercacher/joomla_memcached.htm

Answer:

It’s better to not use APC for backend cache. This can cause a lot of fragmentation. I have only seen it degrade performance of Joomla.