Configuring memcached on managed servers

Boost performance of your site with memcached. Follow our easy step-by-step instructions to learn how to setup 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.

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 at https://my.hosting.com and request memcached for your system.

Connection parameters

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

  • Hostname: localhost

    👍

    Tip

    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 usetelnet to connect to memcached directly.

For example, the following commands demonstrate how to usememcached-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 usetelnet 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.

🚧

Important

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.

🚧

Important

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