Search code examples
phpmysqlmemcachedpecl

PECL/Memcache and PECL/Memcached


I have both PECLs installed in my PHP setup, and when I do php -V I can see both running. In my /etc/php5/conf.d/ folder both of these PECLs are enabled. When I checked phpinfo, then I came to know PECL/Memcache is used as it had the assigned port number 11211. Both PECLs have their own config in .ini files but only one is used. I know we cannot use both at a time, but I want to test both PECLs one at a time. My question is how to test these PECLs without deleting anyone, by just changing the settings. I want to test Memcache and disable it and test Memcached before deciding what to use. Is there any way I can accomplish it without having to delete ?

Currently I am using PhpMemcachedAdmin to check Memcached, its still in beta and buggy. If anyone could suggest me some other better tools then It'd be really helpful.


Solution

  • You can have both extensions active at the same time. Both, PECL Memcache and PECL Memcached are PHP clients connecting to a Memcached server.

    Concerning the pros and cons

    As said before, Memcached build upon libmemcached which is maintained by the the memcached server team. People say it's better, but I could not verify this to that day.

    PECL memcache still has the better PHP session support.

    I found (a bit older, but mostly still valid) comparison chart here

    EDIT

    PHP is using either of the extensions to connect to the Memcached server. Here are some usage examples

    PECL Memcached usage example in PHP

    PECL Memcached class reference

    // create PECL memcached Object
    $m = new Memcached();
    
    // add a server, connection is established lazily as far as I know
    $m->addServer( 'localhost', 11211 );
    
    // set values, those are written into your server's memory using the memcached server
    $m->set( 'integer', 1 );
    $m->set( 'intIncrement', 1 );
    $m->set( 'intDecrement', 1 );
    $m->set( 'string', 'I am a string' );
    $m->set( 'array', array( 'a', 'b', 'c' ) );
    
    // increment
    $m->increment( 'intIncrement' );
    
    // decrement
    $m->decrement( 'intDecrement' );
    
    // get values again
    var_dump ( $m->get( 'integer' ) );
    var_dump ( $m->get( 'intIncrement' ) );
    var_dump ( $m->get( 'intDecrement' ) );
    var_dump ( $m->get( 'string' ) );
    var_dump ( $m->get( 'array' ) );
    
    // delete values
    $m->delete( 'integer' );
    $m->delete( 'intIncrement' );
    $m->delete( 'intDecrement' );
    $m->delete( 'string' );
    $m->delete( 'array' );
    
    // close connection
    $m->close();
    

    PECL Memcache usage example in PHP

    PECL Memcache class reference

    This can be in the same PHP script!

    // create PECL memcache object
    $m = new Memcache;
    
    // connect to the memcached server
    $m->connect('localhost', 11211);
    
    // set values
    $m->set( 'integer', 1 );
    $m->set( 'intIncrement', 1 );
    $m->set( 'intDecrement', 1 );
    $m->set( 'string', 'I am a string' );
    $m->set( 'array', array( 'a', 'b', 'c' ) );
    
    // increment
    $m->increment( 'intIncrement' );
    
    // decrement
    $m->decrement( 'intDecrement' );
    
    // get values again
    var_dump ( $m->get( 'integer' ) );
    var_dump ( $m->get( 'intIncrement' ) );
    var_dump ( $m->get( 'intDecrement' ) );
    var_dump ( $m->get( 'string' ) );
    var_dump ( $m->get( 'array' ) );
    
    // delete values
    $m->delete( 'integer' );
    $m->delete( 'intIncrement' );
    $m->delete( 'intDecrement' );
    $m->delete( 'string' );
    $m->delete( 'array' );
    
    // close connection
    $m->close();
    

    You can also store objects into memcached, check out this article about object serialization and the magic class methods __sleep and __wakeup() in PHP

    As you can easily see, the basic usage of both clients, PECL Memcached and Memcache is pretty identical.