Home  >  Article  >  Backend Development  >  Use of Memcahe in PHP_PHP Tutorial

Use of Memcahe in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:46:471143browse

Memcache is a high-performance distributed memory object caching system. By maintaining a unified huge hash table in the memory, it can be used to store data in various formats, including images, videos, files, and database retrieval results. . Simply put, the data is called into the memory and then read from the memory, thus greatly improving the reading speed. (Selected from Baidu Encyclopedia)

Memcache can be used in two ways: process-oriented and object-oriented in PHP. Here we mainly describe the use of object-oriented. For details, please refer to the PHP manual.

Memcache interface

​ Object-oriented interface:

​ ​ 1. Memcache::connect: Open a Memcache connection, for example:

          $mem=new Memcache; //Instantiate an object

$mem->connect('localhost',11211); //Open the connection, the first parameter is: host address, which can be domain name, IP address; the second parameter is: port

?>

​ ​ 2. Memcache::pconnect: Open a persistent connection to Memcache, the function and use are the same as above

​ ​ 3. Memcache::close: Close a Memcache connection and release resources

​ ​ 4. Memcache::add: Add a key and value to the Memcache hash table. There are four parameters

         bool Memcache::add ( string key, mixed var [, int flag [, int expire]] )

                    1) string key: key name

                                   2) mixed var: the value of the key, which can be of any type (string, number, array, object)

​​​​​​​ 3) int flag: optional parameter, generally MEMCACHE_COMPRESSED is used to indicate the compression method

                                                         int expire: optional parameter, set time, in seconds

$mem->add('mystr','This is a string!',MEMCACHE_COMPRESSED,3600);

​ ​ 5. Memcache::set: Save data to the Memcache server. Its usage is similar to add, but Memcache::set can change the value of the key

​​​​ 6. Memcache::get: Extract a data saved on the Memcache server, with only one parameter: key name. For example: To extract the value of the key mystr in the Memcache server

echo $mem->get('mystr');

​​​​ 7. Memcache::delete: Delete a key in the Memcache server. There is only one parameter: key name

$mem->delete('mystr');

​​​​ 8. Memcache::flush: Delete all key values ​​stored in the Memcache server, no parameters

​ ​ 9. Memcache::getStats: Get the current running status of the Memcache server, no parameters

​ ​ 10. Memcache::getVersion: Get the version of the current Memcache server, no parameters

​​​ 11. Memcache::replace: Replace the value of the key that already exists in the Memcache server. The usage method is similar to add/set

​​​ 12. Memcache::addServer: Add multiple Memcache servers. The usage method is similar to Memcache::connect

It should be noted that if you use Memcache::add twice or more to assign a value to the same key, the value of the key will not be overwritten. Only Memcahe::replace or Memcache::set can be used.

Excerpted from Lee.’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478550.htmlTechArticleMemcache is a high-performance distributed memory object cache system that maintains a unified huge object in memory. Hash table, which can be used to store data in various formats, including images...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn