How to Use Memcache in PHP

Source: Internet
Author: User
Use of Memcache in PHP

Use of Memcache in PHP

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
The Code is as follows:
$ 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 actual applications, the database query result set is usually saved to memcached.
The next access will be obtained directly from memcached instead of performing database query operations, which can greatly reduce the burden on the database.
Generally, the value after the md5 () of the SQL statement is used as the unique identifier key. Below is an example of using memcached to cache the database query result set.
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); // the md5 SQL command is the unique identifier of memcache.
$ Rows = $ m-> get ($ key); // retrieve data from memcache
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;
}
$ M-> add ($ key, $ rows );
// Here, you can set the cache time and the specific time to write the data obtained from the heavy database. Set the time as needed.
}
Var_dump ($ rows); // print the data
// When the program is run for the first time, the database is read once because no data is cached. When the program is accessed again, the 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.