Add ($key, $value, $expiry);
$key: Unique identity, used to differentiate written data
$value: Data to write
$expiry: Expiration time, default is always valid
Purpose: Writes data to Memcache
Get ($key)
$key: Get the corresponding data by $key at the time of writing
Purpose: Get the data in Memcache
Replace ($key, $value, $expiry)
The method parameter is the same as the parameter of the Add method
The use is also very obvious is to replace the data
Delete ($key, $time = 0)
$key: Uniquely identifying
$time: Delay Time
Purpose: Delete data stored in the Memcache
Here's a look at the specific usage:
Add ($key, $value, $expiry);
$key: Unique identity, used to differentiate written data
$value: Data to write
$expiry: Expiration time, default is always valid
Purpose: Writes data to Memcache
Get ($key)
$key: Get the corresponding data by $key at the time of writing
Purpose: Get the data in Memcache
Replace ($key, $value, $expiry)
The method parameter is the same as the parameter of the Add method
The use is also very obvious is to replace the data
Delete ($key, $time = 0)
$key: Uniquely identifying
$time: Delay Time
Purpose: Delete data stored in the Memcache
Here's a look at the specific usage:
Code
Copy CodeThe 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 '); The replacement content is Dataecho $m->get (' MyKey ');//Output data
$m->delete (' MyKey '); Remove echo $m->get (' MyKey '); Output false because it has been deleted oh.
?>
is not very simple. In practical applications, the result set of a database query is usually saved to memcached
The next visit is taken directly from the memcached, and the database query operation is not done, which can greatly reduce the burden on the database.
Typically, the value after the SQL statement MD5 () is used as the unique identifier key. Below is an example of using memcached to cache a database query result set
Code
Copy CodeThe code is as follows:
Connection Memcache
$m = new Memcache ();
$m->connect (' localhost ', 11211);
I'm not going to write a connection to the database.
$sql = ' SELECT * from users ';
$key = MD5 ($sql); MD5 SQL command as a unique identifier for memcache
$rows = $m->get ($key); First re-memcache to get the data
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);
Here to write the data obtained in the database, you can set the cache time, the specific time to set how much, according to their own needs.
}
Var_dump ($rows); Print out data
The first time the program was run, because the data has not been cached, so it will be read once the database, when the program is accessed again, the direct re-memcache obtained.
?>
http://www.bkjia.com/PHPjc/321119.html www.bkjia.com true http://www.bkjia.com/PHPjc/321119.html techarticle Add ($key, $value, $expiry); $key: Unique identifier used to differentiate the data being written $value: data to write $expiry: Expiration time, default is always valid: Write data to Memcach ...