9lessons programming blog
Loading Search
Monday, February 13, 2012

Memcached with PHP.

Memcahced open source distributed memory object caching system it helps you to speeding up the dynamic web applications by reducing database server load. In this post I want to explain how I had implemented Memcached object caching system for demos.9lessons.info. This system is very helpful for high traffic media and blog related websites.

Memcached

Live Demo

Database
Sample database demos table contains id, title and link.
CREATE TABLE demos
(
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(300),
link VARCHAR(300),
);

First User Request
First request goes to database server at the same time data object storing in Memcached server.
Memcached

Second User Request
Second user request data comes from Memcached object.
Memcached

Memcached Installation
Lots of better resources available on web please follow the links. php_memcache.dll
INSTALLING MEMCACHED ON AMAZON LINUX AMI - QUICK AND EASY.
INSTALLING MEMCACHED ON Windows.
install Memcached on Xampp on Windows 7
Memcached for PHP 5.3 on Windows 7.

index.php
Contains PHP code.
<?php
include('db.php');
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$key = md5('List 9lessons Demos'); // Unique Words
$cache_result = array();
$cache_result = $memcache->get($key); // Memcached object 

if($cache_result)
{
// Second User Request
$demos_result=$cache_result;
}
else
{
// First User Request 
$v=mysql_query("select * from demos order by id desc");
while($row=mysql_fetch_array($v))
$demos_result[]=$row; // Results storing in array
$memcache->set($key, $demos_result, MEMCACHE_COMPRESSED, 1200);
// 1200 Seconds
}

// Result
foreach($demos_result as $row)
{
echo '<a href='.$row['link'].'>'.$row['title'].'</a>';
}

?>

db.php
You have to change hostname, username, password and database name.
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "database";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
?>

Share this post

Comments
{ 26 comments }
rusdimori said...

OK..
mantap bro....

Thái Vũ said...

like

Djamel said...

Thank You Bro;)

lazaac said...

so basically what wil be effected by implement this method..? fast render on webpage..? or what.? sorry, i'm not really understand...

John said...

I never tried memcache, now I see it's easy to use, thanks to you Sir!

Djtale said...

How much faster is it ?

Anonymous said...

How to use memcache to cache dyanamic image?

Anonymous said...

easy than i thought

Jonathan said...

Watch out if the database response is "false", the memcached will save a string containing "false", so this:
if($cache_result)

will result false, when the memcached really has an answer. Its better to validet it with:

if($cache_result === false)

Anonymous said...

Very good

bastr3 said...

its nice man :)

Anonymous said...

it's same with wp super cache ?

Sree said...

@srini kitna deti hai? LOL :)
Superb post, was working on the same concept :)

Anonymous said...

Thanks for this Srinivas :)

Vishal

Swapnil Kene said...

i have worked on memcache module, be careful while using memcache since often you dont want to memcache all your queries especially session or user data queries.
The solution is to delete respective cache whenever data is updated.
Njoy power of memcache - \m/

Mohit Bumb said...

Cool Bro.

dskanth said...

Good post...

Kerem said...

Are you sure about that your talking abnout "Memcached"? Should it be just "Memcache"?
Memcached: http://www.php.net/manual/en/book.memcached.php
Memcache: http://www.php.net/manual/en/book.memcache.php

mxtdn said...

nice

Omprakash Bagrao said...

Nice post shri

Komputer Bogor said...

Thank you for sharing, this is what i need it

Moiz said...

hello,

i am having a website which have more than 100,000 users. Will mem cache increase the performance ?
If yes the how ?

Manas.....powered by mind said...

Thanks bro..

Logo EPS said...

Thanks so much!

myinterviewworld said...

Thanks brother,nice post

Wahyu Gugus Nurcahyo said...

Good Tutorial Thanks

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine
After developing a site you’ll require a web hosting plan to host your site, a recommended host is justhost.com which operates a LAMP environment and has been established a number of years.

Like Me