Home > Backend Development > PHP Tutorial > PHP uses memcached simple example sharing_PHP tutorial

PHP uses memcached simple example sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:05:03
Original
738 people have browsed it

Sharing a simple example of using memcached in PHP

On many occasions, we will hear the name memcached, but many students have only heard of it and have not used it or actually understood it. I know it is a very good thing. Here is a brief introduction: memcached is an efficient and fast distributed memory object caching system, mainly used to accelerate WEB dynamic applications. Today we will briefly discuss the usage of memcached

1. Add expansion pack

The code is as follows:


php_memcache.dll

2. Add

in PHP.INI

The code is as follows:


extension=php_memcache.dll

3. Procedure

The code is as follows:


//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.

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template