Basic instance of memcached cache application in PHP

Source: Internet
Author: User
Memcached is a high-performance distributed memory object cache system for dynamic Web applications to reduce database loads. it caches data and objects in the memory to reduce the number of times the database is read, this improves the speed of dynamic and database-driven websites. 1. add extension... memcached is a high-performance distributed memory object cache system for dynamic Web applications to reduce database loads. it caches data and objects in the memory to reduce the number of times the database is read, this improves the speed of dynamic and database-driven websites.

1. add extension package

Php_memcache.dll

2. add the following code in PHP. INI:

Extension = php_memcache.dll

3. the program code is as follows:

 Connect ("10.18.110.213", 11211) {die ('connection failed! ');} // Add // 1. add a string/* if ($ mem-> set ('key1', "beijing", MEMCACHE_COMPRESSED, 60) {echo 'add OK ';} * // 2. add value/* if ($ mem-> set ('key1', 100, MEMCACHE_COMPRESSED, 60) {echo 'add OK ';} * // 3. add an array // add an array as needed. if you want the serial number to be placed in, // serialize <=> unserialize, you can also use json_encode <=> json_decode $ arr = array ("bj", 'TJ ') as needed '); if ($ mem-> set ('key1', $ arr, MEMCACHE_COMPRESSED, time () + 31*3600*24) {echo 'add array ok99111 ';} // 4. add object/* cl Ass Dog {public $ name; public $ age; public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age ;}}$ dog1 = new Dog ('Puppy ', 50); if ($ mem-> set ('key1', $ dog1, MEMCACHE_COMPRESSED, 60) {echo 'add object OK ';} * // 5. add a null Boolean value/* if ($ mem-> set ('key1', false, MEMCACHE_COMPRESSED, 60) {echo 'add Boolean OK ';} * // 6. put the resource type in. /* $ con = mysql_connect ("127.0.0.1", "root", "root"); if (! $ Con) {die ('database connection failed');} var_dump ($ con); echo"
"; If ($ mem-> set ('key1', $ con, MEMCACHE_COMPRESSED, 60) {echo 'add resource OK ';} * // query $ val = $ mem-> get ('key1'); // modify // replace if ($ mem-> replace ("key11 ", 'Hello', MEMCACHE_COMPRESSED, 60) {echo 'replace OK ';} else {echo 'replace no OK';} // delete echo"
"; If ($ mem-> delete ('key14') {echo 'key14 delete';} else {echo 'key14 nonexistent ';}

This article focuses on basic applications. if you need more in-depth study, we will provide relevant tutorials in the future. you can also go to the manual to learn.

Tutorial address:

Reprinted! But please include the article address ^

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.