Thursday, June 3, 2010

MemCache Installation

Today i had worked on installing Memcache

The Procedure to install Memcache is (LAMP)
1) check your system will support pecl installations are not
2) fire this command in CMD "pecl install memcache" (another way is "apt-get install php5-memcache", But it is not working for all the systems.)
3) Fire this command in CMD
    echo "extension memcache.so" >> /etc/php5/apache2/php.ini  (This is the path for your php.ini file)
4) now you have to install the memcached server
   run this command "apt-get install memcached"
5) Restart the apache server "/etc/init.d/apache2 start"
6) Start memcached server "/etc/init.d/memcached start"




Try yhis example


$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."
\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)
\n";

$get_result = $memcache->get('key');
echo "Data from the cache:
\n";

var_dump($get_result);
$get_result = $memcache->get('key');
echo "Data from the cache after 10 sec:
\n";
var_dump($get_result);

Some time you will get this error
Warning: Memcache::connect() [memcache.connect]: Can't connect to localhost:11211, Connection refused

The cause for this error is memcached server is not started. So start the memcached server.

some useful links are
http://www.howtoforge.com/installing-memcached-and-the-php5-memcache-module-on-debian-etch-apache2

http://trentrichardson.com/2010/02/08/installing-memcached-on-ubuntu-for-php/

http://www.lullabot.com/articles/how-install-memcache-debian-etch

Note: the possible ways to install packages in linux are
i) apt-get ii) yum iii) rpm

No comments: