memcached Technology PHP Operation Memcached (i)

Source: Internet
Author: User

How to usePHPprogram to operate ourmemcachedService

Danga Company provides a set of extensions (i.e. Php_memcache.dll) for use in PHP to enable PHP programs to operate memcached.

steps to prepare for the job .

(1) Copy the Php_memcache.dll file to php ext .

? Different versions of PHP Use the same version of Php_memcache.dll

(2) Modify the php.ini file, load the Php_memcache.dll ( the file is encapsulated a bunch of functions )

; Loading Php_memcache.dll file

Extension=php_memcache.dll (. dll extension is used by VC to C or C + + compiled by the program. )

(3) Restart Apache

(4) We write the program to complete the curd operation .

mem1.php

<?PHP//creating an instance of a Mem object    $mem=NewMemcache; if(!$mem->connect ("127.0.0.1", 11211)) {//Open a memcached service-side Connection         die(' Connection failed! ')); }    //add//1. Add a string of //memcached::add (...) Returns False if there are already key1. Memcached::set (...), key1 exists replace, Key1 does not exist add     /*if ($mem->set (' Key1 ', "Beijing", memcache_compressed,60)) {echo ' Add OK '; }*/        //2. Add a value   /* if ($mem->set (' Key1 ', 100,memcache_compressed,60)) {echo ' Add OK '; }*/    //3. Add an array//In the Add array is, as needed. Hope the serial number is put in,//serialize<=>unserialize, if necessary, can also json_encode <=> json_decode    $arr=Array("BJ", ' TJ '); if($mem->set (' Key1 ',$arr, memcache_compressed, Time() +31*3600*24)){        Echo' Add array ok '; }    //4. Adding Objects   /*class dog{public $name;        Public $age;            Public function __construct ($name, $age) {$this->name= $name;        $this->age= $age;    }} $dog 1=new dog (' Puppy ', 50);    if ($mem->set (' Key1 ', $dog 1,memcache_compressed,60)) {echo ' Add object OK '; }*/    //5. Adding a null Boolean value    /*if ($mem->set (' Key1 ', false,memcache_compressed,60)) {echo ' adds Boolean ok '; }*/    //6. Type the resource into.    /*$con =mysql_connect ("127.0.0.1", "root", "root");    if (! $con) {die (' failed to connect to database ');    } var_dump ($con);    echo "<br/>";    if ($mem->set (' Key1 ', $con, memcache_compressed,60)) {echo ' Add resource ok '; }*/    //Enquiry    $val=$mem->get (' Key1 '); Var_dump($val); //Modify//can use replace    if($mem->replace ("Key11", ' Hello ', memcache_compressed,60)){        Echo' Replace OK '; }Else{        Echo' Replace no OK '; }    //Delete    Echo"<br/>"; if($mem->delete (' Key14 ')){        Echo' Key14 Delete '; }Else{        Echo' KEY14 does not exist '; }

How to use PHP source code to operate the memcached service

If the administrator does not let us to load the memcache.dll file, we can directly through the source code operation .

First close the Php_memcache.dll extension, and then look at the mem3.php code:

memcached Technology PHP Operation Memcached (i)

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.