Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Memcached with PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 101
    Comment on it

    Hello Readers,

    In this post I want to explain the Memcached with PHP.

    Memcahced is open source distributed memory object caching system it helps you to speeding up the dynamic web applications by reducing database server load.

    The Memcached is very helpul to manage blog related and the sites who acquires high traffics.

    Memcached steps which are use in PHP code:

    1 Start the memcached program so there is a server that actually stores the key-value pairs.
    2 Make a memcache object.
    3 Store a value for a key.
    4 Retrieve your key.


    $memcache = new Memcache; //point2
    $memcache->set($yourUniqueKey, $yourValue, false, 3600); //point3
    //later:
    $memcache->get($yourUniqueKey); //point4


    The Memcached with PHP code:

    First we make a database connection db.php.

    db.php

    $mysql_hostname = "localhost";
    $mysql_user = "username";
    $mysql_password = "password";
    $mysql_database = "database";
    $db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) ;
    mysql_select_db($mysql_database, $db);
    


    Then we make a index page index.php

    index.php

    include('db.php');
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    $key = md5('List'); // Unique Words
    $cache&_result = array();
    $cache&_result = $memcache->get($key); // Memcached object 
    if($cache_result)
    {
    // Second User Request
    $demos_result=$cache_result;
    }
    else
    {
    // First User Request 
    $v=mysql_query("select * from table_name order by id desc");
    while($row=mysql_fetch_array($v))
    $demos_result[]=$row; // Results storing in array
    $memcache->set($key, $demos_result, MEMCACHE_COMPRESSED, 1100); 
    // 1100 Seconds
    }
    // Result 
    foreach($demos_result as $row)
    {
    
        echo '<a href='.$row['link'].'>'.$row['title'].'</a>';
    
    }
    
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: