I want to just start this little post with that fact that I don’t know all the ins and outs of using Memcache but I have enough knowledge to just about get it working. I have access to a Mediatemple Dedicated Virtual Extreme server and managed to install it on the server using SSH, with the help of this little guide from Mediatemple which runs you through the installation process step by step.
Memcache was developed by Danga Interactive to enhance the speed of LiveJournal.com, Memcache dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a Memcache miss. Currently FaceBook are the biggest users of Memcache. Infact FaceBook are using it so aggressively that they are chartering new territory, and helping to develop the open source project.

So your reading this wondering what I’m talking about. Memcache is basically is a generalpurpose distributed memory caching system, put that in English, it allows you to store any form of data in a ‘temporary cache’ so wherever you go to do a database query, instead of just connecting to the database and getting the data we want we first check the memcache to see if our data is already stored. If the memcache returns nothing, then go to the database, get what you’re looking for, then store it in the memcache for later:
There are five main functions that we use with Memcache and they are as follows:

So once you have Memcache installed on your server you can connect to it and start caching stuff. It’s worth pointing out that you shouldn’t go out and cache everything, it’s really only useful when you have large amounts of data that are going to be requested regularly.
$memcache = new Memcached();
$memcache>connect('127.0.0.1', 11211) or die ("Could not connect");
include('includes/connect.php');
//set the key then check the cache
$key = md5("SELECT * FROM memcached_test where name='ashley'");
$get_result = $memcache>get($key);
if ($get_result) {
echo $get_result['name'];
echo $get_result['username'];
echo "Data Pulled From Cache";
}
else {
// Run the query and get the data from the database then cache it
$query="SELECT * FROM memcached_test where name='ashley';";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
print_r($row);
$memcache>set($key, $row, TRUE, 20); // Store the result of the query for 20 seconds
echo "Data Pulled from the Database";
}
This is a very simple example of how to use memcache, but I hope it has been an insight into how caching works.
I think your sample has some problem… It should be $memcache = new Memcached(); instead of $memcache = new Memcache;
Thank you for sharing this. Its really helpful.
You could have also pointed out how to install and set up memcache aswell
Fantastic informative post. It’s really great….
thanks for you. i’m really deal with large amount of data.this is the best solution for my project
nice and interesting keep it up!!
Hi
This is very useful information of php developer,
I tried to install mamcache in xampp, but i got some error, please help me how to do that one.
Thanks,
Ganesamoorthi.D
thanks a lot man . its really good about you sharing it
Great! Rly nice tutorial..
Just FYI: there’re some encoded html entities on your block of code like $memcache->connect
very useful tuts. Thanks for sharing..
Very useful information i discover…
Thanks,,,
Hi,
This is very useful to me. Provide the link to know further more…..
Thank you so much….
Short and understandable … thanks for sharing
Thanks nice tuts!
hmmm thanks for sharing
Nice and informative article. Very cool to know something new. Thanks to sharing this nice post.
I have some trouble installing memcache on my server. Can you help please
Wow, Thank you for this!
I was looking for something like this.
Wow, its great to know such technology exists. Seems a little advanced for me. But now know, i’d probably come across this in the near future, with my social networking app.
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
21 discussions around Using Memcache With PHP