PHP is used as the memcached client.

Source: Internet
Author: User

Through the introduction in previous articles, we can know the specific working principle of memcached. So, today we will show you how to implementYou can use PHP as the memcached client to call the memcached 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: 100'), // address and port of the memcached service. Multiple array elements can be used to represent multiple memcached services.
  7. 'Debug' =>True, // whether to enable debug
  8. 'Compress _ threshold '=>10240, // how many bytes of data are compressed
  9. 'Persistant' =>False // whether to use persistent connection
  10. );
  11. // Create a memcached object instance
  12. $Mc=NewMemcached ($ options );
  13. // Set the unique identifier used by this script
  14. $Key='Mykey';
  15. // Write an object to memcached
  16. $ Mc->Add ($ key, 'some random string ');
  17. $Val= $ Mc->Get ($ key );
  18. Echo "n". str_pad ('$ mc->Add () ', 60,' _ '). "n ";
  19. Var_dump ($ val );
  20. // Replace the written object data value
  21. $ Mc->Replace ($ key, array ('some' =>'Hahaha', '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 method is used as the memcached client. PHP has an extension called memcache. during compilation in Linux, the "-enable-memcache [= DIR]" option is required, and the "Window" method is in php. remove the annotator in front of php_memcache.dll in ini to make it available.

In addition, there is also a way to avoid the trouble of extension and re-compilation, that is, directly using php-memcached-client.

The second method is used in this article. Although the efficiency is slightly lower than that of the extended library, it is not a problem.

First download the memcached-client.php, after downloading the memcached-client.php, you can operate the memcached service through the class "memcached" in this file. In fact, code calls are very simple. The main methods used include add (), get (), replace (), and delete (). The methods are described as follows:

Add ($ key, $ val, $ exp = 0)

Write an object to memcached. $ key is the unique identifier of the object. $ val is the data of the written object. $ exp is the expiration time, in seconds. The default value is unlimited;

Get ($ key)

Get object data from memcached and get it through the unique identifier $ key of the object;

Replace ($ key, $ value, $ exp = 0)

Replace $ value with the content of the $ key object in memcached. The parameter works only when the $ key object exists like the add () method;

Delete ($ key, $ time = 0)

Delete an object with the identifier $ key in memcached. $ time is an optional parameter, indicating how long it will take before deletion.

The following is a simple test code that allows you to access the object data with the identifier 'mykey:

PHP is easy to implement as a memcached client. In actual applications, the database query result set is usually saved to memcached, which is obtained directly from memcached during the next visit, instead of performing database query operations, this can greatly reduce the burden on the database. Generally, the value after the md5 () of the SQL statement is used as the unique identifier key. Below is an example of using memcached to cache the database query result set (this code snippet is followed by the above sample code ):

 
 
  1. <?Php 
  2. $SQL='Select * FROM users';
  3. $Key=Md5($ SQL); // memcached object identifier
  4. If (! ($Datas= $ Mc->Get ($ key ))){
  5. // If no cached data is obtained in memcached, use database query to obtain the record set.
  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. // Save the result set data obtained from the database to memcached for the next access.
  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 you can see, after using PHP as the memcached client, you can reduce database connection and query operations, load the database, and speed up the running of scripts.


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.