How to Use memcache in PHP

Source: Internet
Author: User

Add ($ key, $ value, $ expiry );
$ Key: Unique Identifier, used to differentiate written data
$ Value: the data to be written.
$ Expiry: expiration time. The default value is always valid.
Purpose: Write Data to memcache.

Get ($ key)
$ Key: Get the corresponding data through the $ key at write time
Purpose: Get data in memcache

Replace ($ key, $ value, $ expiry)
The parameters of this method are the same as those of the add method.
The purpose is to replace the data.

Delete ($ key, $ time = 0)
$ Key: Unique Identifier
$ Time: Delay Time
Purpose: delete data stored in memcache

Let's take a look at the specific usage:
Add ($ key, $ value, $ expiry );
$ Key: Unique Identifier, used to differentiate written data
$ Value: the data to be written.
$ Expiry: expiration time. The default value is always valid.
Purpose: Write Data to memcache.

Get ($ key)
$ Key: Get the corresponding data through the $ key at write time
Purpose: Get data in memcache

Replace ($ key, $ value, $ expiry)
The parameters of this method are the same as those of the add method.
The purpose is to replace the data.

Delete ($ key, $ time = 0)
$ Key: Unique Identifier
$ Time: Delay Time
Purpose: delete data stored in memcache

Let's take a look at the specific usage:

Code Copy codeThe Code is as follows: <? PHP
$ M = new memcache ();
$ M-> connect ('localhost', 11211 );
$ DATA = 'content'; // data to be cached
$ M-> Add ('mykey', $ data); echo $ M-> get ('mykey'); // output content
$ M-> Replace ('mykey', 'data'); // Replace the content with dataecho $ M-> get ('mykey'); // output data
$ M-> Delete ('mykey'); // Delete echo $ M-> get ('mykey'); // output false because it has been deleted ..
?>

is it very simple .. in practical applications, the database query result set is usually saved to memcached.
the result set is directly obtained from memcached at the next access, instead of performing database query operations, this can greatly reduce the burden on the database.
generally, the value after MD5 () of an SQL statement is used as the unique identifier key. Below is an example of using memcached to cache the database query result set
Code copy Code the code is as follows: // connect to memcache
$ M = new memcache ();
$ M-> connect ('localhost', 11211 );
// I will not write the connection to the database.
$ SQL = 'select * From users';
$ key = MD5 ($ SQL ); // MD5 SQL command is the unique identifier of memcache
$ rows = $ M-> get ($ key); // obtain data from memcache first
If (! $ Rows) {
// If $ rows is false, there is no data, and data is written.
$ res = mysql_query ($ SQL );
$ rows = array ();
while ($ ROW = mysql_fetch_array ($ res) {
$ rows [] = $ row;
}< br> $ M-> Add ($ key, $ rows);
// you can set the cache time by writing the data obtained from the database to the database, set the specific time according to your needs.
}< br> var_dump ($ rows ); // print the data
// when the preceding Program is run for the first time, the database is read because no data is cached, when the program is accessed again, memcache is retrieved again.
?>

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.