Quantcast

10 baby steps to install Memcached Server and access it with PHP

Thinking of implementing caching for your php application , you are at a right place. Just in 10 simple (copy and paste) steps you can install and access Memcached Server.

Step1: Install libevent ,libmemcached and libmemcached devel (dependency)

1
2
yum install libevent
yum install libmemcached libmemcached-devel
.

Step 2: Install Memcached Server

1
yum install memcached
.

Step 3: Start Memcached server

1
memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody

(d = daemon, m = memory, u = user, l = IP to listen to, p = port)

.

Step 4: Check your memcached server is running successfully

1
ps -eaf | grep memcached
.

Step 5: Connect Memcached server via telnet

1
telnet 127.0.0.1 11211
.

Step 6: Check current status of Memcached Server on telnet prompt

1
stats
.

Step 7: Exit telnet

1
quit
.

Step 8: Install PHP client to access Memcached Server

1
pecl install memcache

It will make “memcache.so”, you have to just put it on your /etc/php.ini file.

.

Step 9: Restart your apache server

1
service httpd restart
.

Step 10: Open your favorite editor to type below code and execute it, it will cache your data into Memcached server and access it back for you

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); //connect to memcached server
 
$mydata = "i want to cache this line"; //your cacheble data
 
$memcache->set('key', $mydata, false, 100); //add it to memcached server
 
$get_result = $memcache->get('key'); //retrieve your data
 
var_dump($get_result); //show it
?>
.

Hurray!! All done!
Enjoy Caching with Memcached.
Cheers!

Delicious Bookmark

Comments

01.25.10

For example, CentOS does not have Memcached in its repository, so you should use RPMForge (http://wiki.centos.org/AdditionalResources/Repositories/RPMForge) or other repo.

BTW, when you use only one server (localhost), it’s better to use APC or XCache because they don’t open TCP connection.

01.25.10

This post is over-explained,rightly said Baby steps. in reality however you need 4 steps Max.
Cheers !!

01.25.10

pecl memcached 1.0.0 is stable and more near to libmemcached library, but it still not running fine, so avoid pecl memcached before arrival of some more stable version.

01.25.10

really useful! thanks!

01.25.10

install too long

01.25.10

I am to a great extent impressed with the article I have just read. I wish the author of webdeveloperjuice can continue to provide so much useful information and unforgettable experience to its readers. There is not much to state except the following universal truth: No matter how much you dumb down something, someone out there is dumber. I will be back.

01.25.10

me to go somewhere dutch, I’ll brain you.”

01.25.10

“Please keep it. I have several like it,” said Petya, blushing.

01.25.10

and forth-their own CB now in easy reaching distance-there were over three

01.25.10

“That it would be hard for them to make a mistake,” Stu finished for him.

01.25.10

xjRrB3 Excellent article, I will take note. Many thanks for the story!

01.25.10

Nice article.

I would recommend using the newer Memcached PHP library instead of the older ‘Memcache-without-the-d’lib. The newer one is a lot faster and manages connections much faster.

The older library still shows up first in Google results, so here’s the link:
http://php.net/manual/en/book.memcached.php

-mike

01.25.10

“That it would be hard for them to make a mistake,” Stu finished for him.

Leave Your Response

* Name, Email, Comment are Required