Thursday, September 06, 2007

installing memcached on Ubuntu

Dependency:
1. get libevent src package libevent-1.3d.tar.gz from http://www.monkey.org/~provos/libevent
2. untar it - tar xvzf libevent-1.3d.tar.gz
3. build it by running these 2 commands:
./configure
make
4. install it by becoming root and running this command: (be sure to be in the same directory where the package was un-tarred)
make install

Memcached (the daemon):
1. get memcached from http://www.danga.com/memcached/
2. untar it - tar xvzf memcached-1.2.2.tar.gz
3. build it by running these 2 commands:
./configure
make
4. install it by becoming root and running this command: (be sure to be in the same directory where the package was un-tarred)
make install

Starting the daemon:
1. export LD_LIBRARY_PATH=/usr/local/lib
2. memcached -d -u root

Memcache (the client for php):
1. install pear package manager:
become root and run: apt-get install php-pear
2. as root, make a symlink as follows: (replace php5 with php4 if you're using php4)
ln -s /usr/include/php5 /usr/include/php
3. as root run this comand to install the client:
pecl install Memcache
4. edit your php.ini and add extension=memcache.so under extensions
5. restart apache

Use the cache in your php code:
$memcache = memcache_connect('localhost', 11211);
if ($memcache) {
$memcache->set("str_key", "String to store in memcached");
$memcache->set("num_key", 123);

$object = new StdClass;
$object->attribute = 'test';
$memcache->set("obj_key", $object);

$array = Array('assoc'=>123, 345, 567);
$memcache->set("arr_key", $array);

pr($memcache->get('str_key'));
var_dump($memcache->get('num_key'));
var_dump($memcache->get('obj_key'));
}
else {
echo "Connection to memcached failed";
}

No comments: