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...
?>
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.
?>