Papermashup

Subscribe


Tweets


"Excited to be the 962nd pirate on @BackerKit for Piff The Magic Dragon Playing... | Thx @thepiff! https://t.co/jvq8FFw3e6"

@ashleyford 3 hours ago

"@KBedders @elerichardson I'm Free!!!!!! Call / Whatsapp me!!!"

@ashleyford 2 weeks ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Using Memcache With PHP

AshleyAshley

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.

Who uses Memcache?

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.

fb

What’s Memcached?

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:

mem1

The Code

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.

  1. So the first two lines setup the connection to Memcache.
  2. The include is just my database connection script.
  3. Next we set the key. When we store data using Memcache there are 3 parts to storing the data, those three parts are the key, the value, and the expiry time of the cached item. The key is used to reference the stored data. in this example you can see that the key is an MD5 hash of the database query.
  4. we then check if the key exists in the cache. this will return true or false. if the data is found in the cache then we can access it.
  5. If the data isn’t found in the cache we connect to the database to get it. we store the data in the cache by using the follow line: $memcache->set($key, $row, TRUE, 20); $row refers to the array that we get from the database, notice the 20 at the end. This refers to the how long this item will expire in the cache. It is measured in seconds.


$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.

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 31
  • Brian Dean
    Posted on

    Brian Dean Brian Dean

    Reply Author

    Interesting topic
    what you have shared with us. Your writing skill is really very appreciative.
    I love when you share your views through the best articles.Keep sharing and
    posting articles like these.This article has helped me a lot.Keep posting
    this stuff.


  • jiastate.com
    Posted on

    jiastate.com jiastate.com

    Reply Author

    Yes! Finally something about site link.


  • prabha
    Posted on

    prabha prabha

    Reply Author

    Always data retrieve from mysql da… how to retrieve data from memcache and how to know… pls any help me


  • Jam
    Posted on

    Jam Jam

    Reply Author

    Hello Ashley,

    Is the script still valid with jQuery 1.8 ?

    Thx

    Jam


    • Bob
      Posted on

      Bob Bob

      Reply Author

      I cannot believe you just asked that. This is PHP, it has nothing to do with jQuery…


  • StudyHills
    Posted on

    StudyHills StudyHills

    Reply Author

    Short and crispy, good work man.


  • Umar
    Posted on

    Umar Umar

    Reply Author

    Really a great post about the memcache concept ever.


  • Umar
    Posted on

    Umar Umar

    Reply Author

    Really a Great post for the memcache concept ever.


  • Clipping path BD
    Posted on

    Clipping path BD Clipping path BD

    Reply Author

    Great post. I have learned many thing from here. thanks for your kind sharing.


  • Vishal
    Posted on

    Vishal Vishal

    Reply Author

    Nice Tutorial…..

    But can u plz tell me how can install memcache into my server…

    Thanx.
    :)


  • Techie Talks
    Posted on

    Techie Talks Techie Talks

    Reply Author

    I think your sample has some problem… It should be $memcache = new Memcached(); instead of $memcache = new Memcache;


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      Thanks for noticing all updated now


  • Murugan
    Posted on

    Murugan Murugan

    Reply Author

    Thank you for sharing this. Its really helpful.


  • phpcoder
    Posted on

    phpcoder phpcoder

    Reply Author

    You could have also pointed out how to install and set up memcache aswell


  • Image Clipping Service
    Posted on

    Image Clipping Service Image Clipping Service

    Reply Author

    Fantastic informative post. It’s really great….


  • mike
    Posted on

    mike mike

    Reply Author

    thanks for you. i’m really deal with large amount of data.this is the best solution for my project


  • cutecankil
    Posted on

    cutecankil cutecankil

    Reply Author

    nice and interesting keep it up!!


  • ganesamoorthi
    Posted on

    ganesamoorthi ganesamoorthi

    Reply Author

    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


  • raj
    Posted on

    raj raj

    Reply Author

    thanks a lot man . its really good about you sharing it


  • Thiago Belem
    Posted on

    Thiago Belem Thiago Belem

    Reply Author

    Great! Rly nice tutorial.. :)

    Just FYI: there’re some encoded html entities on your block of code like $memcache->connect


  • MukulBD
    Posted on

    MukulBD MukulBD

    Reply Author

    very useful tuts. Thanks for sharing..


  • Color Experts
    Posted on

    Color Experts Color Experts

    Reply Author

    Very useful information i discover…
    Thanks,,,


  • Raja
    Posted on

    Raja Raja

    Reply Author

    Hi,

    This is very useful to me. Provide the link to know further more…..

    Thank you so much….


  • styx
    Posted on

    styx styx

    Reply Author

    Short and understandable … thanks for sharing


  • Jehnee
    Posted on

    Jehnee Jehnee

    Reply Author

    Thanks nice tuts!


  • HeartDisk
    Posted on

    HeartDisk HeartDisk

    Reply Author

    hmmm thanks for sharing


  • clippingimages
    Posted on

    clippingimages clippingimages

    Reply Author

    Nice and informative article. Very cool to know something new. Thanks to sharing this nice post. :)


  • Paul du Long
    Posted on

    Paul du Long Paul du Long

    Reply Author

    I have some trouble installing memcache on my server. Can you help please :)


  • Davinder
    Posted on

    Davinder Davinder

    Reply Author

    Wow, Thank you for this!
    I was looking for something like this.


  • Enatom
    Posted on

    Enatom Enatom

    Reply Author

    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.