Many large load sites around the world use memcache as a cache service to share the pressure on database servers.
Here we will only introduce how to configure memcache in Ubuntu and how to use memecache in PHP. For more information, such as the operating mechanism of memcache or more advanced applications, Google... Pai_^
About memcache and memcached
Memcache is the project name and memcached is the service name. It makes a lot of new contacts feel inexplicable. I personally think it is more correct to use the former.
Install memcache
Memcache is divided into two parts: memcache server and client. The memcache server runs as a Service. The creation, storage, and deletion of all data caches are actually completed here. The client here refers to the extensions that PHP can call.
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
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]
A high-performance distributed memory object cache system maintains a unified and huge hash table in the memory,
It can be used to store data in various formats, including images, videos, files, and database retrieval results.
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 is CRC32.AlgorithmAnd "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 ″
Save PHP. ini and run sudo/etc/init. d/apache2 restart to restart Apache.
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 'from cache: '. $ K;
} Else {
Echo 'normal'; // here we need to replace it with query database and create cache in actual use.
}
?>
For keys, the MD5 query statement is usually used to obtain them. It is determined according to the specific needs in actual use...
Through the above steps, we have completed the configuration and basic usage of memcache...
Method 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