PHP use memcached simple example sharing _ php instance

Source: Internet
Author: User
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, we will briefly discuss the usage of memcached. 1. Add an 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:


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

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.