PHP uses memcached simple example to share

Source: Internet
Author: User

This article simply to the Code farmers introduce memcached. Memcached is an efficient and fast distributed memory object caching system that is primarily used to accelerate web dynamic applications. Today, I and yard farmers to briefly discuss the use of memcached.

1. Add memcached expansion pack

php_memcache.dll

2. Add memcached extension to php.ini

extension=php_memcache.dll

3. Procedures

<?PHP//creating an instance of a Mem object    $mem=NewMemcache; if(!$mem->connect ("10.18.110.213", 11211)){         die(' Connection failed! ')); }    //add a//1. Add a String/*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 ok99111 '; }    //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 ');//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 '; }

Original address: http://www.manongjc.com/article/683.html

Related reading:

PHP Extension Module memcached long connection usage analysis

PHP Module memcached Usage Guide

PHP5.5 How to use the Memcached service side in Windows installation

Introduction to how PHP memcached are used

PHP uses memcached simple example to share

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.