redis 做資料庫緩衝 php實現

來源:互聯網
上載者:User

標籤:red   php   nbsp   color   引號   方法   緩衝   decode   key   

<?php    /*資料庫的表            ( ‘usr_id‘,             ‘name‘,            ‘face_score‘,             ‘usr_sex‘,            ‘usr_headimgurl‘    */    $redis = new Redis();    $redis->connect(‘127.0.0.1‘,6379);    $usr_id =‘2007‘;        //方法例一        //構造一個json字串,存入redis,再讀出來, 這裡的key是 ‘mykey‘ . $usr_id 拼成一個字串        $ret;        $redis->set(‘mykey‘ . $usr_id,‘[{"usr_id":"2007","name":"qipeng","usr_sex":"1","face_score":"1000","usr_headimgurl":"http://aaa"}]‘); //注意key value是用的雙引號        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_score usr_headimgurl:$usr_headimgurl\n";        }        //方法例二        //通過一個數組構造一個json字串,存入redis,這裡的這些值就可以是從資料庫讀出來的             $get_data[] = array( ‘usr_id‘=>‘2007‘,             ‘name‘=>‘qipeng‘,            ‘face_score‘=>‘1000‘,             ‘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_score usr_headimgurl:$usr_headimgurl\n";        }?>

 

redis 做資料庫緩衝 php實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.