PHP as a memcached client-specific implementation method _php tutorial

Source: Internet
Author: User
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.

 
 
  1. php
  2. Contains the Memcached class file
  3. Require_once (' memcached-client.php ');
  4. Option settings
  5. $ Options = Array (
  6. ' Servers ' = > Array (' 192.168.1.1:11211′),//memcached service address, port, multiple array elements can be used to represent multiple memcached services
  7. ' Debug ' = > true,//Whether debug is turned on
  8. ' compress_threshold ' = > 10240,//compression of data over how many bytes
  9. ' persistant ' = > false//whether to use persistent connection
  10. );
  11. Creating an Memcached object instance
  12. $ MC = New memcached ($options);
  13. Sets the unique identifier used by this script
  14. $ Key = ' MyKey ' ;
  15. Writing objects to the memcached
  16. $MC- > Add ($key, ' some random strings ');
  17. $ Val = $MC- > get ($key);
  18. echo "n". Str_pad (' $MC->Add () ', 60, ' _ '). " n ";
  19. Var_dump ($val);
  20. Replace a written object data value
  21. $MC- > replace ($key, array (' some ' =>'haha ', ' array ' =>' xxx '));
  22. $ Val = $MC- > get ($key);
  23. echo "n". Str_pad (' $MC->replace () ', 60, ' _ '). " n ";
  24. Var_dump ($val);
  25. Delete an object in memcached
  26. $MC- > Delete ($key);
  27. $ Val = $MC- > get ($key);
  28. echo "n". Str_pad (' $MC->Delete () ', 60, ' _ '). " n ";
  29. Var_dump ($val);
  30. ?>

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):

 
 
  1. php
  2. $ SQL = ' SELECT * from users ' ;
  3. $ Key = MD5 ($sql); Memcached Object Identifiers
  4. if (! ( $datas = $mc->get ($key))) {
  5. If the cached data is not obtained in memcached, the recordset is obtained using a database query.
  6. echo "n". Str_pad (' Read datas from MySQL. ', 60, ' _ '). " n ";
  7. $ Conn = mysql_connect (' localhost ', ' test ', ' test ');
  8. mysql_select_db (' Test ');
  9. $ result = mysql_query ($sql);
  10. While ($row = mysql_fetch_object($result))
  11. $datas [] = $row;
  12. Saves the result set data obtained in the database to memcached for use on the next visit.
  13. $MC- > Add ($key, $datas);
  14. } else {
  15. echo "n". Str_pad (' Read datas from memcached. ', 60, ' _ '). " n ";
  16. }
  17. Var_dump ($datas);
  18. ?>

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.