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.