Accelerated Access to apache + php + memcached + mysql

Source: Internet
Author: User
Tags zts

Memcache introduction:

Memcached is a free and open source code, high-performance, distributed memory object cache system. It can be used to accelerate dynamic web applications and reduce database load. By maintaining a unified and huge hash table in the memory, you can store data in various formats, including images, videos, files, and database query results.


Here, memcache should be different from memcache in php. memcache in php is supported by php, while memcached is the master program file of the server, and the Server Installation program. If you want to use memcache to cache the system, both memcache and memcached must be installed. Memcache is a memory cache that caches frequently accessed data or objects in the memory. The data cached in the memory is accessed through APIS, the data is like a large hash table. By caching common data or objects, the database load is reduced and the website response speed is improved. In general, the client sends a request to reach memcache. If the requested data exists in memcache, the request data is directly returned without any operations on the data. If the requested data does not exist in memcache, query the database, return the data obtained from the database to the client, and cache the data to memcache. The data in memcache is updated at the same time as the database is updated to ensure that the data in memcache is consistent with the data in the database.


Simple Principle of memcache

-------------------------------------------------------------------------

Install configurations

After briefly introducing the principle, start to install the configuration. The basis is lamp.

Memcache and memcached

[Root @ localhost softs] # wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz

[Root @ localhost softs] # wget http://pecl.php.net/get/memcache-2.2.6.tgz

[Root @ localhost softs] # ls memcache *-l
-Rw-r -- 1 root 35957 Oct 4 2010 memcache-2.2.6.tgz ------------------ php extension for memcache
-Rw-r -- 1 root 320751 Feb 3 2012 memcached-1.4.13.tar.gz ---------------------- memcache server software

Install memcached

Libevent is required to install the software. To prevent the software version from being too low, we do not recommend yum installation.

Install libevent

[Root @ localhost softs] # wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

[Root @ localhost softs] # tar xf libevent-2.0.21-stable.tar.gz-C/usr/src/
[Root @ localhost softs] # cd/usr/src/libevent-2.0.21-stable/

[Root @ localhost libevent-2.0.21-stable] #./configure & make install

Install memcached

[Root @ localhost softs] # tar xf memcached-1.4.13.tar.gz-C/usr/src/
[Root @ localhost softs] # cd/usr/src/memcached-1.4.13/

[Root @ localhost memcached-1.4.13] #./configure -- with-libevent =/usr/local & make install

[Root @ localhost run] # memcached-m 32 m-p 11211-d-u root-P/var/run/memcached. pid-c 256 ---------- start the memcached Process

-P indicates the tcp port used. The default value is 11211.-m indicates the maximum memory block. The default value is 64 mb.-d indicates that the daemon runs in the background.-c indicates the maximum number of concurrent operations, the default value is 1024;-P sets to save the memcached pid file;

-U indicates the user running memcached. By default, memcached cannot be started by the root user. Therefore, you must specify this parameter when the current user is root.-l indicates the address of the listening server if multiple addresses exist.
[Root @ localhost memcached-1.4.13] # ps-e | grep mem
16499? 00:00:00 memcached
[Root @ localhost memcached-1.4.13] # netstat-tunlp | grep mem
Tcp 0 0 0.0.0.0: 11211 0.0.0.0: * LISTEN 16499/memcached
Udp 0 0 0.0.0.0: 11211 0.0.0.0: * 16499/memcached
[Root @ localhost memcached-1.4.13] #

Disable memcached

[Root @ localhost run] # cat/var/run/memcached. pid
16532
[Root @ localhost run] # kill-9 16532

Install memcache extension for php

[Root @ localhost softs] # tar xf memcache-2.2.6.tgz-C/usr/src/
[Root @ localhost softs] # cd/usr/src/memcache-2.2.6/

[Root @ localhost: memcache-2.2.6] # ls
CREDITS memcache. php
README memcache_consistent_hash.c
Config. m4 memcache_queue.c
Config. w32 memcache_queue.h
Config9.m4 memcache_session.c
Example. php memcache_standard_hash.c
Memcache. c php_memcache.h
Memcache. dsp
[Root @ localhost memcache-2.2.6] # phpize
Processing ing:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626

