How to Install memcached And libmemcached

< Back to the Title Topic

How to Install memcached And libmemcached

Memcached is an open source, high-performance, distributed memory caching system intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc., stored in the memory, resulting from database calls, API calls, or page rendering.

The system caches data and objects in memory to minimize the frequency with which an external database or API must be accessed.

Installing memcached and libmemcached

1. Download and install memcached by running the following commands:

*The latest version of memcached can be found at: http://memcached.org/

**Ensure that libevent & libevent-devel packages have been installed prior.

cd /usr/src

wget http://memcached.googlecode.com/files/memcached-1.5.18.tar.gz

tar xvf memcached-1.5.18.tar.gz

cd memcached-1.5.18

./configure -–with-libevent

make && make install

2. Download and install libmemcached by running the following commands:

*The latest version of memcached can be found at: http://libmemcached.org/libMemcached.html

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

tar xvf libmemcached-1.0.18.tar.gz

cd libmemcached-1.0.18

./configure

make && make install

3. Start the memcached as a daemon process and ensure memcached is automatically started after each server reboot.

memcached -d -u nobody -m 1048 -p 11211
127.0.0.1

vim /etc/rc.d/rc.local

# add the line below the rc.local file

/usr/local/bin/memcached -d -u nobody -m 1048 -p 11211 127.0.0.1

4. Install the Memcache PECL Extension for PHP.

pecl install memcache

5. Add the following codes into php.ini

vim /usr/local/lib/php.ini

# Add the line below in the php.ini file

extension=memcache.so

6. Restart the httpd service.

/etc/init.d/httpd restart

7. Verify the memcached using php and libmemcached. Create a new .php file and add the following codes:

connect(“localhost”,11211); # You might need to set “localhost” to “127.0.0.1”

echo “Server’s version: ” . $memcache->getVersion() . ”
\n”;

$tmp_object = new stdClass;
$tmp_object->str_attr = “test”;
$tmp_object->int_attr = 123;

$memcache->set(“key”,$tmp_object,false,10);
echo “Store data in the cache (data will expire in 10 seconds)
\n”;

echo “Data from the cache:
\n”;
var_dump($memcache->get(“key”));
?>

or else, run

# strace -p `pidof memcached`

Previous How to Set HAProxy Load Balancing Using Session Cookie
Next How To Manage VMware Client Servers