This article is mainly to share with you PHP implementation query MySQL and cache to Redis method, hope to help everyone.
First install Redis and open the Php_redis extension in a PHP environment.
Not much to say, directly on the code
<?php $redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6379), $blog = $redis->get (' Redisrow ');//If the $blog array is empty, Then go to the database and add it to Redis if (empty ($blog)) {echo "MySQL";//Connect MySQL server$mysql = new PDO ("Mysql:host=localhost;dbname =blog "," Root "," "", Array (Pdo::mysql_attr_init_command = "SET NAMES UTF8")); $rs = $mysql, Query ("SELECT * from Tbl_post");//$row = $rs, Fetch (), $i =0;while ($row = $rs, Fetch ()) {$rows [$ i][' title ']= $row [' title ']; $rows [$i] [' content ']= $row [' content ']; $i = $i +1;} Print_r ($rows), $redisrow = Json_encode ($rows), $redis->setex (' Redisrow ', ' n ', $redisrow);} else{$redisblog = Json_decode ($blog), echo "Redis";p Rint_r ($redisblog);}? >