3. Procedure
//Create a mem object instance
$mem=new Memcache;
if(!$mem->connect("10.18.110.213",11211)){
die('Connection failed!');
}
//Add
//1. Add a string
/* if($mem->set('key1',"beijing",MEMCACHE_COMPRESSED,60)){
echo 'Add ok';
}*/
//2. Add value
/* if($mem->set('key1',100,MEMCACHE_COMPRESSED,60)){
echo 'Add ok';
}*/
//3.Add array
//When adding the array, as needed. I hope the serial number will be put into ,
//serialize<=>unserialize, if needed, you 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. Add object
/* class Dog{
public $name;
public $age;
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
}
$dog1=new Dog('puppy',50);
if($mem->set('key1',$dog1,MEMCACHE_COMPRESSED,60)){
echo 'Add object ok';
}*/
//5.Add null Boolean value
/* if($mem->set('key1',false,MEMCACHE_COMPRESSED,60)){
echo 'Add boolean ok';
}*/
//6. Put the resource type.
/* $con=mysql_connect("127.0.0.1","root","root");
if(!$con){
die('Failed to connect to database');
}
var_dump($con);
echo "
";
if($mem->set('key1',$con,MEMCACHE_COMPRESSED,60)){
echo 'Add resource ok';
}*/
//Query
$val=$mem->get('key1');
//Modify
//You can use replace
if($mem->replace("key11",'hello',MEMCACHE_COMPRESSED,60)){
echo 'replace ok';
}else{
echo 'replace no ok';
}
//Delete
echo "
";
if($mem->delete('key14')){
echo 'key14 delete';
}else{
echo 'key14 does not exist';
}
The above is the introduction of this article on the use of memcache in php. I hope it can be helpful to everyone.
http://www.bkjia.com/PHPjc/963980.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963980.htmlTechArticlePHP sharing a simple example of using memcached On many occasions, we will hear the name memcached, but many students have only heard of it , I have never used or actually understood it, I only know that it is...