The memcached module in php is very useful in actual application development, especially for websites with large traffic volumes. its main function is to cache data into the memory, the read speed will be improved by times when it is used, but this requires the cost of memory usage. the memcached module in php is very useful in actual application development, especially for websites with large traffic volumes. its main function is to cache data into the memory, the read speed will be improved by times when it is used, but this requires the cost of memory usage.
Install the memcached module of php, remove ";" from/etc/php5/conf. d/memcached. ini, restart apache, and run phpinfo () to display memcached information.
Run:
/Usr/local/bin/memcached-d-m 10-u root-l 127.0.0.1-p 11211-c 256-P/tmp/memcached. pid
Memcached service officially started
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
We recommend that you use an object-oriented method to test this library:
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
$ Version = $ memcache-> getVersion ();
Echo "Server's version:". $ version. "\ n ";
?>
Memcache: the getVersion method returns the version information of the running Memcache.
Memcache: The getStats method is used to return some running statistics of the server. Memcache: The getStats method has three parameters. The first parameter indicates the type to be returned: reset, malloc, maps, cachedump, slabs, items, sizes; the second and third parameters are used when the first parameter is set to "cachedump. Memcache: The getExtendedStats method is used to obtain the running system statistics of all processes in the process pool.
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
Print_r ($ memcache-> getStats ());
/**
* Array
*(
* [Pid] = & gt; 8052
* [Uptime] = & gt; 9205
* [Time] = & gt; 1205898428
* [Version] => 1.2.5
* [Pointer_size] => 32
* [Rusage_user] = & gt; 0.008000
* [Rusage_system] = & gt; 0.000000
* [Curr_items] => 1
* [Total_items] => 17
* [Bytes] => 57
* [Curr_connections] => 2
* [Total_connections] => 15
* [Connection_structures] => 3
* [Cmd_get] => 9
* [Cmd_set] => 23
* [Get_hits] => 5
* [Get_misses] => 4
* [Evictions] => 0
* [Bytes_read] = & gt; 671
* [Bytes_written] = & gt; 850
* [Limit_maxbytes] = & gt; 10485760
* [Threads] => 1
*)
*/
?>
Memcache: The connect method is used to create a Memcache object. Memcache: The pconnect method is used to create a persistent connection object for Memcache. Memcache: The close method is used to close a Memcache object.
Memcache: The set method adds a value. The Memcache: set method has four parameters: the first parameter is the key, the second parameter is the value, and the third parameter is optional, indicates whether to compress and save data. The fourth parameter is optional. it is used to set a time when automatic destruction is expired. The function of the Memcache: add method is similar to that of the Memcache: set method. The difference is that if the return value of the Memcache: add method is false, the key already exists, and Memcache :: the set method is overwritten directly. The Memcache: get method is used to obtain a key value. The Memcache: get method has a parameter that indicates the key. The Memcache: replace method is used to override an existing key. the Memcache: replace method has four parameters, which serve the same purpose as the Memcache: set method. The Memcache: delete method is used to delete a key value. The Memcache: delete method has two parameters. The first parameter indicates the key, and the second parameter is optional, indicating the deletion delay.
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
$ Memcache-> set ('name', 'Leo ', 0, 30 );
If (! $ Memcache-> add ('name', 'Susan ', 0, 30 )){
Echo 'Susan is exist ';
}
$ Memcache-> replace ('name', 'on', 0,300 );
Echo $ memcache-> get ('name ');
$ Memcache-> delete ('name', 5 );
?>
The memcache_debug () function is used to control the debugging function, provided that the-enable-debug option is used during php compilation. Otherwise, this function will not work.
The Memcache: addServer method adds an available server address. the Memcache: addServer method has eight parameters, except for the first parameter, which are optional, the first parameter indicates the server address, the second parameter indicates the port, the third parameter indicates whether the connection is persistent, and the fourth parameter indicates the weight of the server on all servers, the fifth parameter indicates the connection duration, and the sixth parameter indicates the connection retry interval. the default value is 15 and the value-1 indicates no retry, the seventh parameter is used to control the online status of the server. The seventh parameter allows you to set a return function to handle error messages.
The Memcache: setServerParams method modifies server parameters at runtime. the Memcache: setServerParams method has six parameters. the Memcache: addServer method has fewer parameters than the third and fourth parameters. Memcache: The getServerStatus method is used to obtain the parameters of the running server. The two parameters indicate the address and port respectively.
Function _ callback_memcache_failure ($ host, $ port ){
Print "memcache '$ host: $ port' failed ";
}
$ Memcache = new Memcache;
$ Memcache-> addServer ('192. 168.1.116', 192 );
$ Memcache-> setServerParams ('192. 168.1.116', 192, 1, 15, true, '_ callback_memcache_failure ');
Echo $ memcache-> getServerStatus ('192. 168.1.116', 192 );
?>
Memcache: The flush method is used to clear all cached data, but does not reduce the memory space used.
The Memcache: increment method is used to add the value of a saved key. the Memcache: decremen method is used to subtract the value of a saved key.
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211 );
$ Memcache-> set ('test _ item', 8 );
$ Memcache-> increment ('test _ item', 4 );
Echo $ memcache-> decrement ('test _ item', 7 );
// Display 5
?>
The setCompressThreshold method compresses data larger than a certain size. The setCompressThreshold method has two parameters. The first parameter indicates the critical point for processing data size, and the second parameter indicates the compression ratio. the default value is 0.2.
$ Memcache = new Memcache;
$ Memcache-> addServer ('memcache _ host', 11211 );
$ Memcache-> setCompressThreshold (20000, 0.2 );
?>
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.