Playing Memcached with simple CRUD php

Another stuff that i just play with Memcached, to cure my curiosity =D.
Memcached Is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached runs on Unix, Linux, Windows and Mac OS X and is distributed under a permissive free software license.(wikip)

Installing the packages:
Installing is pretty easy, its already on CentOS repos:




[root@cluster01 ~]# yum install memcached php-pecl-memcache

==========================================================
 Package                                     Arch                             Version
==========================================================
Installing:
 memcached                                   x86_64                           1.4.4-3.el6
 php-pecl-memcache                           x86_64                           3.0.5-4.el6
Installing for dependencies:
 libevent                                    x86_64                           1.4.13-4.el6
 php-pear                                    noarch                           1:1.9.4-4.el6

Configure the listening address:


[root@cluster01 ~]# sed -i 's/=""/="-l 127.0.0.1"/g' /etc/sysconfig/memcached 
[root@cluster01 ~]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"
Because the place that i configure memcached is same place with web server.

Configure the php scripts:
I get a crud from my friend(github.com/dodotdot), then i configure the script for working with my memcached server.

index.php
[root@cluster01 ~]# cat /var/www/html/index.php
<?php
include('config.php');
?>

<a href=new.php>Buat Berita Baru</a>

<table border="1" >
<thead>
<tr>
<th><b>Id</b></th>
<th><b>Judul</b></th>
<th><b>Action</b></th>
</tr>
</thead>

<?php
include('new.php');

define('MEMCACHED_HOST', '127.0.0.1');
define('MEMCACHED_PORT', '11211');
$memcache = new Memcache;
$cacheAvailable = $memcache->connect(MEMCACHED_HOST, MEMCACHED_PORT);

$berita = null;
if ($cacheAvailable == true)
{
 $key = 'berita_' . $id;
 $berita = $memcache->get($key);
}

if (!$berita)
 {
   $result = mysqli_query($con, "SELECT * FROM `berita` WHERE ID = " . $id);
   while($row = mysqli_fetch_array($result))
   {
    echo "<tbody><tr>"; 
    echo "<td valign='top'>" . $row['id']. "</td>"; 
    echo "<td valign='top'>" . $row['title']. "</td>";
    echo "<td valign='top'><a href=edit.php?id={$row['id']}>Edit</a> | <a href=read.php?id={$row['id']}>Read</a> |  <a href=delete.php?id={$row['id']}>Delete</a></td> ";
    echo "</tr></tbody>";
   }
 }
?>

</table>
new.php
[root@cluster01 ~]# cat /var/www/html/new.php 
<?php
include('config.php');

define('MEMCACHED_HOST', '127.0.0.1');
define('MEMCACHED_PORT', '11211');
$memcache = new Memcache;
$cacheAvailable = $memcache->connect(MEMCACHED_HOST, MEMCACHED_PORT);

if (isset($_POST['submitted'])) {

        $sql = sprintf("INSERT INTO `berita` ( `title`,`content`) VALUES ('%s','%s') ",$_POST['title'],$_POST['content']);
        $exec = mysqli_query($con,$sql) or die(mysqli_error());

        if($exec){
                        $key = 'berita_' . $id;
                                $berita = array('id' => $id, 'title' => $title, 'content' => $content);
                                $memcache->set($key, $berita);
                echo "Added row.<br />";
        }
}
?>

<form method='POST'>
<p><b>Judul:</b><br /><input type='text' name='title'/></p>
<p><b>Content:</b><br /><textarea name='content'></textarea></p>
<p><input type='submit' value='Add record' /><input type='hidden' value='1' name='submitted' /> </p>
</form>

<p><a href='index.php'>Back To Listing</a></p>

Results
The cool thing..!!!
[root@cluster01 ~]# memcached-tool 127.0.0.1:11211 display
  #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
  2     120B      8953s       1       1      no        0        0    0
[root@cluster01 ~]# memcached-tool 127.0.0.1:11211 dump
Dumping memcache contents
  Number of buckets: 1
  Number of items  : 1
Dumping bucket 2 - 1 total items
add berita_ 1 1383730644 47
a:3:{s:2:"id";N;s:5:"title";N;s:7:"content";N;}
[root@cluster01 ~]# memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211   Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes         120
              bytes_read       21159
           bytes_written      191657
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get        1473
                 cmd_set          12
             conn_yields           0
   connection_structures           7
        curr_connections           5
              curr_items           1
               decr_hits           0
             decr_misses           0
             delete_hits           0
           delete_misses           0
               evictions           0
                get_hits        1227
              get_misses         246
               incr_hits           0
             incr_misses           0
          limit_maxbytes    67108864
     listen_disabled_num           0
                     pid       15865
            pointer_size          64
           rusage_system    0.917860
             rusage_user    0.229965
                 threads           4
                    time  1383739626
       total_connections        2480
             total_items          12
                  uptime        8982
                 version       1.4.4