1. Install libevent. Because the libevent class library is required for memcached installation, install libevent1. download # wgetwww.monkey.org ~ Provoslibevent-2.0.12-stable.tar.gz2.decompress unzip tarxzfvlibevent-2.0.12-stable.tar.gz 3. Go to the directory # cdlibevent-2.0.12-stable
1: Install libevent because memcached installation, you need to use the libevent class library, so first install libevent 1. Download # wget http://www.monkey.org /~ Provos/libevent-2.0.12-stable.tar.gz 2. Unzip # tar xzfv libevent-2.0.12-stable.tar.gz 3. Go to directory # cd libevent-2.0.12-stable
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 memcached
Write a PHP program and test it.
Connect ('localhost', 11211) or die ("cocould not connect"); // connect to the Memcached server $ memcache-> set ('key', 'test '); // set a variable to the memory and its name is test $ get_value = $ memcache-> get ('key '); // retrieve the key value from memory echo $ get_value;?>
Common Operations
Connect ("localhost", 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 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"
"; // 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 ();?>
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
AddServer ("localhost", 11211); $ mem-> addServer ("localhost", 11212); // save data $ mem-> set ('key1 ', 'This is first value', 0, 60); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ val."
"; // 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"
"; // Delete data $ mem-> delete ('key1'); $ val = $ mem-> get ('key1'); echo" Get key1 value :". $ val."
"; // 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.
? The maximum data expiration time of 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 the data occupation and step-by-step mode of slab.
View 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-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 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