Use of Memcahe in PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Memcahe is used in PHP. Memcache is a high-performance distributed memory object cache system. by maintaining a unified and huge hash table in the memory, Memcache can be used to store data in various formats, image Memcache is a high-performance distributed memory object cache system. by maintaining a unified and huge hash table in the memory, it can be used to store data in various formats, including image, video, file, and database retrieval results. Simply put, the data is called to the memory and then read from the memory, which greatly improves the reading speed. (From Baidu Encyclopedia)

Memcache can be used in PHP in process-oriented and object-oriented ways. here we will mainly introduce the use of object-oriented. for details, 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 the host address, which can be a domain name or IP address. The second parameter is Port.

?>

2. Memcache: pconnect: enable a persistent connection to Memcache.

3. Memcache: close: Closes a Memcache connection and releases resources.

4. Memcache: 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: key value, which can be any type (string, number, array, object)

3) int flag: an optional parameter. generally, MEMCACHE_COMPRESSED is used to indicate compression.

4) int expire: an optional parameter. it specifies the time, in seconds.

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

5. Memcache: set: Save data to the Memcache server. the usage is similar to add. However, Memcache: set can change the key value.

6. Memcache: get: Extract data stored on the Memcache server. the parameter is a key name. For example, to extract the value of mystr from the Memcache server

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

7. Memcache: delete a key in the Memcache server. the parameter is the key name.

$ Mem-> delete ('mystr ');

8. Memcache: flush: delete all key values stored on the Memcache server. no parameter is specified.

9. Memcache: getStats: Gets the running status of the current Memcache server. no parameter is set.

10. Memcache: getVersion: get the current version of the Memcache server. no parameter is specified.

11. Memcache: replace the existing key value in the Memcache server. the usage is similar to that of add/set.

12. Memcache: addServer: add multiple Memcache servers. the usage is similar to that of Memcache: connect.

Note that if you use Memcache: add to assign values to the same key twice or multiple times, the value of this key will not be overwritten. only Memcahe: replace or Memcache :: set.

From Lee.'s column

Http://www.bkjia.com/PHPjc/478550.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478550.htmlTechArticleMemcache is a high-performance distributed memory object cache system, by maintaining a unified huge hash table in the memory, it can be used to store various formats of data, including images...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.