21 Jun 2010 @ 5:29 PM 

This is a very basic example of using memcache + PHP

Use at your own peril

$memcache = new Memcache;
$memcache->connect("127.0.0.1");
$sql = "select * from some_huge_table where tx_date <= now()-365 limit 0, 5000";
$key = md5($sql);
$query = $memcache->get($key);
//the query isnt in the cache
if ($query == null)
{
    $query = $this->db->query($sql);
    $query = $query->result();
    //put it in the cache
    $memcache->set($key,$query,0,600);//10 minute expiry
}
if (count($query) > 0)
{
//do something with $query
}

This example loads memcached, looks for a key, and if its not there, stores it. SIMPLE

****UPDATE****

Today while going through CI framework, I realized that CI can do its own query caching, albeit to disk. For more info on CI query caching, look go here

To change this to memcached caching should be a cinch, and indeed, it is. Head over here to see how.

So without having to change a single line of code in your application, you can take full advantage of memcached for caching query results

Posted By: Zayin
Last Edit: 23 Jun 2010 @ 02:07 AM

EmailPermalink
Tags
Tags: ,
Categories: php


 

Responses to this post » (4 Total)

 
  1. zayinkrige says:

    New Blog Post -> Using memcached with PHP and CodeIgniter – http://www.zayinkrige.com/using-memcache

  2. Hello Zayin,

    Can you tell me on which email address i should communicate to you as i am interested to talk to you for some business opportunities with IT projects?

    Regards,
    Gaurang Upadhyay
    Email: gupadhyay@solusofttech.com

  3. Zayin says:

    zkrige at gmail dot com

  4. ehcache.net says:

    Using memcached with PHP and CodeIgniter…

    This is a very basic example of using memcache + PHP Use at your own peril ?[Copy to clipboard]View Code PHP$memcache = new Memcache; $memcache->connect(“127.0.0.1″); $sql = “select * from some_huge_table…

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 40
  • Comments » 64
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Portfolio



    No Child Pages.