1 Header("Content-type:text/html;charset=utf-8");2 /*3 PHP set up distributed memcache access4 5 */6 //Create a Memcache object7 $mem=NewMemcache;8 9 //Connecting Memcache serversTen $memConnect (' localhost ', 11211); One $memAddserver (' 192.168.1.102 ', 11211);//add a server to the server A - - /*$mem, add (' One ', ' This is Memcache test ', memcache_compressed,1000); the $mem, add (' N ', Array (' 111 ', ' 222 ', ' 333 '), memcache_compressed,0); - $mem, add (' Three ', New Test (), memcache_compressed,0); - $mem, add (' Four ', 100,memcache_compressed,0);*/ - + //Connection and operation of the database - + $key= "MyData"; A //memcache data directly from memory at $data=$mem-A Get ($key); - //If the data is returned from the content, if the database is not connected, execute the SQL statement - - if(Empty($data)){ - - $sql= "Select ' id ', ' title ', ' Author ', ' Content ', ' add_time ' from ' the ' News ' ORDER by ' id ' = 1"; in - Try{ to $pdo=NewPDO ("Mysql:host=localhost;dbname=ci", ' root ', ' root '); + //Var_dump ($pdo); -}Catch(pdoexception$e){ the Echo"Database link failed!".$e->getmessage ($sql); * } $ Panax Notoginseng //get data, execute query statement - $stmt=$pdo-Prepare ($sql); the + $stmt-execute (); A the $data=$stmt-Fetchall (PDO::FETCH_ASSOC); + - $mem-Add ($key,$data, memcache_compressed,10); $ $ Echo"This is the first time you've accessed data from a database and put it in memory!"; - } - Echo"<pre>"; the Print_r($data); - Echo"</pre>";Wuyi //Close Memcache Connection the $memClose ();
Memcache Learning a complete example of PHP