Simple example of using memcached in PHP _ PHP Tutorial

Source: Internet
Author: User
Simple example of using memcached in PHP. The simple example of using memcached in PHP is shared in many occasions and we will hear the name of memcached. However, many of you have only heard of it and have never used it or actually understood it, I only know that it is a simple example of using memcached in PHP.

On many occasions, we will hear the name memcached, but many of you have heard of it, and have never used it or actually understood it. I only know that it is a good stuff. Memcached is an efficient and fast distributed memory object cache system, mainly used to accelerate dynamic WEB applications. Today, let's briefly discuss the usage of memcached.

1. add extension package

The code is as follows:


Php_memcache.dll

2. add in PHP. INI

The code is as follows:


Extension = php_memcache.dll

3. program

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 a value
/* If ($ mem-> set ('key1', 100, MEMCACHE_COMPRESSED, 60 )){
Echo 'add OK ';
}*/
// 3. add an array
// When adding an array, you need to add the serial number as needed,
// Serialize <=> unserialize. if needed, you can also use json_encode <=> json_decode
$ Arr = array ("bj", 'TJ ');
If ($ mem-> set ('key1', $ arr, MEMCACHE_COMPRESSED, time () + 31*3600*24 )){
Echo 'add an array ok99111 ';
}
// 4. add an 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 a null Boolean value
/* If ($ mem-> set ('key1', false, MEMCACHE_COMPRESSED, 60 )){
Echo 'add Boolean OK ';
}*/
// 6. add the resource type.
/* $ Con = mysql_connect ("127.0.0.1", "root", "root ");
If (! $ Con ){
Die ('database connection failed ');
}
Var_dump ($ con );
Echo"
";
If ($ mem-> set ('key1', $ con, MEMCACHE_COMPRESSED, 60 )){
Echo 'add resource OK ';
}*/
// Query
$ Val = $ mem-> get ('key1 ');
// Modify
// Replace can be used
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 an introduction to the usage of memcache in php. I hope it will be helpful to you.

Http://www.bkjia.com/PHPjc/963980.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/963980.htmlTechArticlePHP using memcached simple example to share in many occasions, we will hear the name of memcached, but many people just heard of it, and did not know it or actually know it is...

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.