Brief Introduction to using memcache caching technology in PHP

Source: Internet
Author: User

For more information about how to use memcache cache to accelerate PHP response, see.

Connect to database

The Code is as follows: Copy code

$ Memcache_obj = memcache_connect ("localhost", 11211 );

Memcache_add ($ memcache_obj, 'var _ key', 'test variable', false, 30 );

$ Memcache_obj-> add ('var _ key', 'test variable', false, 30 );


Instance 1

The Code is as follows: Copy code

<? Php
// Connection
$ Mem = new Memcache;
$ Mem-> connect ("127.0.0.1", 11211) or die ("cocould not connect ");
// Display version
$ Version = $ mem-> getVersion ();
Echo "Memcached Server version:". $ version. "<br> ";
// Save data
$ Mem-> set ('key1', 'this is first value', 0, 60 );
$ Val = $ mem-> get ('key1 ′);
Echo "Get key1 value:". $ val. "<br> ";
// Replace Data
$ Mem-> replace ('key1', 'this is replace value', 0, 60 );
$ Val = $ mem-> get ('key1 ′);
Echo "Get key1 value:". $ val. "<br> ";
// Save the Array
$ Arr = array ('aaa', 'bbb ', 'ccc', 'ddd ');
$ Mem-> set ('key2', $ arr, 0, 60 );
$ Val2 = $ mem-> get ('key2 ′);
Echo "Get key2 value:";
Print_r ($ val2 );
Echo "<br> ";
// Delete data
$ Mem-> delete ('key1 ′);
$ Val = $ mem-> get ('key1 ′);
Echo "Get key1 value:". $ val. "<br> ";
// Clear all data
$ Mem-> flush ();
$ Val2 = $ mem-> get ('key2 ′);
Echo "Get key2 value:";
Print_r ($ val2 );
Echo "<br> ";
// Close the connection
$ Mem-> close ();
?>

The Memcache function Library is used in the PECL (PHP Extension Community Library) to build a temporary storage area for large memory data volumes.

In distributed mode, network marketing training is not recommended.

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! [4 F8 C2 e, X
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.