PHP connection Memcache program code

Source: Internet
Author: User
Tags website performance
Memcache is a high-performance distributed memory object cache system commonly used in php. we can use it to improve website performance and reduce server load, the following is my learning notebook. let's take a look at the simplest connection method.

Memcache is a high-performance distributed memory object cache system commonly used in php. we can use it to improve website performance and reduce server load, the following is my learning notebook. let's take a look at the simplest connection method.

The sample code is as follows:

  1. $ Mem = new Memcache;
  2. $ Mem-> connect ('127. 0.0.1 ', 127) or die ("cocould not connect ");
  3. $ Mem-> set ('key', 'This is a test! ', 0, 60 );
  4. $ Val = $ mem-> get ('key ');
  5. Echo $ val;
  6. ?>

The application will create a class, the code is as follows:

  1. Class MCache
  2. {
  3.  
  4. Private static $ server = array ('2017. 0.0.1 ', 127 );
  5. Private static $ client = 'memcached'; // memcache or memcached
  6.  
  7. Private static function get_memcache ()
  8. {
  9. Static $ memcache;
  10. If (! Is_object ($ memcache ))
  11. {Www.111cn.net
  12. $ Memcache = self: $ client = 'memcached '? New Memcached (): new Memcache;
  13. $ Memcache-> addServer (self: $ server [0], self: $ server [1]);
  14. }
  15. Return $ memcache;
  16. }
  17.  
  18. Public static function set ($ key, $ value, $ expire = 600)
  19. {
  20. Self: $ client === 'memcached '? Self: get_memcache ()-> set ($ key, $ value, $ expire)
  21. : Self: get_memcache ()-> set ($ key, $ value, MEMCACHE_COMPRESSED, $ expire );
  22. }
  23.  
  24. Public static function get ($ key)
  25. {
  26. Return self: get_memcache ()-> get ($ key );
  27. }
  28.  
  29. }
  30. MCache: set ('test _ one', 'value _ one', 60); #60 Sec
  31. Print_r (MCache: get ('test _ one '));

The list of all memcache functions is as follows:

Memcache: add-add a value. if it already exists, false is returned.

Memcache: addServer-add an available server address

Memcache: close-close a Memcache object

Memcache: connect-create a Memcache object

Memcache_debug-control debugging function

Memcache: decrement-deletes the value of a saved key.

Memcache: delete-delete a key value

Memcache: flush-clear all cached data

Memcache: get-get a key value

Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.

Memcache: getServerStatus-get the parameters of the running server

Memcache: getStats-return some running statistics of the server

Memcache: getVersion-returns the version information of the running Memcache.

Memcache: increment-adds the value of a saved key.

Memcache: pconnect-creates a persistent connection object for Memcache.

Memcache: replace-overwrite an existing key

Memcache: set-add a value. if it already exists, overwrite it.

Memcache: setCompressThreshold-compresses data larger than a certain size

Memcache: setServerParams-modify server parameters at runtime

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.