Debian system installation memcached and PHP extension methods

Source: Internet
Author: User
Tags install php ini memcached php memcached socket zts

1, Memcache Introduction

Memcache is a free and open source, high-performance, allocated memory object caching system. Used to speed up dynamic Web applications and reduce database load. It can handle any number of connections and use non-blocking network IO. Since its working mechanism is to open up a space in memory, and then create a hash table, memcached to manage these hash tables.
Memcached is simple and powerful. Its simple design facilitates rapid deployment, easy to develop the problems faced, and solves many large data caches. Its APIs are available in the most popular languages.

Memcache Official website: http://memcached.org/

2, Memcached Introduction

Memcache is the project name for the system, memcached is the system's main program file (the letter D can be understood as daemon), running in one or more servers in a daemon, accepting client-side connection operations, and accessing data using shared memory.

3, PHP memcache and php memcached extension

PHP memcache and PHP memcached are memcache extensions of PHP, all memcache clients, but they are not the same thing.

PHP memcache based on PECL extension Library implementation, is the old client, from our practice has found that there are many problems, and less function, attributes can also be set less;
Http://pecl.php.net/package/memcache

PHP memcached is based on the libmemcached extension of native C, more perfect, it is recommended to replace the PHP memcached.
Http://pecl.php.net/package/memcached

Apt Way installs memcache

Execute the following code:

Apt-get Install memcached Php5-memcache php5-memcached

If you want to see what memcache related software is in the apt source, you can execute the following code:

Apt-cache Search Memcache

will appear as follows

Memcached–a high-performance Memory Object Caching system
Memcachedb–persistent storage Engine using the Memcache protocol
......
Php5-memcache–memcache Extension Module for PHP5
php5-memcached–memcached Extension Module for PHP5
Compile the Memcache installation method

1, Installation Memcache service end

wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
Tar xzf memcached-1.4.25.tar.gz
CD memcached-1.4.25
./configure--prefix=/usr/local/memcached
Make && make install

Ln-s/usr/local/memcached/bin/memcached/usr/bin/memcached
CP scripts/memcached.sysv/etc/init.d/memcached
Sed-i ' s@^user=.* @USER =root@ '/etc/init.d/memcached
Sed-i ' s@chown@ #chown @ '/etc/init.d/memcached
Sed-i ' s@/var/run/memcached/memcached.pid@/var/run/memcached.pid@ '/etc/init.d/memcached
Sed-i ' s@^prog=.* @prog = "/usr/local/memcached/bin/memcached" @ '/etc/init.d/memcached #前面有软链接, where you can omit
chmod +x/etc/init.d/memcached

Start the Memcache service side

Service memcached Start #或者执行下面
Memcached-p 11211-l 127.0.0.1-d-u root-p/var/run/memcached.pid-m 64m-c 1024

Explain the parameters

-p memcached Listening TCP port
-L Listening IP address, 127.0.0.1 is this machine, of course, you can also write your server IP, such as: 10.0.0.10,
This is the IP address of my server, if you need multiple servers to be able to read this memcached cache data,
Then you have to set this IP
-D runs in daemon, putting programs in the background
-U memcached run user, I set the nobody
-P memcached PID file path
The maximum amount of memory that can be used by-M memcached
-C memcached Maximum number of connections that can be accepted at the same time
If you want to access memcached by socket, you must remove the-l and-P arguments at startup, plus the-s argument:

-s memcached socket file path

2. Install php memcache Extension

wget http://pecl.php.net/get/memcache-3.0.8.tgz
Tar xzf memcache-3.0.8.tgz
CD memcache-3.0.8
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make && make install

The above installation will have a similar hint:

Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/

Change the Extension_dir = "./" in php.ini to

Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/"

Add a row to load the memcache extension:

Extension=memcache.so

3. Install php memcached Extension

First install libmemcached

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
Tar xzf libmemcached-1.0.18.tar.gz
CD libmemcached-1.0.18
Sed-i "s@lthread-pthread-pthreads@lthread-lpthread-pthreads@"./configure
./configure--with-memcached=/usr/local/memcached
Make && make install

Then install the PHP memcached extension

wget http://pecl.php.net/get/memcached-2.2.0.tgz
Tar xzf memcached-2.2.0.tgz
CD memcached-2.2.0
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make && make install

Change the Extension_dir = "./" in php.ini to


Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/"

Add a row to load the memcache extension:

Extension=memcached.so

View Memcache Installation Effects

Execute the following code:

Netstat-tap | grep memcached

If you are running, you will get the following results:

TCP 0 0 localhost:11211 *:* LISTEN 2132/memcached


The Phpinfo () function is then used to see if the extension has been successfully installed.

Example

<?php

Require_once '.. /includes/common.inc.php ';

$memcache = new Memcache;

$memcache->connect (' localhost ', 11211) or die ("could not Connect");


$version = $memcache->getversion ();

echo "Server" version: ". $version." <br/>\n ";

Echo ' <br>=================================== ';

$data = Array ();

$data 2 = array ();


$sql = "SELECT * from T1";

Echo $sql;

$key = MD5 ($sql);

Echo ' <br> ', $key;

/* $data 2 = $memcache->get ($key);

Echo ' <pre> ';

Print_r ($data 2);

Echo ' </pre> ';

exit;*/

if (!) ( $data 2 = $memcache->get ($key)))

{

$query = $db->query ($sql);

while ($rs = $db->fetch_array ($query))

{

$data [] = $rs;

}

$ts = $memcache->add ($key, $data, 10);

$ts = $memcache->set ($key, $data, memcache_compressed,60);

if ($ts)

{

Echo ' <br>add success ';

}

$memcache->set ($key, $data, 30);

$data 2 = $data;

}
Echo ' <pre> ';

Print_r ($data 2);

Echo ' </pre> ';

Echo ' <pre> ';

Print_r ($memcache->getstats ());

Echo ' </pre> ';

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.