PHP MemCached Win Assembly

Source: Internet
Author: User
PHP MemCached Win Installation

1. Most PHP environments currently use memcache versions with no D, this version is earlier and is a native version, developed entirely within the PHP framework. The corresponding memcached with D is built on the basis of libmemcached, so thememcached version is relatively functional.
memcache:http://cn2.php.net/manual/en/book.memcache.php
memcached:http://cn2.php.net/manual/en/book.memcached.php
2.Memcache is a native implementation, supporting OO and non-oo two sets of interfaces coexist. The memcached is using libmemcached, which only supports OO interfaces.
The 3.memcached also has a very good place, that is, flag is not set at the time of operation, but with a unified setoption (). The memcached implements more memcached protocols.
The 4.memcached supports binary Protocol, while Memcache is not supported. This means that the memcached will have higher performance. However, Memcached currently does not support long connections.

?

Memcached is a high-performance distributed memory object caching system that can be used to store a variety of

Format data, including images, videos, files, and database retrieval results.


?

Use Memcached website General traffic is relatively large, in order to alleviate the pressure of the database, let MemcacheD as a cache area, the part of the information stored in memory, the front-end can

?

Fast enough to access it.

a ? memcached server-side installation (it is installed as a system service here)
Win32 Binary:memcached-win32-bin.zip Download Address: http://code.jellycan.com/memcached/

1 Unzip the file to

C:\memcached

2 command Line Input

C:\memcached\memcached.exe-d Install

Note: memcached will start automatically every time a service is turned on for Windows. This way the server side is already installed.
3 command Line Input

C:\memcached\memcached.exe-d start

The command starts Memcached, the default listener port is 11211, and the memcached.exe-h can be used to view its help

?

Test under command line

4. telnet 127.0.0.1 11211?? (Test success with Telnet connection)

5. input: Stats command to view basic information, then enter Netstat-n-A on the command line to view the ports?

Memcache Server Security:

Memcache server is directly through the client connection after direct operation, there is no verification process, so if the server is directly exposed to the Internet is more dangerous, light data leakage by other unrelated personnel view, heavy server was compromised, In addition, there may be some of our unknown bugs or buffer overflow situation, these are unknown to us, so the danger is predictable. For the sake of security, do two suggestions, can be a little to prevent hacking or data leakage.

The problem with modifying the Memcache server configuration now is described below:

1> provides Web application server calls in the form of an intranet IP, and does not allow direct calls through an external network, such as placing the Memcache server on a 192.168.1.55 server

2> Modify the port, such as change to 11200

3> allocating memory, such as allocating 1024M (1G memory)

Here's how:

?

1> Start > Run: CMD (OK)

2>CD C:\memcached (carriage return)

3>memcached-m 1024-p 11200-l 192.168.1.55 (carriage return)

Note that at this point the command line does not return to the c:\memcached> state, and the Memcache server is actually quietly becoming a stop state. This window cannot be closed. Open a new CMD window

4> Start > Run: CMD (OK)

5>CD C:\memcached (carriage return)

6>memcached-d Start (enter) to close this cmd window.

You can now use the newly configured memcache server.

?

Although the above method resolves the problem of modifying the default configuration, there will always be a CMD window that cannot be closed, otherwise it will return to the default configuration of Port 11211.

A better solution is to modify the service's registry configuration by:

1> Start > Run: regedit (enter)

2> found in the registry: hkey_local_machine\system\currentcontrolset\services\memcached Server

3> the value of the default ImagePath key is: "C:\memcached\memcached.exe"-drunservice, instead: "C:\memcached\memcached.exe"-D runservice-m 512-p?11200-l 192.168.1.55 (OK, close the registry)

4> My Computer (right-click) > Management > Services to find memcache services, reboot once to take effect.

At this point, the same network segment of the computer can still use this memcache server, we limit the specified Web application server to be able to use, through the firewall way. If only allow 192.168.1.2 this Web server to the Memcache server access, can effectively prevent some illegal access, the corresponding can also add some other rules to enhance security, which can be done according to their own needs.

?

Configuring MEMCACHD Client side in PHP two

Here we need php_memcache.dll to connect memcached server

PHP membercached Extension Download address: http://downloads.php.net/pierre/

Method 1?

1. Download: Php_memcache.dll Components (Wamp comes with it) please find the corresponding PHP version of the file yourself

2. Copy Php_memcache.dll to the specified component directory, Default in Php\ext\

3, add a sentence in php.ini: Extension=php_memcache.dll?

4. Restart the Web service, using phpinfo () function View enabled if there is memcache, then the installation is successful!

PHP test

 
  Addserver (' 127.0.0.1 ', 11211) or Die ("Could not connect Memcached server!");  $memcache->set (' Memcached ', "Hello memcached!
") echo $memcache->get (' Memcached ');? >
?
 
  Connect (' 127.0.0.1 ', 11211) or Die ("Could not connect Memcached server!");  $memcache->set (' memcache ', "Hello memcache!
") echo $memcache->get (' memcache ');? >

Results

Congratulations, the configuration is successful, now start slowly! cannot run see if the configuration is successful, or check if the lower port is open

?

Method Two

You can avoid the hassle of scaling and recompiling by using php-memcached-client directly.

Although the efficiency is slightly less than the expansion of the library some , but the problem is not big.
Download memcached-client.php First, 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 way to use is add (), get (), replace () and delete (), the method is described as follows:
Add ($key, $val, $exp = 0)
to write to Memcached object, $key object A unique identifier, $val is the object data written to, $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)
replaces the object contents of an identifier $key in memcached with a $value, as with the Add () method, which only works if the $key object exists;
Delete ($key, $ti me = 0)
Deletes an object with an identifier $key in memcached, $time as an optional parameter, indicating how long to wait before deleting. The
below is a simple test code that accesses object data with identifiers of ' MyKey ':

 array (' 192.168.1.1:11211 '),//memcached service address, port, multiple array elements can be used to represent multiple memcached services ' Debug ' = + true,// Whether to turn on debug ' compress_threshold ' = 10240,//More than how many bytes of data to compress ' persistant ' = false//whether to use persistent connection;//Create Mem Cached object Instance $MC = new memcached ($options);//Set the unique identifier used by this script $key = ' mykey ';//write Object memcached to $mc->add ($key, ' some RA Ndom strings '); $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 ' = ' haha ', ' array ' = ' xxx ')); $val = $MC Get ($key), echo "n". Str_pad (' $MC->replace () ', 60, ' _ '). " n "; Var_dump ($val);//Delete Object $mc->delete ($key) in memcached; $val = $MC->get ($key); echo" n ". Str_pad (' $MC->delete () ', 60, ' _ '). " n "; Var_dump ($val);? 

? is not very simple, in the actual application, the result set of the database query is usually saved to memcached, the next time access is obtained directly from the memcached, and no longer do the database query operation, which can greatly reduce the burden of the database. 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):

 
  Pconnect (' 127.0.0.1 ', 11211);  $key = MD5 ($sql);   Memcached object identifier if (! ( $datas = $MC->get ($key))) {    //   The cache data is not fetched in memcached, then 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 you can see, after using memcached, you can reduce database connection, query operations, database load down, scripts run faster.

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