Install, configure, and use memcache in the cache system

Source: Internet
Author: User

Software Introduction
[My Linux makes Linux easier to use]MemcachedIt is a distributed cache or cache system developed by danga interactive for livejournal, but it is currently used by many software such as mediawiki. This is a set of open-source software that is licensed by BSD license.
Memcached lacks authentication and security control, which means that the memcached server should be placed behind the firewall.
Memcached's API uses a-bit cyclic redundancy check (CRC-32) to calculate the key value and scatter the data on different machines. When the table is full, the new information will be replaced by the LRU mechanism. Because memcached is generally used as a cache system, memcached applications require additional code to update information in memcached when writing back a slow system (such as a back-end database. Memcached has a variety of language client development kit, including: PERL/PHP/Java/C/Python/Ruby/C #/MySQL/client package is: http://www.danga.com/memcached/apis.bml

Download and install
Install memcache on the server
Wget http://www.monkey.org /~ Provos/libevent-1.4.11-stable.tar.gz
Wget http://danga.com/memcached/dist/memcached-1.4.0.tar.gz

Tar-zxvf libevent-1.4.11-stable.tar.gz
CD libevent-1.4.11-stable
Make clean
./Configure -- prefix =/usr
Make; make install

Tar zxvf memcached-1.4.0.tar.gz
CD memcached-1.4.0
./Configure -- prefix =/www/wdlinux/memcached -- With-libevent =/usr
Make; make install
Installation Complete, start
/Www/wdlinux/memcached/bin/memcached-D-M 512-u APACHE-P 11211-C 25600
-D start as a daemon
-M 512 refers to allocating MB of memory.
-U Apache is started with an Apache user.
-P 11211 port 11211
-C 25600 maximum number of outgoing connections

PHP extension (that is, the client), that is, to allow PHP to support memcache (assuming that this server is configured with a lamp or lnmp application environment, you can install my customized version (http://www.wdlinux.cn/wdlinux_CentOS_5.5)
Download and install
Wget http://pecl.PHP.net/get/memcache-2.2.5.tgz
Tar zxvf memcache-2.2.5.tgz
CD memcache-2.2.5
/Www/wdlinux/PHP/bin/phpize
./Configure -- enable-memcache -- With-PHP-Config =/www/wdlinux/PHP/bin/PHP-config -- With-zlib-Dir
Make; make install

Add the following lines at the end of PHP. ini, such as VI/www/wdlinux/PHP/lib/PHP. ini.
[Memcache]
Extension_dir = "/www/wdlinux/PHP/lib/PHP/extensions/no-debug-zts-20060613 /"
Extension = memcache. So

Restart Apache
Service httpd restart

Memcache status Web InterfaceCopy memcache. php to the root directory of the website, as shown in figure
CP memcache. php/www/web/wdlinux
Modify the user password as follows:
VI memcache. php
Define ('admin _ username', 'wdlinux '); // admin Username
Define ('admin _ password', 'Password'); // admin password

Then open http: // 192.168.1.200/memcache. php in IE browser to view the relevant status information.

Enable phpinfo check, as shown in figure
Http: // 192.168.1.200/phpinfo. php
If you can see the following information, the installation is successful, that is, your PHP already supports memcache.

In your PHP program, you can use the memcache cache database.

PHP test instance

<? PHP
// Connection
$ Mem = new memcache;
$ Mem-> connect ("127.0.0.1", 11211 );

// Save data
$ Mem-> set ('key1', 'this is first value', 0, 60 );
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ Val ."
";

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

// Save the Array
$ Arr = array ('aaa', 'bbb ', 'ccc', 'ddd ');
$ Mem-> set ('key2', $ arr, 0, 60 );
$ Val2 = $ mem-> get ('key2 ');
Echo "Get key2 value :";
Print_r ($ val2 );
Echo"
";

// Delete data
$ Mem-> Delete ('key1 ');
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ Val ."
";

// Clear all data
$ Mem-> flush ();
$ Val2 = $ mem-> get ('key2 ');
Echo "Get key2 value :";
Print_r ($ val2 );
Echo"
";

// Close the connection
$ Mem-> close ();
?>

You are welcome to reprint it, but please keep this information
[My Linux makes Linux easier to use] centos Lite version integrates lamp, lnmp, wdcp, wdcdn, wddns, one-click installation package, Cluster load balancing LVS, smart DNS/CDN, and Performance Optimization
Connection: http://www.wdlinux.cn/memcache_install

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.