Home > Backend Development > PHP Tutorial > memcache Memcache usage tips in PHP

memcache Memcache usage tips in PHP

WBOY
Release: 2016-07-29 08:41:46
Original
1013 people have browsed it

add($key, $value, $expiry);
$key: unique identifier, used to distinguish written data
$value: data to be written
$expiry: expiration time, the default is forever valid
Usage: Data is written to memcache
get($key)
$key: Get the corresponding data through the $key when writing
Purpose: Get the data in memcache
replace($key, $value, $expiry)
This method The parameters are the same as those of the add method
The purpose is also to replace data
delete($key, $time = 0)
$key: unique identifier
$time: delay time
Purpose: delete the data stored in memcache
The following 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 Always valid
Purpose: Write data into memcache
get($key)
$key: Get the corresponding data through the $key when writing
Purpose: Get the data in memcache
replace($key, $value, $expiry)
The parameters of this method are the same as those of the add method.
The purpose is also to replace the data.
delete($key, $time = 0)
$key: unique identifier
$time: delay time
Purpose: delete from memcache Stored data
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...
?>


Isn't it very simple... In practical applications, Usually, the result set of the database query is saved in memcached. The next time you access it, it is obtained directly from memcached instead of doing the database query operation. 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:

//Connect memcache
$m = new Memcache();
$m ->connect('localhost', 11211);
//I will not 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 own needs.
}
var_dump($rows); / /Print out the data
//When the program is run for the first time, because the data has not been cached yet, the database will be read once. When the program is accessed again, it will be obtained directly from memcache.
?>

The above introduces memcache Memcache usage skills in PHP, including memcache content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template