Memcached Client's explanation

Source: Internet
Author: User
Tags php download php framework

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 based on the libmemcached, so the memcached version is relatively functional, and currently only has a Linux version.
memcache:http://cn2.php.net/manual/en/book.memcache.php
memcached:http://cn2.php.net/manual/en/book.memcached.php Download: https://github.com/php-memcached-dev/php-memcached/
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.

However, under window can be used under the Memcache,linux with the memcached, as long as they use the same interface to ensure that:

memcached/memcache::add-Add an element below a new key
memcached/memcache::addserver-Adding a server to the server pool
Memcached/memcache::d ecrement-Decrease the value of a numeric element
Memcached/memcache: Delete an element:d elete-
memcached/memcache::flush-all elements in the cache
memcached/memcache::get-Retrieving a single element
memcached/memcache::getstats-Getting statistics for a server pool
memcached/memcache::getversion-Get version information for all servers in the server pool
memcached/memcache::increment-Increasing the value of a numeric element
memcached/memcache::replace-Replace an element under existing key
memcached/memcache::set-Storage of an element

one. 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"-D runservice, 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.

Two. Configure MEMCACHD client side in PHP

Test code:

1.<? PHP   2.$memcachenew memcache ();  // Windows   3.$memcache die ("Could not connect Memcached server!"  );     4.$memcache->set (' memcache ', "Hello memcache!<br/>");   5.echo$memcache->get (' memcache ');   6.?>  

1.<? PHP   2.$memcachenew Memcached ();  // Linux   3.$memcache die ("Could not connect Memcached server!"  );     4.$memcache->set (' Memcached ', "Hello memcached!<br/>");   5.echo$memcache->get (' Memcached ');   6.?>  
three. Examples of caching in PHP
<?PHP$sql= ' SELECT * from users ';$MC=NewMemcache;$MC->pconnect (' 127.0.0.1 ', 11211); $key=MD5($sql);//memcached Object Identifiersif( ! ($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);?>

Four. Installing the OCS Client (Linux)before installing memcached, you need to confirm whether there are zlib-devel packages that need to be executedYum Install Zlib-develPlease first detect if there are installed Memcached client Package "contains source package" If there is no need to install, but need to recompile to increase-ENABLE-MEMCACHED-SASL this extensionwget http://pecl.php.net/get/memcached-2.1.0.tgzTar zxvf memcached-2.1.0.tgzCD memcached-2.1.0Phpize (if there are two sets of PHP environments in the system , you need to call the command/usr/bin/phpize absolute path, the path is the PHP environment path using OCS) ./configure--with-libmemcached-dir=/usr/local/libmemcached--ENABLE-MEMCACHED-SASL--with-php-config= (This is followed by the absolute path of the Php-config file) (note this parameter) MakeMake Installfinally modify the php.ini file (locate find the file, if the system has two sets of PHP environment, you need to find the use of the OCS PHP environment path, corresponding modification), increase the extension=memcached.soMEMCACHED.USE_SASL = 1 Five. Troubleshoot OCS password issues (Linux) OCS must enter the user name password This is more annoying, I got a patch to solve the problem of the SASL configuration , share to everyone to try. Should be able to solve c++/c Python PHP and other dependent libmemcached as the client's application, not guaranteed to solve all the problems in the environment ha.  download here: Https://github.com/ychtiger/libmemcached-OCSConfiguration: Aliyun_ocs_enable is used to identify whether the OCS feature is turned on, and if access to the OCS service, it needs to be set to True
Aliyun_ocs_name OCS Service User name
ALIYUN_OCS_PASW OCS Service Password
Note that the environment variable must be set under the user that your program starts,recommended to be set directly in. BASHRCThe main code below can be run (commented on the authentication function)

<?php
$MEMC = new Memcached ();
$MEMC->setoption (Memcached::opt_compression, false);
$MEMC->setoption (Memcached::opt_binary_protocol, true);
$MEMC->addserver ("10.232.x.x", 11211);
$MEMC->setsaslauthdata ("username", "password");//The authentication function can be canceled.
while (1)
{
$MEMC->set ("Key", "Get OCS value");
$result = $memc->get ("key");
Var_dump ($result);
Sleep (1);
}
?>

Memcached Client's explanation

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.