We can know the specific working principle of memcached through the introduction of previous articles. Well, today we're going to show you how to achieveThere are two ways to make PHP a memcached client, invoking Memcached's service for object access.
- php
- Contains the Memcached class file
- Require_once (' memcached-client.php ');
- Option settings
- $ Options = Array (
- ' Servers ' = > Array (' 192.168.1.1:11211′),//memcached service address, port, multiple array elements can be used to represent multiple memcached services
- ' Debug ' = > true,//Whether debug is turned on
- ' compress_threshold ' = > 10240,//compression of data over how many bytes
- ' persistant ' = > false//whether to use persistent connection
- );
- Creating an Memcached object instance
- $ MC = New memcached ($options);
- Sets the unique identifier used by this script
- $ Key = ' MyKey ' ;
- Writing objects to the memcached
- $MC- > Add ($key, ' some random strings ');
- $ Val = $MC- > get ($key);
- echo "n". Str_pad (' $MC->Add () ', 60, ' _ '). " n ";
- Var_dump ($val);
- Replace a written object data value
- $MC- > replace ($key, array (' some ' =>'haha ', ' array ' =>' xxx '));
- $ Val = $MC- > get ($key);
- echo "n". Str_pad (' $MC->replace () ', 60, ' _ '). " n ";
- Var_dump ($val);
- Delete an object in memcached
- $MC- > Delete ($key);
- $ Val = $MC- > get ($key);
- echo "n". Str_pad (' $MC->Delete () ', 60, ' _ '). " n ";
- Var_dump ($val);
- ?>
The first PHP as a method of memcached client, PHP has a extension called memcache, Linux under the compiler need to take the –enable-memcache[=dir] option, Window under the php.ini to remove Php_ Memcache.dll the annotation character in front of it to make it available.
In addition, there is a way to avoid the problem of scaling and recompiling, which is to use php-memcached-client directly.
This article chooses the second way, although the efficiency will be slightly worse than the expansion library, but the problem is not big.
First download the memcached-client.php, after downloading the memcached-client.php, you can use the class "memcached" in this file to operate the memcached service. In fact, the code call is very simple, the main use of the method has add (), get (), replace () and delete (), the method is described as follows:
Add ($key, $val, $exp = 0)
Writes an object to Memcached, $key is the unique identifier of the object, $val is the object data written, $exp the expiration time, in seconds, the default is unlimited time;
Get ($key)
Gets the object data from the memcached, $key obtained by the object's unique identifier;
Replace ($key, $value, $exp =0)
Use $value to replace object content with an identifier $key in memcached, as with the Add () method, which only works if $key object exists;
Delete ($key, $time = 0)
Deletes an object with an identifier $key in memcached, $time as an optional parameter, indicating how long to wait before deleting.
The following is a simple test code that accesses object data with identifier ' MyKey ' in the code:
is not PHP as a memcached client implementation is very simple, in practice, the result set of the database query is usually saved to memcached, the next time access is directly from the memcached, and no longer do database query operations, This can reduce the burden on the database to a large extent. 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 result set of a database query (the code snippet follows the example code above):
- php
- $ SQL = ' SELECT * from users ' ;
- $ Key = MD5 ($sql); Memcached Object Identifiers
- if (! ( $datas = $mc->get ($key))) {
- If the cached data is not obtained in memcached, the recordset is obtained using a database query.
- echo "n". Str_pad (' Read datas from MySQL. ', 60, ' _ '). " n ";
- $ Conn = mysql_connect (' localhost ', ' test ', ' test ');
- mysql_select_db (' Test ');
- $ result = mysql_query ($sql);
- While ($row = mysql_fetch_object($result))
- $datas [] = $row;
- Saves the result set data obtained in the database to memcached for use on the next visit.
- $MC- > Add ($key, $datas);
- } else {
- echo "n". Str_pad (' Read datas from memcached. ', 60, ' _ '). " n ";
- }
- Var_dump ($datas);
- ?>
As can be seen, the PHP as a memcached client, you can reduce the database connection, query operations, the database load down, the script is also running faster.
http://www.bkjia.com/PHPjc/446355.html www.bkjia.com true http://www.bkjia.com/PHPjc/446355.html techarticle we can know the specific working principle of memcached through the introduction of previous articles. So, today we will show you how to implement there are two ways to make PHP as a memcached guest ...