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 Copy codeThe Code is as follows: <? PHP
$ 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 practical applications, the database query result set is usually saved to memcached.
the result set is directly obtained from memcached at the next access, instead of performing database query operations, this can greatly reduce the burden on the database.
generally, the value after MD5 () of an SQL statement 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 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 ); // MD5 SQL command is the unique identifier of memcache
$ rows = $ M-> get ($ key); // obtain data from memcache first
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;
}< br> $ M-> Add ($ key, $ rows);
// you can set the cache time by writing the data obtained from the database to the database, set the specific time according to your needs.
}< br> var_dump ($ rows ); // print the data
// when the preceding Program is run for the first time, the database is read because no data is cached, when the program is accessed again, memcache is retrieved again.
?>