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.
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),
);
(
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.
Second User Request
Second user request data comes from Memcached object.
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>';
}
?>
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");
?>
$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");
?>
OK..
ReplyDeletemantap bro....
like
ReplyDeleteThank You Bro;)
ReplyDeleteso basically what wil be effected by implement this method..? fast render on webpage..? or what.? sorry, i'm not really understand...
ReplyDeleteI never tried memcache, now I see it's easy to use, thanks to you Sir!
ReplyDeleteHow much faster is it ?
ReplyDeleteHow to use memcache to cache dyanamic image?
ReplyDeleteeasy than i thought
ReplyDeleteWatch out if the database response is "false", the memcached will save a string containing "false", so this:
ReplyDeleteif($cache_result)
will result false, when the memcached really has an answer. Its better to validet it with:
if($cache_result === false)
its nice man :)
ReplyDeleteit's same with wp super cache ?
ReplyDelete@srini kitna deti hai? LOL :)
ReplyDeleteSuperb post, was working on the same concept :)
Thanks for this Srinivas :)
ReplyDeleteVishal
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.
ReplyDeleteThe solution is to delete respective cache whenever data is updated.
Njoy power of memcache - \m/
Cool Bro.
ReplyDeleteGood post...
ReplyDeletenice
ReplyDeleteNice post shri
ReplyDeleteThank you for sharing, this is what i need it
ReplyDeletehello,
ReplyDeletei am having a website which have more than 100,000 users. Will mem cache increase the performance ?
If yes the how ?
Thanks bro..
ReplyDeleteThanks so much!
ReplyDeleteThanks brother,nice post
ReplyDeleteGood Tutorial Thanks
ReplyDeleteAre you sure about that your talking abnout "Memcached"? Should it be just "Memcache"?
ReplyDeleteMemcached: http://www.php.net/manual/en/book.memcached.php
Memcache: http://www.php.net/manual/en/book.memcache.php
your demo link doesnt work
ReplyDeleteThis is really nice brother....
ReplyDeleteWhat scheme Editor Did You Use For Illustration In this Article?
ReplyDeleteSo nice
ReplyDeletehow to install memcache in ububtu 12.04?
ReplyDeleteI like your tutorial, but how can I implement on live web server or hosting provider?
ReplyDeleteI like your tutorial but how can I implement this on live web server or any hosting provider?
ReplyDeletenice .... you got very useful things on your blog keep it up thnx
ReplyDeleteIts nice article on memcache. Thank you for sharing it.
ReplyDeleteThanks
ReplyDeleteJust to notice you that you tell you learn how to use memcachED with PHP but in the code you use memcachE
ReplyDeleteI wanted to implement memcached on windows but looks like it is not available for windows. Memcached is available only on Linux.
ReplyDeletegood
ReplyDeleteWarning: Memcache::connect(): Can't connect to localhost:11211, Connection refused (111) in /home/ajaybhulawat/sandbox/blr/memcache.php on line 13 Could not connect
ReplyDeletehow to solve this?
how does it's works when an updated data on database?
ReplyDeleteEXCELLENT post.
ReplyDeleteCan you tell me which network topology icons are those?
Thanks in advance.
i think you are using Memcache while your post title related to Memcached
ReplyDeleteOne question lets say we have page having new data after every 5 minutes user refresh page 2nd time system will fetch data from cache what about new data?
ReplyDeletewhat is the difference between memcached & mamcache ?? i am confused.. !!!
ReplyDeletereally superb tutorial... I got scared to use memcache when I see a few other tutorials. But this is awesome. Thanks.
ReplyDeleteAm getting this error. pls help
ReplyDeleteFatal error: Class 'Memcache' not found in C:\wamp
Make sure you need to enable php_memcache.dll in php.ini file and need to place that dll in ext directory , restart the apache server and download memcahce.exe file and run that one. Then you can get the correct out put.
Deletecurrently i developed a web portal for multiple user.data arrived from data base it depends on user id so how can i implement on this scenario ?
ReplyDeletethe code is for memcache, NOT memcached
ReplyDelete