How to configure memcached on managed servers

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

  • Managed VPS
  • Managed Dedicated server
  • Turbo Velocity and Turbo Nitro Web Hosting accounts

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 Velocity or Turbo Nitro 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 Dedicated servers

Before you can use memcached on a managed VPS or 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

$mc = new Memcached();
$mc->addServer('localhost', 11211) or die ("Unable to connect");

echo "Server version:<pre>";
print_r($mc->getVersion());
echo "</pre>";

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

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

$result = $mc->get('testkey');

echo "Data from the cache:<pre>";
var_dump($result);
echo "</pre>";

?>

In this example, we store an object value ($tmp) 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.memcached.php.

Turbo Boost and Turbo Max Web Hosting accounts

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

/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 section of the cPanel home screen, click Select PHP Version.
  3. In the list of PHP extensions, confirm that the memcached 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 memcached 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

$mc = new Memcached();
$mc->addServer('/opt/memcached/run/username/memcached-1.sock', 0) or die ("Unable to connect");

echo "Server version:<pre>";
print_r($mc->getVersion());
echo "</pre>";

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

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

$result = $mc->get('testkey');

echo "Data from the cache:<pre>";
var_dump($result);
echo "</pre>";

?>

In this example, we store an object value ($tmp) 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.memcached.php.

More Information

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.