There are two processes:Memcache ServerInstallation andMemcached Client.
The so-called server-side installation is to install memcache on the server (generally Linux systems) to store data.
The so-called Client installation refers to PhP (or other programs, memcache and other good API interfaces) to use the functions provided by memcache on the server side. php needs to add extensions.
1) install the Memcache Server
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 we need to describe the memcached service startup parameters:
-P listening port
-L connected IP address. The default value is local
-D start: Start the memcached service.
-D restart: restart the memcached service.
-D stop | Shutdown the running memcached Service
-D install the memcached Service
-D uninstall memcached Service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C: Maximum number of simultaneous connections. The default value is 1024.
-F block size growth factor. The default value is 1.25-n. The minimum allocation space is 48 for key + value + flags.
-H Show Help
2) install the Memcache client (for example, PHP5)
Sudo apt-get install php5-memcache
After the installation, we need to perform simple configuration in PHP. ini. Open the/etc/PhP5/apache2/PHP. ini file and add the following content at the end:
[Memcache]
Whether to transparently transfer data to other servers in case of errors.
Memcache. allow_failover = On
The maximum number of servers allowed to receive and send data, which is valid only when memcache. allow_failover is enabled. Memcache. max_failover_attempts = 20
Data will be transferred according to the block size set by this value. The smaller the value, the more extra network transmission is required.
If you find that the speed cannot be explained is reduced, you can try to increase this value to 32768.
Memcache. chunk_size = 8192
The default TCP port used to connect to the memcached server.
Memcache. default_port = 11111
Control the policy to map keys to the server. The default value "standard" indicates that the old hash policy of the previous version is used.
Set as "consistent" to allow you to add/delete servers in the connection pool without re-computing the ing between keys and servers.
; Memcache. hash_strategy = "standard"; controls the hash function that maps keys to the server. The default value "crc32" uses the CRC32 algorithm, while "fnv" indicates that the FNV-1a algorithm is used.
; FNV-1a than CRC32 speed is slightly lower, but the hash effect is better.
; Memcache. hash_function = "crc32 ″
LastSave php. ini and run sudo/etc/init. d/apache2 restart to restart Apache.
3) Use Memcache in PHP
<? PHP
$ Mem = new memcache; // creates a memcache object.
$ Mem-> connect ("127.0.0.1", 11111); // connect to the memcache Server
$ Val = "This Is A memcache test .";
$ Key = MD5 ($ Val );
$ Mem-> set ($ key, $ Val, 0,120); // adds a cache to be inserted. The cache time is 120 s.
If ($ k = $ mem-> get ('key') {// determines whether the specified key is obtained.
Echo 'fromcache: '. $ K;
} Else {
Echo 'normal'; // here we need to replace it with query database and create cache in actual use.
}
?>
So farThrough the above steps, we have completed the configuration and basic use of memcache...
4) methods provided by php5-memcache Extension
Memcache: Add-Add a value. if it already exists, false is returned.
Memcache: addserver-add an available server address
Memcache: Close-close a memcache object
Memcache: connect-create a memcache object
Memcache_debug-control debugging Function
Memcache: decrement-deletes the value of a saved key.
Memcache: delete-delete a key value
Memcache: flush-Clear All cached data
Memcache: get-get a key value
Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.
Memcache: getServerStatus-get the parameters of the running server
Memcache: getStats-return some running statistics of the server
Memcache: getVersion-returns the version information of the running Memcache.
Memcache: increment-adds the value of a saved key.
Memcache: pconnect-creates a persistent connection object for Memcache.
Memcache: replace-R: overwrites an existing key.
Memcache: set-Add a value. if it already exists, overwrite it.
Memcache: setCompressThreshold-compresses data larger than a certain size
Memcache: setServerParams-Modify server parameters at runtime
The following describes how to install and use Memcached on ubuntu 10.04:
1. Download, decompress, and 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 successfully installed:
# Ls-al/usr/lib | grep libevent
3. Download and decompress the package and 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 whether memcached is successfully installed:
# Ls-al/usr/local/bin/mem *
5. Start the memcache Server:
#/Usr/local/bin/memcached-d-m 10-u root-l127.0.0.1-p 11211-c 256-P/tmp/memcached. pid
-D option is to start a daemon,
-M indicates the amount of memory allocated to memcache. The unit is mb. Here I am 10 MB,
-U is the user who runs memcache. Here I am root,
-L is the IP address of the listener server. If there are multiple IP addresses, I have specified the Server IP address 127.0.0.1,
-P is the port for memcache listening. I have set port 11211 here, preferably port 1024 or above,
-The "C" option is the maximum number of concurrent connections. The default value is 1024. I have set 256 here, which is based on the load of your server,
-P is the PID file for saving memcache. Here I save it in/tmp/memcached. PID,
6. to end the memcache process, run:
# Kill 'cat/tmp/memcached. Pi'
You can also start multiple daemon processes, but the ports cannot be repeated.
7. Restart the web Service
/Etc/init. d/lighttpd-flighttpd. conf restart
8. Memcache environment test:
Telnet local host 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
// Add at the end
Append good 32 0 5
After
STORED
Get good
VALUE good 32 15
Worldhelloafter
END
// Add a header
Prepend good 32 0 6
Before
STORED
Get good
VALUE good 32 21
Beforeworldhelloafter
END
// Delete
Delete good
DELETED
Get good
END
Install the server
Sudo apt-get install memcached
$ Memcached-d-m 50-p 11211-uroot
Parameter description-m specifies the MB of cache space used;-p specifies the port to be listened on;-u specifies the user to run
Install the php Module
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, 60 );
$ Val = $ mem-> get ('key ');
Echo $ val;