Divided into two procedures: Memcache server-side installation and memcached client installation.
The so-called server-side installation is on the server (typically Linux system) on the installation of Memcache implementation data storage
The so-called client installation means PHP (or other programs, memcache and other good API interface) to use the server-side memcache provided functions, the need for PHP to add extensions.
1) Install Memcache service End
sudo apt-get install memcached
After installing the Memcache server, we need to start the service:
memcached-d-M 128-p 11111-u root
Here you need to explain the startup parameters for the memcached service:
-P Listening Port
The IP address of the-l connection, the default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown shut down the running memcached service
-D Install installation memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when run as root)
-m maximum memory usage, in MB. Default 64MB
-Returns an error when M memory is exhausted instead of deleting the item
-C Maximum Simultaneous connection number, default is 1024
-F Block size growth factor, default is 1.25-n minimum allocation space, key+value+flags default is 48
-H Display Help
2 Install Memcache client (PHP5 for example)
sudo apt-get install Php5-memcache
After installation we need to do a simple configuration in the php.ini, open the/etc/php5/apache2/php.ini file at the end to add the following:
[Memcache]
; Whether to transparently failover to other servers when an error is encountered.
Memcache.allow_failover = On
; The maximum number of servers to try when accepting and sending data, only when opening memcache.allow_failover. Memcache.max_failover_attempts = 20
Data is transferred according to the block size set by this value. The smaller the value, the more network traffic is required.
; If you find an unexplained rate decrease, you can try to increase this value to 32768.
Memcache.chunk_size = 8192
; The default TCP port to use when connecting to the memcached server.
Memcache.default_port = 11111
; Controls the policy that maps the key to the server. The default value "Standard" represents the old hash policy using the previous version.
; Set to "consistent" allows you to add/Remove servers in a connection pool without having to recalculate the mapping relationship between key and server.
; memcache.hash_strategy = "standard"; Controls the hash function that maps the key to the server. The default value, Crc32″, uses the CRC32 algorithm, whereas "FNV" means the FNV-1A algorithm is used.
; The fnv-1a is slightly lower than the CRC32 speed, but the hash effect is better.
; memcache.hash_function = "Crc32″
finally , save the php.ini and perform the sudo/etc/init.d/apache2 restart restart Apache.
3 The use of memcache in PHP
<?php
$mem = new Memcache; Creating Memcache Objects
$mem->connect ("127.0.0.1″, 11111"); Connecting Memcache servers
$val = "This is a memcache test.";
$key = MD5 ($val);
$mem->set ($key, $val, 0, 120); Add a cache insertion, cache time of 120s
if (($k = $mem->get (' key ')) {//To determine whether to get the specified key
Echo ' Fromcache: '. $k;
} else {
echo ' normal '; Here we need to replace the query database and create the cache in actual use.
}
?>
so far, through the above steps, we have completed the Memcache configuration and basic use ...
4) Php5-memcache extension provided method
Memcache::add-adds a value and returns False if it already exists
memcache::addserver-add a server address to use
Memcache::close-closes a Memcache object
memcache::connect-Create a Memcache object
memcache_debug-Control Debugging function
Memcache::d ecrement-to subtract values from a key that is saved
Memcache: Delete a key value:d elete-
memcache::flush-clears all cached data
Memcache::get-gets a key value
memcache::getextendedstats-get run System statistics for all processes in the process pool
memcache::getserverstatus-get the parameters to run the server
memcache::getstats-returns some running statistics for the server
memcache::getversion-returns the version information of the running Memcache
memcache::increment-an addition to the value in a key that is saved
Memcache: Create a Memcache Persistent connection object:p connect-
Memcache::replace-r Overwrite an existing key
memcache::set-Add a value, if it already exists, overwrite
memcache::setcompressthreshold-compression of data larger than a certain size
memcache::setserverparams-Modify server parameters at run time
The following is the installation and use of memcached on the operating system Ubuntu 10.04:
1. Download decompression, install Libevent
mkdir ~/SRC
CD ~/SRC
Wgethttp://www.monkey.org/~provos/libevent-2.0.10-stable.tar.gz
Tar xzvf libevent-2.0.10-stable.tar.gz
CD libevent-2.0.10-stable
./configure--prefix=/usr
Make
sudo make install
Cd..
2. Test whether the Libevent is installed successfully:
# Ls-al/usr/lib | grep libevent
3. Download decompression, install memcached
Wgethttp://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
Tar xzvf memcached-1.4.5.tar.gz
CD memcached-1.4.5
./configure--with-libevent=/usr
Make
sudo make install
Cd..
4. Test for successful installation of memcached:
# ls-al/usr/local/bin/mem*
5. Start the Memcache server side:
#/usr/local/bin/memcached-d-M 10-u root-l127.0.0.1-p 11211-c 256-p/tmp/memcached.pid
The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, I am 10MB here,
-U is the user running memcache, I am here root,
-L is a listening server IP address, if there are more than one address, I specify the IP address of the server 127.0.0.1,
-P is the port that sets the memcache listening, I set 11211 here, preferably 1024 or more ports,
The-c option is the maximum number of concurrent connections to run, the default is 1024, I set 256 here, according to the load of your server to set,
-P is set to save the Memcache pid file, which I am here to save in/tmp/memcached.pid,
6. If you want to end the memcache process, execute:
# Kill ' Cat/tmp/memcached.pid '
You can also start multiple daemons, but the ports cannot be duplicated.
7. Restart Web Services
/etc/init.d/lighttpd-flighttpd.conf Restart
8. Memcache Environment test:
telnet localhost 11211
Save
Set
Good 32 0 10
HelloWorld
STORED
Retrieve
Gets good
VALUE Good 32 10 10
HelloWorld
End
Replace
Replace good 32 0 10
Worldhello
STORED
Get good
VALUE Good 32 10
Worldhello
End
Tail add
Append Good 32 0 5
After
STORED
Get good
VALUE Good 32 15
Worldhelloafter
End
Head add
Prepend Good 32 0 6
Before
STORED
Get good
VALUE Good 32 21
Beforeworldhelloafter
End
Delete
Delete Good
DELETED
Get good
End
Install server
sudo apt-get install memcached
$ memcached-d-M 50-p 11211-uroot
Parameter Description-m specifies the number of megabytes of cache space to use;-p specifies the port to listen to, and-u specifies which user to run
Installing PHP Modules
sudo apt-get install Php5-memcache
Edit configuration file
$ sudo vim/etc/php5/conf.d/memcache.ini
; Uncomment the next line to Enablethe module
extension=memcache.so
[Memcache]
memcache.dbpath= "/var/lib/memcache"
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);
$mem->set (' key ', ' This is amemcached test! ', 0);
$val = $mem->get (' key ');
echo $val;