Php uses memcached cache to add, delete, modify, and query data

Source: Internet
Author: User
Php uses memcached cache to add, delete, modify, and query data

  1. // Create a memcache object instance
  2. $ Memcache = new Memcache;
  3. If (! $ Memcache-> connect ("127.0.0.1", 11211 )){
  4. Die ('connection failed ');
  5. }
  6. If ($ memcache-> set ('key1', "xian", MEMCACHE_COMPRESSED, 60 )){
  7. Echo 'sucess! ';
  8. } // Save the value. The xian string can also be an array and an object, but cannot be a resource.
  9. // Bbs.it-home.org
  10. $ Val = $ memcache-> get ('key1'); // query the obtained value
  11. Echo $ val;
  12. $ Memcache-> replace ('key1', 'Beijing', MEMCACHE_COMPRESSED, 60); // modify
  13. $ Memcache-> delete ('key1'); // delete
  14. ?>

Part 2: php memcached advanced cache application code

PHP MemCached advanced cache application

Common Memcache methods:

  1. $ Memcache = new memcache;
  2. $ Memcache-> connect ('127. 0.0.1 ', 127) or die ("connection failed ");
  3. $ Memcache-> set ('name', 'Zhang San ');
  4. $ Val = $ memcache-> get ('name ');
  5. ?>

Note: The complete version of the set method, set (key name, key value, whether to compress, retention time)

Example:

  1. $ Memcache = new memcache;
  2. $ Memcache-> connect ('127. 0.0.1 ', 127) or die ("connection failed ");
  3. $ Memcache-> set ('name', array ('one', 'two '));
  4. $ Val = $ memcache-> get ('name ');
  5. Print_r ($ val );
  6. $ Memcache-> close ();
  7. ?>

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.