Optimizing Dynamic Content With Memcached

I have posted before on how to use Memcached to reduce database load, but I failed to give a sound example. Hopefully this will rectify that. Please read this post to catch up if you haven’t already. otherwise I will assume you have experience with Memcached.

In this scenario, you have a custom built blog application that has a section with the latest posts. Instead of constantly pulling this text from the database or saving the output to text files, which can be slow, saving the test into memory may be the best solution.

Take the following code:

$sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query)
{
     while($row=mysql_fetch_array($query))
     {
          echo "<h2>".$row['title']."</h2>";
     }
}

Lets make that faster.

$memcache = new Memcache;
$memcache->connect('memcache_host', 11211);
if(!$memcache->get('recent_posts'))
{
     $sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
     $query = mysql_query($sql);
     $post_array = array();
     if(mysql_num_rows($query)
     {
          while($row=mysql_fetch_array($query))
          {
               $post_array[] = $row;
          }
     }
    //recent expires in one hour
    $memcache->set('recent_posts',$post_Array, 0, 3600);
}

$recent_posts = $memcache->get('recent_posts');
for($i=0;$i<count($recent_posts);$i++)
{
     echo "<h2>".$recent_posts[$i]['title']."</h2>";
}

You won’t notice a large increase if your site is small, but if you have a busy site and you have a bunch of spare ram, you might as well put it to use. In the future I will show you some more examples of real-world usage of Memcached.

2 Responses to “Optimizing Dynamic Content With Memcached”

  1. OXODesign TEAM Says:

    Some more optimize:

    function mcQuery($sql_query, $key){
    global $mc;
    $result = $mc->get($key);
    if(!$result){
    $rs = mysql_query($sql_query);
    if(mysql_num_rows($row)){
    while($row = mysql_fetch_array($rs))
    $result[] = $row;

    $memcache->set($key,$result, 0, 3600);
    }
    }

    return $result;
    }

    $mc = new Memcache;
    $mc->connect(’memcache_host’, 11211);

    $rs = mcQuery(’SELECT * FROM posts ORDER BY date LIMIT 5′, ‘recent_posts’);
    for($i=0;$i<count($rs);$i++){
    echo “”.$rs[$i]['title'].”";
    }

  2. Cameron Eure Says:

    You could simplify that a bit, by doing:

    $data = $memcache->get();
    if (!data) {
    // Build $data
    }

    // Code Here

    That way you don’t have to fetch twice.

    Also, in cases when your site doesn’t get a lot of writes, you can cache the whole page, and set hooks in the administration panel to clear the cache on UPDATES/DELETES:

    $data = $memcache->get($_SERVER['REQUEST_URI']);
    if ($data) {
    // Cache Hit
    echo $data;
    die();
    }

    // Cache Miss
    ob_start();
    ob_implicit_flush(false);

    // Your code goes here

    $data = ob_get_contents();
    $memcache->set($_SERVER['REQUEST_URI'], $data);
    ob_end_flush();

Leave a Reply






Copyright 2008 Troy Whiteley & OjaiSoft

CSS | PHP | XHTML/HTML | JavaScript | Featured Sites

Home | Labs | License | RSS | Log in