Redis does database caching PHP implementation

Source: Internet
Author: User

<?PHP/*database Tables (' usr_id ', ' name ', ' Face_score ', ' usr_sex ', ' Usr_headi Mgurl '*/    $redis=NewRedis (); $redis->connect (' 127.0.0.1 ', 6379); $usr _id= ' 2007 '; //example One//constructs a JSON string, which is stored in Redis and then read out, where the key is ' MyKey '. $usr _id into a string        $ret; $redis->set (' MyKey ').$usr _id, ' [{' usr_id ': ' + ', ' name ': ' Qipeng ', ' usr_sex ': ' 1 ', ' face_score ': ' + ', ' usr_headimgurl ': ' http://aaa '}] ');//Note that key value is a double-quote        if($ret=$redis->get (' MyKey ').$usr _id))        {            //echo $ret;            $row= Json_decode ($ret,true); $usr _id=$row[0] [' usr_id ']; $name=$row[0] [' Name ']; $usr _sex=$row[0] [' Usr_sex ']; $face _score=$row[0] [' Face_score ']; $usr _headimgurl=stripslashes($row[0] [' Usr_headimgurl ']); Echo"\ r \ n UserID:$usr _id, Wxname:$name, Sex:$usr _sex, Face_score:$face _scoreUsr_headimgurl:$usr _headimgurl\ n "; }        //Method Example two//through an array to construct a JSON string, into Redis, where these values can be read from the database            $get _data[] =Array(' usr_id ' = ' + ', ' name ' = ' Qipeng ', ' face_score ' = ' + ', ' usr_sex ' = ' 1 ', ' Usr_headimgurl ' = ' http://aaa '        ); $data= Json_encode ($get _data); //echo $data;        $redis->set (' MyKey ').$usr _id,$data); if($ret=$redis->get (' MyKey ').$usr _id))        {            $row= Json_decode ($ret,true); //Var_dump ($row);            $usr _id=$row[0] [' usr_id ']; $name=$row[0] [' Name ']; $usr _sex=$row[0] [' Usr_sex ']; $face _score=$row[0] [' Face_score ']; $usr _headimgurl=stripslashes($row[0] [' Usr_headimgurl ']); Echo"\ r \ n UserID:$usr _id, Wxname:$name, Sex:$usr _sex, Face_score:$face _scoreUsr_headimgurl:$usr _headimgurl\ n "; }?>

Redis does database caching PHP implementation

Related Article

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.