Install memcached and call PHP

Source: Internet
Author: User
Tags zts

1. Install libevent

Because the libevent class library is required for installing memcached, install libevent first.

1. Download

# Wget

Http://www.monkey.org /~ Provos/libevent-2.0.12-stable.tar.gz

 

2. Extract

# Tar xzfv libevent-2.0.12-stable.tar.gz

 

3. Enter the Directory

# Cd libevent-2.0.12-stable

 

4. Compile and install
#./Configure
# Make
# Make install
Note: by default/Usr/local/lib/Directory

Ii. Install memcached

Http://memcached.org/is the official website of memcached

1. Download

# Wget

Http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

 

2. Extract

# Tar xzfv memcached-1.4.5.tar.gz

 

3. Enter the Directory

# Cd memcached-1.4.5

 

4. Compile and install
./Configure -- prefix =/local/memcached
Make
Make install


After the installation is complete, the bin and share directories will appear in/local/memcached.
Run the bin directory to start memcache.
The method is as follows:
./Memcached-D-u nobody-M 512 127.0.0.1-P 11211
An exception is reported.
Error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: no such file or directory

The reason is that the libevent-2.0.so.5 class library cannot be found, the solution is as follows:

Use ld_debug = help./memcached-V to determine the path of the loaded class library. The method is as follows:
Ld_debug = libs./memcached-V 2> & 1>/dev/null | less

If (end) appears, you can directly enter P to exit.

# Ln-S/usr/local/lib/libevent-2.0.so.5/lib/libevent-2.0.so.5

Now memcached can be started normally.
./Memcached-D-u nobody-M 512 127.0.0.1-P 11211

Here, we can see that memcached has been started, indicating that the installation is successful.

Iii. Install the memcache PHP Module

 

# Wget

Http://pecl.php.net/get/memcache-2.2.4.tgz

# Tar zxvf memcache-2.2.4.tgz
# Cd memcache-2.2.4

Find phpize

# Whereis phpize

The local machine is/usr/bin/phpize

#/Usr/bin/phpize
#./Configure -- With-PHP-Config =/usr/bin/PHP-config
# Make
# Make install

Add a line to the php. ini file

PHP. ini in/etc extension=memcache.so Restart httpd #service httpd restart Use phpinfo () in PHP. The following information indicates that the memcached extension has been installed!

4. php calls memcachedWrite a PHP program and test it.
<? PHP $ memcache = new memcache; // create a memcache object $ memcache-> connect ('localhost', 11211) or die ("cocould not connect "); // connect to the memcached server $ memcache-> set ('key', 'test'); // set a variable to the memory, name is key value is test $ get_value = $ memcache-> get ('key'); // retrieve the key value from memory echo $ get_value;?>
Common Operations
<? PHP // connect to memcache $ mem = new memcache; $ mem-> connect ("localhost", 11211); // save data $ mem-> set ('key1 ', 'This is first value', 0, 60); $ val = $ mem-> get ('key1'); 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 array data $ arr = array ('aaa', 'bbb ', 'ccc', 'ddd '); $ mem-> set ('key2', $ arr, 0, 60); $ val2 = $ Mem-> get ('key2'); echo "Get key2 value:"; print_r ($ val2); echo "<br> "; // delete data $ mem-> Delete ('key1'); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ Val. "<br>"; // clear all data $ mem-> flush (); $ val2 = $ mem-> get ('key2'); echo "Get key2 value: "; print_r ($ val2); echo" <br> "; // close the connection $ mem-> close ();?>
Enable one or more processes on one or more machines.
Use two processes and two ports:
#./Memcached-D-u nobody-M 512 127.0.0.1-P 11211
#./Memcached-D-u nobody-M 512 127.0.0.1-P 11212
<? PHP // connect to memcache $ mem = new memcache; $ mem-> addserver ("localhost", 11211); $ mem-> addserver ("localhost", 11212 ); // Save the data $ mem-> set ('key1', 'this is first value', 0, 60); $ val = $ mem-> get ('key1 '); echo "Get key1 value :". $ Val. "<br>"; // save array data $ arr = array ('aaa', 'bbb ', 'ccc', 'ddd '); $ mem-> set ('key2', $ arr, 0, 60); $ val2 = $ mem-> get ('key2'); echo "Get key2 value :"; print_r ($ val2); echo "<br>"; // delete data $ mem-> Delete ( 'Key1'); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ Val. "<br>"; // close the connection $ mem-> close ();?>
V. memcached features and restrictions• There is no limit on the data size of items that can be stored in memcached. Only the memory is sufficient.
• The maximum memory used by a single memcached process is 2 GB. To use more memory, you can enable multiple memcached processes on multiple ports.
• A data expiration time of up to 30 days. If it is set to permanent, it will also expire at this time. The constant realtime_maxdelta
60*60*24*30 control
• The maximum key length is 250 bytes. If the key length is greater than this length, it cannot be stored. The constant key_max_length is controlled by 250.
• The maximum data size of a single item is 1 MB. Data larger than 1 MB is not stored. The constant power_block 1048576 is used for control,
It is the default slab size.
• The maximum number of simultaneous connections is 200 and is controlled through freetotal in conn_init (). The maximum number of soft connections is 1024.
Settings. maxconns = 1024 Control
• Parameters related to space occupation: settings. Factor = 1.25, settings. chunk_size = 48, which affects slab's data occupation and step-by-step viewing the internal working status of memcached
Access memcached: Telnet host name port number
View the total status: stats
View A status: stats curr_connections
Disable LRU
In some cases, the LRU mechanism may cause problems. When memcached is started, LRU can be disabled by the "-M" parameter,
As follows:
$ Memcached-m 1024
Note that the "-M" option in lower case is used to specify the maximum memory size. If no specific value is specified
The default value is 64 MB.
After the "-M" parameter is specified, memcached returns an error when the memory usage is exhausted. Come back, memcached bi
LRU is recommended because it is not a memory but a cache. Memcached works in thread mode
You must enable:./configure -- enable-threads during installation.
After the installation is complete, check whether the help information contains the following information during startup:
-T <num> Number of threads to use, default 4
If this option exists, it indicates that the thread is supported. You can use the-T option to start multithreading at startup.
Then, the number of threads you need to support must be added during startup:
/Usr/local/memcache/bin/memcached-T 1024 /////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////

Install and configure memcached in popular PHP installation methods

Change the version to the latest version.

1. Install memcached

# tar vxzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr/local/memcached
# make
# make install

Start the service after installation.

# cd /usr/local/memcached/bin
# ./memcached -d -m 50 -p 11211 -u root

Parameter description-M specifies the MB of cache space used;-P specifies the port to be listened on;-u specifies the user to run

2. Install the memcache PHP Module

# tar vxzf memcache-1.5.tgz
# cd memcache-1.5
# /usr/local/php/bin/phpize
// Note the following differences
#./Configure -- enable-memcache -- With-PHP-Config =/usr/local/PHP/bin/PHP-config -- With-zlib-Dir
# Make
# Make install

After the installation is complete, a message like this will be prompted:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/

Remember this, modify PHP. ini, and

extension_dir = "./"

Change

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"

Add a row

extension=memcache.so

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.