memcache

来源:互联网 发布:淘宝网灯饰 编辑:程序博客网 时间:2024/06/10 02:41
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Welcome extends CI_Controller {   public function index()   {        $pdo=new PDO('mysql:host=localhost;dbname=text','root','root');        $pdo->exec("set names utf8");        $data=$pdo->query("select * from blog");        $this->load->vars('data',$data);      $this->load->view('index.html');   }    public function show()    {        $pdo=new PDO('mysql:host=localhost;dbname=text','root','root');        $pdo->exec("set names utf8");        $id=$_GET['id'];        $mem=new Memcache;        $mem->connect('127.0.0.1',11211);        $num=$mem->get($id);        if($num){            //之前存在  每次访问+1            $num++;            $mem->set($id,$num);            if($num>5){                //直接从memcache中取出数据                $str=$mem->get('data'.$id);                echo $str;            }elseif($num==5){                //取出数据存到memache中                $data=$pdo->query("select * from blog where id=$id")->fetch(PDO::FETCH_ASSOC);                $str=json_encode($data);                $mem->set('data'.$id,$str);                echo $str;            }else{                $data=$pdo->query("select * from blog where id=$id")->fetch(PDO::FETCH_ASSOC);                $str=json_encode($data);                echo $str;            }        }else{            //第一次查询            $mem->set($id,1);            $data=$pdo->query("select * from blog where id=$id")->fetch(PDO::FETCH_ASSOC);            echo json_encode($data);        }    }}
0 0
原创粉丝点击