This article describes how to use memcached on the following hosting packages:
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.
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.
After memcached is installed, you can connect to it using the following parameters:
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.
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.
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:
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('/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.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
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.