Simple install of memcached on Ubuntu

Memcached is a free, high performance distributed memory object caching system. In English, it can make your single server PHP applications faster or for a little more technical users you can write cache and share cache objects over a web server farm or cluster. Great, eh.

No seriously, it can really make your PHP faster.Here is how to install it really simply on Ubuntu for use with PHP.

[codesyntax lang=”bash”]
apt-get install memcached php5-memcache

Simple as that!

Lets check it’s running

[codesyntax lang=”bash”]
itsupport@intranet4:~$ ps uxa | grep memcache

[codesyntax lang=”bash”]
nobody 8108 0.0 0.8 19104 17664 ? S Jan20 0:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l

By default its running on localhost only and on the default port of 11211. You can change these if you need network access or whatever in /etc/memcached.conf. But we’ll leave it as default for now.

Now, how hard was that?

PHP lists a simple example file to test PHP and memcached interoperatability.

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *