Home >Backend Development >PHP Tutorial >PHP uses memcached cache to add, delete, modify and query data

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

WBOY
WBOYOriginal
2016-07-25 08:52:45931browse
  1. //Create a memcache object instance
  2. $memcache = new Memcache;
  3. if(!$memcache->connect("127.0.0.1",11211)){
  4. die('Connect Failed');
  5. }
  6. if($memcache->set('key1',"xian",MEMCACHE_COMPRESSED,60)){
  7. echo 'sucess!';
  8. }//Save the value, where the xian string is also Can be an array, object, but not a resource
  9. // bbs.it-home.org
  10. $val = $memcache->get('key1');//Query to get the value
  11. echo $val;
  12. $memcache- >replace('key1','beijing',MEMCACHE_COMPRESSED,60);//Modify
  13. $memcache->delete('key1');//Delete
  14. ?>
Copy code

Second Part, php memcached advanced caching application code

PHP MemCached advanced caching application

Memcache common methods:

  1. $memcache = new memcache;
  2. $memcache->connect('127.0.0.1', 11211) or die("Connection failed");
  3. $memcache->set( 'name', 'Zhang San');
  4. $val = $memcache->get('name');
  5. ?>
Copy code

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', 11211) or die("Connection failed");
  3. $memcache -> set( 'name', array('one','two'));
  4. $val = $memcache->get('name');
  5. print_r($val);
  6. $memcache -> close();
  7. ?>
Copy code


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