[Root @ localhost: memcache-2.2.6] # ls
CREDITS config. sub memcache. php
Makefile. global config. w32 memcache_consistent_hash.c
README config9.m4 memcache_queue.c
Acinclude. m4 configure memcache_queue.h
Aclocal. m4 configure. in memcache_session.c
Autom4te. cache example. php memcache_standard_hash.c
Build install-sh missing
Config. guess ltmain. sh mkinstalldirs
Config. h. in memcache. c php_memcache.h
Config. m4 memcache. dsp run-tests.php
[Root @ localhost memcache-2.2.6] #

[Root @ localhost memcache-2.2.6] #./configure -- enable-memcache -- with-php-config =/usr/local/bin/php-config

[Root @ localhost memcache-2.2.6] # make

[Root @ localhost memcache-2.2.6] # make install
Installing shared extensions:/usr/local/lib/php/extensions/no-debug-zts-20090626/

[Root @ localhost memcache-2.2.6] # cd modules/
[Root @ localhost modules] # ls
Memcache. so
[Root @ localhost modules] # pwd
/Usr/src/memcache-2.2.6/modules

Next, modify the php configuration file php. ini.

[Root @ localhost lib] # vi php. ini

Extension = "/usr/local/lib/php/extensions/no-debug-zts-20090626 /"

Extension = memcache. so --------------- add these two lines to the file

Test whether the php extension of memcache is successfully installed.

[Root @ localhost lib] # memcached-m 32 m-p 11211-u root-P/var/run/memcached. pid-c 256-d
[Root @ localhost lib] #/etc/init. d/apache start

[Root @ localhost ~] # Cd/www/--------------------/www is my apache Document Root

[Root @ localhost www] # vi mem_test.php
<? Php
$ Mem = new Memcache;
$ Mem-> connect ("127.0.0.1", 11211 );
$ Mem-> set ('hello', 'World', 0, 60 );
$ Val = $ mem-> get ('hello ');
Echo $ val;
?>

[Root @ localhost www] # elinks -- dump 127.0.0.1/mem_test.php
World

If you can see the world, your memcachephp extension will be successful.

-------------------------------------------------------------------------

Use memcache with php

Php and memcache combined test code

[Root @ localhost www.www] # vi php-mem.php
<? Php
// Connect
$ Mem = new Memcache; -------- initialize a memcache object
$ Mem-> connect ('localhost', 11211); ------------- connect to the memcache server, IP address, and Port

// Save data
$ Mem-> set ('key1', 'this is first value',); ------- key of the key data, used to locate a data; the second parameter is the data content to be saved. The third parameter is the tag, which is generally 0. The fourth parameter is the effective time of the mixed village, and the time-out will be cleared.
$ Val = $ mem-> get ('key1'); ------------- get is to get data and get key1 data
Echo "Get key1 value:". $ val. "<br/> ";

// Replace data
$ Mem-> replace ('key1', 'this is replace value', 0, 60 );
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ val. "<br/> ";

// Save data group
$ Arr = array ('A', 'bb ', 'cc ');
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ val. "<br/> ";


// Replace data
$ Mem-> replace ('key1', 'this is replace value',); ----------- replace key1
$ Val = $ mem-> get ('key1'); ------------- save key1
Echo "Get key1 value:". $ val. "<br/> ";


// Save data group
$ Arr = array ('A', 'bb ', 'cc ');
$ Mem-> set ('key2', $ arr, 0, 60 );
$ Val2 = $ mem-> get ('key2 ');
Echo "Get key2 value :";
Print_r ($ val2 );
Echo "<br/> ";


// Delete data
$ Mem-> delete ('key1'); ----------- delete key1
$ Val = $ mem-> get ('key1'); -------------- save key1
Echo "get key1 value:". $ val. "<br/> ";


// Close connetions
$ Mem-> close (); -------- close

?>
"Php-mem.php" [New] 36L, 678C written
[Root @ localhost www] # elinks -- dump 127.0.0.1/php-mem.php
Get key1 value: this is first value
Get key1 value: this is replace value
Get key2 value: Array ([0] => aa [1] => bb [2] => cc)
Get key1 value:

Now it is successful.


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.