I just started learning Zend Framework and in one of the tutorials I read, I need to enable memcached in my PHP which is not available by default.

  1. Install libevent.

  2. Install memcached.

  3. Install libmemcached.

  4. Install memcache.

    wget http://pecl.php.net/get/memcache-2.2.6.tgz
    tar -zxvf memcache-2.2.6.tgz
    cd memcache-2.2.6
    phpize && ./configure --enable-memcache && make
    cp modules/memcache.so /usr/lib/php/extensions/
    
  5. Load the module using .ini files in /etc/php

    touch /etc/php/memcached.ini
    echo 'extension=memcache.so' > /etc/php/memcached.ini
    
  6. Run memcached as a daemon (d = daemon, m = memory, u = user, l = IP to listen to, p = port).

    memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211
    
  7. Put this file in your web directory and run it. It should be rendered without any error.

    <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    
    $version = $memcache->getVersion();
    echo "Server's version: ".$version."<br/>\n";
    
    $tmp_object = new stdClass;
    $tmp_object->str_attr = 'test';
    $tmp_object->int_attr = 123;
    
    $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
    echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
    
    $get_result = $memcache->get('key');
    echo "Data from the cache:<br/>\n";
    
    var_dump($get_result);
    ?>
    

References: