PHP Connection and use Memcache

Source: Internet
Author: User

1.PHP installation memcache extended download extension

PHP default is not with memcache extension, so to use the first to go to the official website to download the extension.

Official website: http://pecl.php.net/package/memcache/3.0.8/windows

    

    Here is a problem, my system is 64-bit should choose x64,x64 after download but unable to successfully configure, choose X86 but no problem

    After the download is done, unzip the DLL file into the PHP extension directory.

Configure PHP

Open a php.ini file, and add Extension=php_memcache.dll.

Restarting the server View Installation Results

You can use Phpinfo () to confirm that the installation was successful, and if Mencache is found, the installation succeeds.

    

2. Connecting the Memcache server

The object-oriented approach is mainly described here and in the next sections of use.  

1 <? PHP 2     $MEMCA=new Memcache; // instantiate a Memcache object 3     $MEMCA->connect (' 127.0.0.1 ', 11211); // Connect using the IP address, and the port number 4 5     $MEMCA->close (); // Remember to release the resources when you are finished using 6 ?>

Do not forget to open the Memchache service before connecting, the specific can refer to the previous blog.

3. Adding data using Memcache
1<?PHP2     $MEMCA=NewMemcache;3     if(!$MEMCA->connect (' 127.0.0.1 ', 11211))Exit(' Connection failed ');4 5     $MEMCA->add (' v1 ', ' This is test ',NULL, 0);//add a data named V1 value to a string and store the time as permanent6 7     $arr=Array(' AAA ', ' bbbb ', ' CCC ');8     $MEMCA->add (' v2 ',$arr,NULL, 24*60*60);//add an array, in the same vein, the object can also be added, storage time is one day9 Ten  One     classTest A     { -          Public functionA () {} -     } the     $ob=Newtest; -     $MEMCA->add (' v3 ',$ob, memcache_compressed);//stores an object, using the third parameter, which is compressed when the data is too long -  -     $MEMCA->set (' v4 ', 123);//If the data does not exist, it is added, there is a modification, and this is successfully added +     $MEMCA->set (' v1 ', 123);//Modify V1 here -  +     $MEMCA-close (); A?>
Remove data
1<?PHP2     $MEMCA=NewMemcache;3     if(!$MEMCA->connect (' 127.0.0.1 ', 11211))Exit(' Connection failed ');4 5     /*Get v1 v2 v3 separately*/6     Echo $MEMCA->get (' v1 ');Echo"<br>";7     Var_dump($MEMCA->get (' v2 '));Echo"<br>";8     Var_dump($MEMCA->get (' v3 '));Echo"<br>";9 Ten     /*Get V1 v2 v3 in the form of an associative array*/ One     Var_dump($MEMCA->get (Array(' v1 ', ' v2 ', ' v3 '));Echo"<br>"; A  -     $MEMCA-close (); -?>
Delete data
1<?PHP2     $MEMCA=NewMemcache;3     if(!$MEMCA->connect (' 127.0.0.1 ', 11211))Exit(' Connection failed ');4 5     $MEMCA->delete (' v1 ', 0);//deleting v1,0 Now is the default option, which can be added without6 7     $MEMCA->delete (' v2 ', 30);//Delete in 30 seconds8 9     $MEMCA-close ();Ten?>

    Here's a try. Set the second parameter to 30, but it is also immediately deleted, no difference is found

If all data like delete can be used

$MEMCA,flush(); // This method does not really delete all the data, except that all variables expire and the contents of the memory are overwritten
3. Example

Suppose you have the following script that accesses the script and, based on the ID value passed in, finds the record from the table account and returns it.

1<?PHP2     Try{3         $pdo=NewPDO (' Mysql:host=localhost;dbname=example ', ' root ', ');4 5         $result=$pdo->query ("SELECT * From Account WHERE id={$_get[' ID ']} ");6 7         Var_dump($result-fetch ());8     }9     Catch(pdoexception$e){Ten         Exit(' Database connection or operation failed '); One     }; A?>

There is no logical problem with this code, but there may be many clients accessing the script concurrently on the network, many of which have the same ID value.

For each access, the script is non-discriminatory: reads the data from the database (the data exists on the hard disk) according to the SQL statement and reads the results into memory and then outputs.

Now use Memcache as long as adding an add operation and judgment, as follows

1<?PHP2     $id=$_get[' ID '];3 4     $MEMCA=NewMemcache;5     $MEMCA->connect (' 127.0.0.1 ', 11211);6 7     if($row=$MEMCA->get ($id))//determines whether the value of the ID exists in the MEMCAHCE and returns it directly from memory if it exists8     {9         Var_dump($row);Ten     } One     Else A     { -         Try{ -             $pdo=NewPDO (' Mysql:host=localhost;dbname=example ', ' root ', '); the             $result=$pdo->query ("SELECT * From Account WHERE id=$id"); -  -             if($result->rowcount ()!=0) -             { +                 $row=$result-fetch (); -  +                 $MEMCA->add ($id,$row);//The ID is the key and the result of the query is stored in the Memcache value . A  at                 Var_dump($row);Echo"First time Query"; -             } -             Else{ -                 Echo"No Records"; -             } -         } in         Catch(pdoexception$e){ -             Exit(' Database connection or operation failed '); to         }; +     } -     $MEMCA-close (); the?>

This way, as long as the ID has been queried before, then the record will be taken directly from memory, you do not have to connect to the database for a series of operations.

PHP Connection and use Memcache

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.