William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Tag Archives: memcached

PHP Memcached and autocomplete

PHP Memcached and autocomplete

For search-form autocomplete, it is also a good idea to use PHP-Memcached prior of MongoDB, and MySQL.

Why memcached? Because PHP-Memcached does *NOT* interact with SQL; it directly operates data in cache, so very suitable for quick-retrieving: autocomplete, mis-spelling, session etc.

The PHP-Memcached autocomplete processing is probably like this:

if(! empty($_GET['key'])) {
 $key = trim($_GET['key']);
 // check Memcached first.
 $result = Memcached::Find($key);
 if($result) return $result;
 // not found in Memcached, then turn to MongoDB, then MySQL.
 else { 
  // do call to MongoDB, MySQL sequently and return that result
 }
}

However, I discarded PHP-Memcached in my app after some bad experience. The reason I prefer to MongoDB other than PHP-Memcached are:

  • MongoDB has a better monitoring GUI tool than Memcached:
    moadmin.php is better than phpmemcachedadmin.
    Since both of them are hard to track and debug, so a good monitoring GUI tool is very important during developing and maintaining.
  • MongoDB stores persistent data, the data is kept physically in disk: hash files
  • PHP-Memcached data is kept in memory; that means when Memcached server stop/restart, the data will lost.
  • PHP-Memcached performance is not much higher than MongoDB;
    MongoDB is smart enough to store and optimize data, their performance are similar.
  • Other reasons.
    such as MongoDB+Memcached make thing complicated;
    in CentOS 5.8, Perl Memcached module is very difficult to compile and transfer.
  • So for a long and higher view, MongoDB is the better choice.

Summary

For search-form autocomplete feature, a good sequence for autocomplete probably be:

  1. Sphinx (for large data)
  2. PHP-Memcached (newer than PHP-Memcache)
  3. MongoDB (File/Hash always a good choice).
  4. MySQL

Among them, (1) and (2) can be ignored, (3) and (4) are necessary.

By the way, I extract some comments regarding on PHP-Memcached vs. MongoDB from web:

I use Mongo for everything that needs to be persistent (ie needs to be on a disk for later retrieval). I use memcached for incoming request data (between a few kb and a megabyte) that can be lost and does not need to be available beyond a few minutes.

using Memcache is more about reducing query load on the server than it is about speeding up queries.

A good memcache implementation basically just tries to keep the most common data in memory so that the database server can churn away on bigger stuff.

Advertisements

CentOS 6.2: install memcached server and PHP memcache, memcached extensions

CentOS 6.2: install memcached server and PHP extensions

Here I summary the steps of installing memcached server and PHP memcached/memcache extension which are working on CentOS 6.2.

A. Install memcached server

A good reference is Here. memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. My steps are:

$ sudo rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm 
$ sudo yum install memcached
$ /etc/init.d/memcached start

3-steps to install memcached server and make it running, very simple.

B. install PHP memcache extension

The installation of PHP memcache extension is much easier than memcached extension. At this point, memcache extension is quicker solution. Just 1 step:

$  sudo pecl install memcache

C. install PHP memcached extension

It is a challenge to install PHP’s memcached extension. A good article is here for the installation. There are 2 pre-required packaged are needed:

  • libevent
  • libmemcached

(1) First we need to install libevent support libaray (http://libevent.org/)

$ wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
$ tar xzvf libevent-2.0.20-stable.tar.gz
$ cd libevent-2.0.20-stable
$ ./configure
$ make
$ sudo make install

(2) Second install libmemcached. I try to find the latest stable version of libmemcached which is used by PHP’s memcached.
$ wget https://launchpad.net/libmemcached/1.0/1.0.12/+download/libmemcached-1.0.12.tar.gz
$ tar xzvf libmemcached-1.0.12.tar.gz
$ cd libmemcached-1.0.12
$ ./configure && $ make && $ sudo make install
$ sudo pecl install memcached
The pecl will auto install memached latest version 2.1 into server. However it is *NOT* work, because libmemcached’s version(1.0.12) is not compatible with PHP memcached version(2.1)

(3) I googled and tried a lower version of libmemcached: Using 1.0.10 instead of 1.0.12

$ wget http://download.tangent.org/libmemcached-1.0.10.tar.gz
$ ./configure --with-memcached; make; sudo make install
// then:
$ sudo pecl install memcached

It works! So the key here is the version compatible of libevent, libmemcached, and PHP memcached.

C. Add in php.ini as extensions

After all the parts work fine, now adding them into php.ini to make they are the PHP extension which can be quickly shared.
$ sudo vi /etc/php.ini:

[memcached]
extension=memcached.so

[memcache]
extension=memcache.so

After the adding, restart httpd server to make it activiate:
$ sudo /etc/init.d/httpd restart
$ sudo /etc/init.d/memcached restart

By using phpinfo.php, or ‘pecl list’, we can see all memcached, memcache extensions are available.
By the way, PHP official site have complete documents for these wonderful cache systems.

  1. Memcache: http://www.php.net/manual/en/book.memcache.php
  2. Memcached: http://www.php.net/manual/en/book.memcached.php
  3. Mongo: http://www.php.net/manual/en/book.mongo.php