Knowledge Base

How to use memcached on managed servers

This article describes how to use memcached on the following hosting packages:

  • Managed VPS
  • Managed Flex Dedicated server
  • Turbo Web Hosting account
  • Turbo Reseller Hosting account

Memcached is an open-source memory object caching system that web sites can use to help accelerate page load times. Memcached caches in RAM frequently accessed data, such as the results of API calls, database calls, and more.

Memcached can significantly help improve site performance.

If you have a Turbo Web Hosting account, you should use the A2 Optimized plugin to configure and manage memcached for many popular web applications (such as WordPress, Drupal, Joomla, and several others). The following information only applies if you want to use memcached in an application you have developed yourself, or if you are using an application that the A2 Optimized plugin does not support.

Managed VPS and Flex Dedicated servers

Before you can use memcached on a managed VPS or Flex Dedicated server, we must install it for you. To do this, please open a support ticket on the Customer Portal at https://my.a2hosting.com and request memcached for your system.

Connection parameters

After memcached is installed, you can connect to it using the following parameters:

  • Hostname: localhost
    Alternatively, you can use the IP address 127.0.0.1.
  • Port: 11211
Diagnostics

To verify memcached is running, you can use the memcached-tool program or use telnet to connect to memcached directly.

For example, the following commands demonstrate how to use memcached-tool to display general statistics for memcached, and then dump all of its stored key-value pairs:

memcached-tool localhost:11211 stats
memcached-tool localhost:11211 dump

Alternatively, you can use telnet to connect directly to the running memcached instance. To do this, type the following command:

telnet localhost 11211

To display general statistics for memcached, type stats. To exit, type quit.

Code sample

The following PHP code demonstrates how to connect to memcached and store a key-value pair in the cache:

<?php

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Unable to connect");

$version = $memcache->getVersion();
echo "Server version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = rand(0,1000);

$memcache->set('testkey', $tmp_object, false, 10) or die ("Unable to save data in the cache");

$get_result = $memcache->get('testkey');

echo "Data from the cache:<br/>\n";
var_dump($get_result);

?>

In this example, we store an object value ($tmp_object) that contains a string value and a random number between 0 and 1000 in the testkey key. The key-value pair expires from the cache after 10 seconds.

For more information about how to use PHP with memcached, please visit https://secure.php.net/manual/en/book.memcache.php.

Turbo Web Hosting and Turbo Reseller Hosting accounts

To connect to memcached on a Turbo Web Hosting or Turbo Reseller Hosting account, use the following Unix socket path. Replace username with your own account username:

unix:///opt/memcached/run/username/memcached-1.sock

To verify that the memcached socket is active for your account, type the following command. Replace username with your own account username:

ls -l /opt/memcached/run/username/memcached-1.sock

If you receive a No such file or directory error message, then the socket has not been activated for your account yet. To do this, type the following command:

touch ~/.memcached.on

The server checks for this file every five minutes, and starts the memcached process for the account if it does not already exist. After five minutes, run the ls command above again, and you should see the memcached-1.sock file in the directory listing.

Before you try to use memcached with PHP on your account, you should also make sure the correct PHP extension is enabled. To do this, follow these steps:

  1. Log in to cPanel.
  2. In the Software/Services section of the cPanel home screen, click Select PHP Version.
  3. In the list of PHP extensions, confirm that the memcache check box is selected. (If you do not see a list of PHP extensions, click Switch To PHP Extensions.) Note that there is a memcache extension and a memcached extension—you want to use the memcache extension.
Code sample

The following PHP code demonstrates how to connect to memcached and store a key-value pair in the cache.

If you run this code, remember to replace username with your own account username.
<?php

$memcache = new Memcache;
$memcache->connect('unix:///opt/memcached/run/username/memcached-1.sock', 0) or die ("Unable to connect");

$version = $memcache->getVersion();
echo "Server version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = rand(0,1000);

$memcache->set('testkey', $tmp_object, false, 10) or die ("Unable to save data in the cache");

$get_result = $memcache->get('testkey');

echo "Data from the cache:<br/>\n";
var_dump($get_result);

?>

In this example, we store an object value ($tmp_object) that contains a string value and a random number between 0 and 1000 in the testkey key. The key-value pair expires from the cache after 10 seconds.

For more information about how to use PHP with memcached, please visit https://secure.php.net/manual/en/book.memcache.php.

More Information