Home  >  Article  >  Backend Development  >  Tips for using Memcache in PHP_PHP Tutorial

Tips for using Memcache in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:41:45942browse

add($key, $value, $expiry);
$key: unique identifier, used to distinguish written data
$value: data to be written
$expiry: expiration time, default is Valid forever
Purpose: Write data into memcache

get($key)
$key: Get the corresponding data through the $key when writing
Purpose: Get memcache The data

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

delete($ key, $time = 0)
$key: unique identifier
$time: delay time
Purpose: delete the data stored in memcache

Let’s take a look at the specific usage:
add($key, $value, $expiry);
$key: unique identifier, used to distinguish written data
$value: data to be written
$expiry: expiration time, default is Valid forever
Purpose: Write data into memcache

get($key)
$key: Get the corresponding data through the $key when writing
Purpose: Get memcache The data

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

delete($ key, $time = 0)
$key: unique identifier
$time: delay time
Purpose: delete the data stored in memcache

Let’s take a look at the specific usage:

Code

Copy code The code is as follows:

$m = new Memcache() ;
$m->connect('localhost', 11211);
$data = 'content'; //Data that needs to be cached
$m->add('mykey', $data );echo $m->get('mykey'); //Output content
$m->replace('mykey', 'data'); //Replace content with dataecho $m->get ('mykey'); //Output data
$m->delete('mykey'); //Delete echo $m->get('mykey'); //Output false because it has been deleted Oh...
?>



Isn’t it very simple... In practical applications, the result set of the database query is usually saved in memcached
The next time you visit, you can obtain it directly from memcached instead of doing database query operations. This can reduce the burden on the database to a great extent.
Usually the value after the SQL statement md5() 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:

< ;?php
//Connect memcache
$m = new Memcache();
$m->connect('localhost', 11211);
//I won’t write about connecting to the database .
$sql = 'SELECT * FROM users';
$key = md5($sql); //md5 SQL command as the unique identifier of memcache
$rows = $m->get ($key); //Re-memcache to get the data first
if (!$rows) {
//If $rows is false, then there is no data, then write the data
$res = mysql_query ($sql);
$rows = array();
while ($row = mysql_fetch_array($res)) {
$rows[] = $row;
}
$m ->add($key, $rows);
//Write the data obtained from the database here. You can set the cache time. You can set the specific time according to your needs.
}
var_dump ($rows); //Print out the data
//When the program is run for the first time, because the data has not been cached, the database will be read once. When the program is accessed again, it will be obtained directly from memcache.
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321119.htmlTechArticleadd($key, $value, $expiry); $key: unique identifier, used to distinguish writes Data $value: The data to be written $expiry: Expiration time, the default is always valid Purpose: Write data to memcach...
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