Simple example of using memcached in php and phpmemcached
This article briefly introduces memcached to coders. Memcached is an efficient and fast distributed memory object cache system, mainly used to accelerate Dynamic WEB applications. Today, I am going to discuss the usage of memcached with coders.
1. Add a memcached extension package
php_memcache.dll
2. Add the memcached extension in PHP. INI.
extension=php_memcache.dll
3. Program
<? 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 Value/* if ($ mem-> set ('key1', 100, MEMCACHE_COMPRESSED, 60) {echo 'add OK ';} * // 3. add an array // Add an array as needed. if you want the serial number to be placed in, // serialize <=> unserialize, you can also use json_encode <=> json_decode $ arr = array ("bj", 'tj ') as needed '); 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 a null Boolean value/* if ($ mem-> set ('key1', false, MEMCACHE_COMPRESSED, 60) {echo 'add Boolean OK ';} * // 6. put the resource type in. /* $ con = mysql_connect ("127.0.0.1", "root", "root"); if (! $ Con) {die ('database connection failed');} var_dump ($ con); echo "<br/>"; 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 "<br/>"; if ($ mem-> delete ('key14 ')) {echo 'key14 delete';} else {echo 'key14 does not exist ';}
Address: http://www.manongjc.com/article/683.html
Related reading:
Php extension module memcached persistent connection Usage Analysis
Php module memcached User Guide
PHP5.5 how to install and use memcached server on windows
Php Memcached usage