Configure and analyze the memcached + PhP Environment

Source: Internet
Author: User
Tags php memcached

Version: memcached-1.2.1-win32.zip

Introduction:

1. Introduction to memcached

On many occasions, we will hear the name memcached, but many of you have heard of it, and have never used it or actually understood it. I only know that it is a good stuff. Memcached is an efficient and fast distributed memory object cache system, mainly used to accelerate Dynamic Web applications.

Working principle:

How memcached works

First, memcached runs on one or more servers as a daemon and accepts client connection operations at any time. The client can be written in various languages, currently, known client APIs include Perl, PHP, Python, Ruby, Java, C #, and C. After the PHP client establishes a connection with the memcached service, the next thing is to access the object. Each accessed object has a unique identifier key, and the access operation is performed through this key, objects saved to memcached are actually stored in the memory, not in the cache file. This is why memcached is so efficient and fast. Note that these objects are not persistent. After the service is stopped, the data in the objects will be lost.

How PHP acts as a memcached Client
You can use PHP as the memcached client to call the memcached service for object access. First, PHP has an extension called memcache, which must be included during compilation in Linux.-Enable-memcache [= dir]Option. In the window, remove the comments in front of php_memcache.dll in PHP. 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.

Iv. php memcached application example
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 method is 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
// Contains the memcached class file
Require_once ('memcached-client. php ');
// Option settings
$ Options = array (
'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.
'Debug' => true, // whether to enable debug
'Compress _ threshold '=> 10240, // when the number of bytes of data exceeds
'Persistant' => false // whether to use persistent connection
);
// Create a memcached object instance
$ MC = new memcached ($ options );
// Set the unique identifier used by this script
$ Key = 'mykey ';
// Write an object to memcached
$ Mc-> Add ($ key, 'some random string ');
$ Val = $ Mc-> get ($ key );
Echo "N". str_pad ('$ Mc-> Add ()', 60, '_'). "N ";
Var_dump ($ Val );
// Replace the written object data value
$ Mc-> Replace ($ key, array ('some' => 'hahaha', '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 );
?> Is it very easy? In actual applications, the database query result set is usually saved to memcached. The result set 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): <? PHP
$ SQL = 'select * From users ';
$ Key = MD5 ($ SQL); // memcached object identifier
{
// If no cached data is obtained in memcached, use database query to obtain the record set.
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;
// Save the result set data obtained from the database to memcached for the next access.
$ Mc-> Add ($ key, $ datas );
{
Echo "N". str_pad ('read datas from memcached. ', 60,' _ '). "N ";
}
Var_dump ($ datas );
?>

It can be seen that after memcached is used, the database connection and query operations can be reduced, the database load is reduced, and the script running speed is also improved. I have previously written an article titled "php achieves multi-server sharing session data". The session in this article is saved in a database. When the concurrency traffic is high, the load on the server is very large and often exceeds the maximum number of connections in MySQL. Using memcached, we can solve this problem well. The working principle is as follows:

  • When a user accesses the webpage, check whether the current user's session data exists in memcached and use session_id () as the unique identifier. If the data exists, the system returns the result directly. If the data does not exist, the system connects to the database, obtain the session data and save the data to memcached for the next use;
  • When the current PHP running ends (or session_write_close () is used), The my_sess: Write () method is called to write data to the database. In this way, database operations are still performed each time, this method also needs to be optimized. Use a global variable to record the session data when the user enters the page, and then compare the data with the session data to be written in the write () method, different databases are connected and written to the database. At the same time, the corresponding objects in memcached are deleted. If they are the same, the session data is not changed, so no operation can be performed and the data is returned directly;
  • So how can we solve the user's session expiration time? Do you remember that the add () method of memcached has an expiration time parameter $ exp? Set this parameter to a value smaller than the maximum session survival time. In addition, do not forget to extend the session duration for those users who have been online. This can be solved in the write () method. By judging the time, the database data will be updated if the conditions are met.

Install and test memcache on Windows
Download memcached for Win32: http://jehiah.cz/projects/memcached-win32/
This page not only supports downloading, but also provides simple and detailed installation steps.
Server installation:
1. decompress the Binary Package to your desired directory (for example, C: memcached)
2. run Windows cmd to install the service. Enter C:/memcached/memcached.exe-D install in the command line.
3. Run the command line to start the memcached service C:/memcached/memcached.exe-d start.
4. Use the default port 11211 listener.
Client installation:
After completing the above steps, you should add memcache extension to PhP.
1. Copy php_memcache.dll from pecl-5.2.3-win32.zip to the ext directory of the PHP installation directory.
2. Add extended extension = php_memcache.dll to PhP. ini.
3. Restart the Apache server
4. Test memcache
Create a memcache. php file in the website directory. The Code is as follows:
<? PHP
$ Memcache = memcache_connect ('localhost', 11211 );
If ($ memcache ){
$ Memcache-> set ("str_key", "string to store in memcached ");
$ Memcache-> set ("num_key", 123 );
$ Object = new stdclass;
$ Object-> attribute = 'test ';
$ Memcache-> set ("obj_key", $ object );
$ Array = array ('assoc '=> 123,345,567 );
$ Memcache-> set ("arr_key", $ array );
Var_dump ($ memcache-> get ('str _ key '));
Var_dump ($ memcache-> get ('num _ key '));
Var_dump ($ memcache-> get ('obj _ key '));
}
Else {
Echo "connection to memcached failed ";
}
?>
If the following content is output normally, the installation is successful.
String (28) "string to store in memcached" string (3) "123" Object (stdclass) #3 (1) {["attribute"] => string (4) "test "}
Otherwise, check whether php_memcache.dll exists in the ext directory. Remember to restart Apache.
Run memcache.exe-h to view the help information. Some Common commands are as follows:

  • Install: X:/memcached/memcached.exe-D install
  • Start: X:/memcached/memcached.exe-d start
  • Stop: X:/memcached/memcached.exe-d stop
  • Restart: X:/memcached/memcached.exe-D restart
  • Help: X:/memcached/memcached.exe-H
  • Basic settings of memcached:
    -P listening port
    -L connected IP address. The default value is local
    -D start: Start the memcached service.
    -D restart: restart the memcached service.
    -D stop | Shutdown the running memcached Service
    -D install the memcached Service
    -D uninstall memcached Service
    -U runs as the identity (only valid when running as root)
    -MB maximum memory usage, in MB. The default value is 64 MB.
    -An error is returned when M memory is used up, instead of deleting items.
    -C: Maximum number of simultaneous connections. The default value is 1024.
    -F block size growth factor, default value: 1.25
    -N: minimum allocation space. The default value of key + value + flags is 48.
    -H Show Help

    Memcache method list:

    • Memcache: Add-add an item to the server
    • Memcache: addserver-Add a memcached server to connection pool
    • Memcache: Close-close memcached server connection
    • Memcache: connect-open memcached server connection
    • Memcache_debug-turn debug output on/off
    • Memcache: decrement-Decrement item's Value
    • Memcache: delete-delete item from the server
    • Memcache: flush-flush all existing items at the server
    • Memcache: Get-retrieve item from the server
    • Memcache: getextendedstats-get statistics from all servers in Pool
    • Memcache: getserverstatus-returns server status
    • Memcache: getstats-get statistics of the server
    • Memcache: getversion-return version of the server
    • Memcache: Increment-increment item's Value
    • Memcache: pconnect-open memcached server persistent connection
    • Memcache: replace-replace value of the existing item
    • Memcache: set-store data at the server
    • Memcache: setcompressthreshold-enable automatic compression of large values
    • Memcache: setserverparams-changes server parameters and status at runtime

    After the client establishes a connection with the memcached service, it performs Object Access Operations. Each accessed object has a unique identifier key, which is used for access operations, objects saved to memcached are actually stored in the memory, not in the cache file. This is why memcached is so efficient and fast. Note that these objects are not persistent. After the service is stopped, the data in them will be lost.
    Usage
    Case 1:
    Failed to capture one thousand rows of records returned from the database using the add method. When limit limits the number of returned results from the database, it can work again.
    Check the memchache documentation and find that memcache has a limit on the size of key and value.
    Key: less than 250 characters. Value: The value is smaller than 1 MB.

    Original article: http://www.socialtext.net/memcached/index.cgi? FAQ # how_do_ I _access_memcached

    References:
    If you have any questions about memcached, refer to the following articles:
    Install memcache in Linux: http://www.ccvita.com/257.html
    Windows memcache installation: http://www.ccvita.com/258.html
    Memcache basic Tutorial: http://www.ccvita.com/259.html
    Discuz! Memcache cache implementation: http://www.ccvita.com/261.html
    Memcache protocol (http://www.ccvita.com/306.html)
    ========================================================== ==============
    Example:

    <? PHP
    // Connection
    $ Mem = new memcache;
    $ Mem-> connect ("192.168.0.200", 12000 );
    // Save data
    $ Mem-> set ('key1', 'this is first value', 0, 60 );
    $ Val = $ mem-> get ('key1 ');
    Echo "Get key1 value:". $ Val. "<br/> ";
    // Replace Data
    $ Mem-> Replace ('key1', 'this is replace value', 0, 60 );
    $ Val = $ mem-> get ('key1 ');
    Echo "Get key1 value:". $ Val. "<br/> ";
    // Save the Array
    $ Arr = array ('aaa', 'bbb ', 'ccc', 'ddd ');
    $ Mem-> set ('key2', $ arr, 0, 60 );
    $ Val2 = $ mem-> get ('key2 ');
    Echo "Get key2 value :";
    Print_r ($ val2 );
    Echo "<br/> ";
    // Delete data
    $ Mem-> Delete ('key1 ');
    $ Val = $ mem-> get ('key1 ');
    Echo "Get key1 value:". $ Val. "<br/> ";
    // Clear all data
    $ Mem-> flush ();
    $ Val2 = $ mem-> get ('key2 ');
    Echo "Get key2 value :";
    Print_r ($ val2 );
    Echo "<br/> ";
    // Close the connection
    $ Mem-> close ();
    ?>

    If it is normal, the browser will output:
    Get key1 value: This is first value
    Get key1 value: This is replace value
    Get key2 value: array ([0] => AAA [1] => BBB [2] => CCC [3] => DDD)
    Get key1 value:
    Get key2 value:

    Use of memcache
    Memcache websites generally have a large traffic volume. To relieve the pressure on the database, apsaradb for memcache is used as a cache area and some information is stored in the memory, allowing quick access at the front end. The general focus is to focus on how to share the pressure on the database and distribute it. After all, the memory capacity of a single memcache is limited. I would like to put forward my personal opinion here. Without practice, you can use it as a reference.

    Distributed Application
    Apsaradb for memcache originally supports distributed architectures. Our client has been slightly transformed to provide better support. Our keys can be properly encapsulated. For example, for a user-based website, each user has a user ID, which can be extracted and accessed based on a fixed ID, for example, users starting with 1 are saved on the first memcache server, and user data starting with 2 is stored on the second child mecache server, data access is converted and accessed according to the user ID.

    However, this disadvantage is that you need to determine the user ID. if the business is inconsistent or other types of applications are not suitable, you can consider it based on your actual business, or think about a more appropriate method.

    Reduce database pressure
    This is important. All the data is basically stored in the database. Each time the database is accessed frequently, the database performance is greatly reduced and it cannot serve more users at the same time, for example, for MySQL, tables with frequent locks, let memcache share the pressure on the database. We need a method that is relatively small and will not change the front-end on a large scale to change the current architecture.

    A simple method I want to consider:
    The back-end database operation module extracts all select operations (update/delete/insert ), then, calculate the corresponding SQL statement using the corresponding hash algorithm to obtain a hash data key (such as MD5 or Sha), and then query the key in memcache for data. If the data does not exist, it indicates that the data has not been written into the cache, so the data is extracted from the database. One is the array format, and the data is then set to memcache. The key is the hash value of this SQL statement, then set an expiration time, for example, one hour. The data in one hour is extracted from the cache, effectively reducing the pressure on the database. The disadvantage is that the data is not real-time. After the data is modified, it cannot be displayed on the front-end in real time, and memory usage may be high. After all, the number of data in each select operation may be large, this is a factor to consider.

    Memcache Security
    The above memcache servers are directly connected through the client and there is no verification process. In this way, it is dangerous to directly expose the server to the Internet, if data leaks are viewed by other unrelated personnel, the server is infiltrated because mecache runs with the root permission. Besides, some unknown bugs or buffer overflow may exist, these are all unknown, so the danger is foreseeable. For the sake of security, I have made two suggestions to slightly prevent hacker intrusion or data leakage.

    Intranet access
    It is recommended that the access between the two servers is in the Intranet format, generally between the Web server and the memcache server. Generally, the server has two NICs, one pointing to the Internet and the other pointing to the Intranet, so that the Web server can access the memcache server through the Intranet Nic, when the memcache server is started, it listens to the Intranet IP address and port, and the access between the Intranet can effectively prevent other illegal access.
    # Memcached-D-M 1024-u root-l 192.168.0.200-P 11211-C 1024-P/tmp/memcached. PID
    The memcache server sets listening to port 11211 of the IP address 192.168.0.200 over the Intranet, occupying 1024 MB of memory and allowing a maximum of concurrent connections.

    Set firewall
    Firewall is a simple and effective method. If both servers are connected to the Internet and memcache needs to be accessed through an Internet IP address, you can use a firewall or proxy program to filter out illegal access.
    In Linux, we can use iptables or FreeBSD ipfw to specify rules to prevent unauthorized access. For example, we can set to allow only our web servers to access our memcache server, at the same time, other accesses are blocked.
    # Iptables-F
    # Iptables-P input drop
    # Iptables-A input-p tcp-s 192.168.0.2-dport 11211-J accept
    # Iptables-A input-p udp-s 192.168.0.2-dport 11211-J accept
    The above iptables rule only allows access from the Web server 192.168.0.2 to the memcache server. It can effectively prevent some illegal access and add other rules to enhance security, this can be done according to your own needs.

    This article is sorted and collected, the main source is: http://blog.csdn.net/heiyeshuwu/archive/2006/11/13/1380838.aspx
    ========================================================== ==============

    Command Line to view the memcached running status

    First, log on to the server, and then type telnet 127.0.0.1 11211 In the CMD command line.
    127.0.0.1 is the server address (here it is the local machine), and 11211 is the port number bound to memcached.
    Then the command line window is black, only the cursor prompts, touch the black and enter stats to get the parameter describing the running status of the memcached server. For example:

    Here, uptime is the number of seconds for memcached to run, and pai_get is the number of times the cache is queried. When the two data are separated, the average number of requests cached per second can be obtained. Recently, the niupu traffic is very low, so the average number of requests is more than once per second. This is a great deal of pressure, it's okay to use the file system cache. it doesn't reflect the advantages of using memcached.

    The interval _set below is the number of times key => value is set. The whole memcached is a large hash. If you use the content not found by using mongo_get, you will call mongo_set to write it into the cache. Followed by get_hits, which is the number of cache hits. Cache hit rate = get_hits/pai_get * 100%.
    The following get_misses number plus get_hits should be equal to pai_get. Total_itemscurr_items indicates the number of key-value pairs in the cache. total_items = pai_set = get_misses is shown in the figure. However, when the maximum available memory is used up, memcached deletes some content, the equation above is not true.

    In other words, it would be great for memcached to have a complete set of monitoring tools. For more information about installing memcached and configuring PHP, see here.

    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.