php缓存

  1. 什么是缓存?

         a)  缓存就是在硬盘或者是内存当中临时存放数据的一块区域

  1. 为什么要使用缓存?

          a)  降低数据库的访问压力

          b)  减少对数据库的查询

          c)  提高页面的响应速度

  1. 什么时候使用缓存?

          a)  项目的访问量较大时

  1. 缓存的介质类型:

          a)   硬盘(文件缓存)

  1. 数据缓存
    1. 序列化处理

                    a)   Serialize         将数据进行序列化处理

                    b)   Unserialize     将数据进行反序列化处理

  1. JSON格式

                    a)   Json_encode   将数据转换成json格式的字符串

                    b)   Json_decode   将数据转换成数组

  1. 注意,第二个参数为true,则为数组,否则为对象
  2. XML格式:

       a)    simpleXML_load_string()

  1. 数组格式:

         a)   var_export($data,true)   将数组打散成PHP语法格式的字符串

                    b)   $data = include/require $cachefile; 直接将该文件return的内容返回

      b)        内存(内存缓存)

  1. 缓存的形式:

                  a)         数据缓存

                  b)        Html代码缓存

 

json格式

<?php
header("content-type:text/html;charset=utf-8");
//1.定义缓存文件的名称
$cachefile = "./cache/2.php.json";
//2.定义缓存文件的生命周期
$cache_lifetime = 30;

//判断缓存文件是否存在,并且是否过期,如果文件存在没有过期,读取缓存文件,否则查询数据库,生成缓存
if(file_exists($cachefile)&&time()-filemtime($cachefile)<=30){

//读取缓存文件
$data = file_get_contents($cachefile);
//处理数据
$data = json_decode($data,true);

}else{

echo "select data....";

//1.查询数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=psd1412","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));

$pdo->query("set names utf8");

$stmt = $pdo->prepare("select * from cms_user");

$stmt->execute();

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

//将数据缓存到文件当中
//JSON
//echo json_encode($data);
file_put_contents($cachefile,json_encode($data));

}catch(PDOException $e){
echo $e->getMessage();
}

}

//2.遍历数据
echo "<table border='1' width='500'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>username</th>";
echo "<th>rtime</th>";
echo "<th>rip</th>";
echo "</tr>";
foreach($data as $user){
echo "<tr>";
echo "<td>".$user['id']."</td>";
echo "<td>".$user['username']."</td>";
echo "<td>".date("Y-m-d H:i:s",$user['rtime'])."</td>";
echo "<td>".long2ip($user['rip'])."</td>";
echo "</tr>";
}
echo "</table>";

//.json的数据格式
[{"id":"1","username":"admin","password":"202cb962ac59075b964b07152d234b70","rtime":"1426840429","rip":"2130706433"},{"id":"2","username":"ligousheng","password":"202cb962ac59075b964b07152d234b70","rtime":"1426840448","rip":"2130706433"},{"id":"3","username":"zhansgan","password":"202cb962ac59075b964b07152d234b70","rtime":"1426840457","rip":"2130706433"},{"id":"4","username":"lisi","password":"123","rtime":"123","rip":"123"},{"id":"5","username":"wangwu","password":"123","rtime":"123","rip":"123"},{"id":"6","username":"zhaoliu","password":"123","rtime":"123","rip":"123"},{"id":"7","username":"tianqi","password":"123","rtime":"123","rip":"123"},{"id":"8","username":"zhaoba","password":"123","rtime":"123","rip":"123"},{"id":"9","username":"lisi","password":"123","rtime":"123","rip":"123"},{"id":"10","username":"wangwu","password":"123","rtime":"123","rip":"123"},{"id":"11","username":"zhaoliu","password":"123","rtime":"123","rip":"123"},{"id":"12","username":"tianqi","password":"123","rtime":"123","rip":"123"},{"id":"13","username":"zhaoba","password":"123","rtime":"123","rip":"123"}]

 

序列化

<?php
    header("content-type:text/html;charset=utf-8");
    
    //定义程序开始时间
    $start = microtime(true);
    
    //1.定义缓存文件的名称
    $cachefile = "./cache/1.php.cache";
    
    //定义缓存的生命周期
    $cache_lifetime = 30;
    
    //2.判断缓存文件是否存在
    if(!file_exists($cachefile) || time()-filemtime($cachefile)>$cache_lifetime){
        
        echo "select data....";

        //数据库的查询
        try{
            $pdo = new PDO("mysql:host=localhost;dbname=psd1412","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
            $pdo->query("set names utf8");
            $stmt = $pdo->prepare("select * from cms_user");
            $stmt->execute();

            //$data即为查询出的数据
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

            //将数据写入到缓存文件当中
            //需要将数据提前处理一下
            //序列化
            /*class Person{
                public $name='zhangsan';
            }
            $p = new Person;*/
            //echo serialize($data);
            file_put_contents($cachefile,serialize($data));

        }catch(PDOException $e){
            echo $e->getMessage();
        }
    
    }else{
        //读取缓存数据
        $data = file_get_contents($cachefile);
        //处理序列化数据(反序列化)
        $data = unserialize($data);
    }
    
    //数据的遍历
    echo "<table border='1' width='500'>";
    echo "<tr><th>ID</th><th>用户名</th><th>注册时间</th><th>注册IP</th><th>操作</th></tr>";
    foreach($data as $user){
        echo "<tr>";
        echo "<td>".$user['id']."</td>";
        echo "<td>".$user['username']."</td>";
        echo "<td>".date("Y-m-d H:i:s",$user['rtime'])."</td>";
        echo "<td>".long2ip($user['rip'])."</td>";
        echo "<td>编辑 删除</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    $end = microtime(true);
    
    echo "spend:".($end-$start);

序列化数据的格式:
a:13:{i:0;a:5:{s:2:"id";s:1:"1";s:8:"username";s:5:"admin";s:8:"password";s:32:"202cb962ac59075b964b07152d234b70";s:5:"rtime";s:10:"1426840429";s:3:"rip";s:10:"2130706433";}i:1;a:5:{s:2:"id";s:1:"2";s:8:"username";s:10:"ligousheng";s:8:"password";s:32:"202cb962ac59075b964b07152d234b70";s:5:"rtime";s:10:"1426840448";s:3:"rip";s:10:"2130706433";}i:2;a:5:{s:2:"id";s:1:"3";s:8:"username";s:8:"zhansgan";s:8:"password";s:32:"202cb962ac59075b964b07152d234b70";s:5:"rtime";s:10:"1426840457";s:3:"rip";s:10:"2130706433";}i:3;a:5:{s:2:"id";s:1:"4";s:8:"username";s:4:"lisi";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:4;a:5:{s:2:"id";s:1:"5";s:8:"username";s:6:"wangwu";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:5;a:5:{s:2:"id";s:1:"6";s:8:"username";s:7:"zhaoliu";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:6;a:5:{s:2:"id";s:1:"7";s:8:"username";s:6:"tianqi";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:7;a:5:{s:2:"id";s:1:"8";s:8:"username";s:6:"zhaoba";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:8;a:5:{s:2:"id";s:1:"9";s:8:"username";s:4:"lisi";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:9;a:5:{s:2:"id";s:2:"10";s:8:"username";s:6:"wangwu";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:10;a:5:{s:2:"id";s:2:"11";s:8:"username";s:7:"zhaoliu";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:11;a:5:{s:2:"id";s:2:"12";s:8:"username";s:6:"tianqi";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}i:12;a:5:{s:2:"id";s:2:"13";s:8:"username";s:6:"zhaoba";s:8:"password";s:3:"123";s:5:"rtime";s:3:"123";s:3:"rip";s:3:"123";}}

XML的形式

<?php
    header("content-type:text/html;charset=utf-8");
    
    //1.定义缓存文件的名称
    $cachefile = "./cache/3.php.xml";
    //2.定义一个缓存文件的生命周期
    $cache_lifetime = 30;
    
    //判断缓存文件是否存在,并且是否过期,如果存在并且没有过期,读取缓存文件,否则查询数据库,更新缓存文件
    if(file_exists($cachefile)&&time()-filemtime($cachefile)<=$cache_lifetime){
        //读取缓存文件
        $data = file_get_contents($cachefile);
        //并且转换成二维数组
        $obj = simplexml_load_string($data);
        $data = array();
        foreach($obj->users->user as $user){
            $data[] = array('id'=>strval($user->id),'username'=>strval($user->username),'password'=>strval($user->password),'rtime'=>strval($user->rtime),'rip'=>strval($user->rip));
        }
        
    }else{
    
        //查询数据库
        try{
            $pdo = new PDO("mysql:host=localhost;dbname=psd1412","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
            $pdo->exec("set names utf8");
            $stmt = $pdo->prepare("select * from cms_user");
            $stmt->execute();
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            //拼装xml字符串,写入到xml文件当中
            $xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            $xml .= "<root><users>";
            foreach($data as $user){
                $xml .= "<user>";
                $xml .= "<id>".$user['id']."</id>";
                $xml .= "<username>".$user['username']."</username>";
                $xml .= "<rtime>".$user['rtime']."</rtime>";
                $xml .= "<rip>".$user['rip']."</rip>";
                $xml .= "</user>";
            }
            $xml .= "</users></root>";
            file_put_contents($cachefile,$xml);
            
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }
        
    //遍历数据
    echo "<table border='1' width='500'>";
    echo "<tr>";
    echo "<th>ID</th>";
    echo "<th>username</th>";
    echo "<th>rtime</th>";
    echo "<th>rip</th>";
    echo "</tr>";
    foreach($data as $user){
        echo "<tr>";
        echo "<td>".$user['id']."</td>";
        echo "<td>".$user['username']."</td>";
        echo "<td>".date("Y-m-d H:i:s",$user['rtime'])."</td>";
        echo "<td>".long2ip($user['rip'])."</td>";
        echo "</tr>";
    }
    echo "</table>";

xml数据的格式
<?xml version="1.0" encoding="utf-8" ?><root><users><user><id>1</id><username>admin</username><rtime>1426840429</rtime><rip>2130706433</rip></user><user><id>2</id><username>ligousheng</username><rtime>1426840448</rtime><rip>2130706433</rip></user><user><id>3</id><username>zhansgan</username><rtime>1426840457</rtime><rip>2130706433</rip></user><user><id>4</id><username>lisi</username><rtime>123</rtime><rip>123</rip></user><user><id>5</id><username>wangwu</username><rtime>123</rtime><rip>123</rip></user><user><id>6</id><username>zhaoliu</username><rtime>123</rtime><rip>123</rip></user><user><id>7</id><username>tianqi</username><rtime>123</rtime><rip>123</rip></user><user><id>8</id><username>zhaoba</username><rtime>123</rtime><rip>123</rip></user><user><id>9</id><username>lisi</username><rtime>123</rtime><rip>123</rip></user><user><id>10</id><username>wangwu</username><rtime>123</rtime><rip>123</rip></user><user><id>11</id><username>zhaoliu</username><rtime>123</rtime><rip>123</rip></user><user><id>12</id><username>tianqi</username><rtime>123</rtime><rip>123</rip></user><user><id>13</id><username>zhaoba</username><rtime>123</rtime><rip>123</rip></user></users></root>

 

存入的是数组

<?php
    header("content-type:text/html;charset=utf-8");
    
    //1.定义一个缓存文件名称
    $cachefile = "./cache/4.data.php";
    //2.声明缓存文件的生命周期
    $cache_lifetime = 30;
    
    //判断缓存文件是否存在,并且是否过期,如果缓存文件存在,并且没有过期,直接读取缓存文件,否则查询数据库,更新缓存文件
    if(file_exists($cachefile)&&time()-filemtime($cachefile)<=$cache_lifetime){
        //读取缓存文件
        $data = include $cachefile;
    }else{
        echo "select data....";
        //查询数据库
        try{
            $pdo = new PDO("mysql:host=localhost;dbname=psd1412","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
            $pdo->exec("set names utf8");
            $stmt = $pdo->prepare("select * from cms_user");
            $stmt->execute();
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            //将数组写入到缓存文件当中
            $str = "<?php\nreturn ".var_export($data,true).";";
            file_put_contents($cachefile,$str);
            
        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }
        
    //遍历数据
    echo "<table border='1' width='500'>";
    echo "<tr>";
    echo "<th>ID</th>";
    echo "<th>username</th>";
    echo "<th>rtime</th>";
    echo "<th>rip</th>";
    echo "</tr>";
    foreach($data as $user){
        echo "<tr>";
        echo "<td>".$user['id']."</td>";
        echo "<td>".$user['username']."</td>";
        echo "<td>".date("Y-m-d H:i:s",$user['rtime'])."</td>";
        echo "<td>".long2ip($user['rip'])."</td>";
        echo "</tr>";
    }
    echo "</table>";

 

存入的是html格式

<?php
    header("content-type:text/html;charset=utf-8");

    //1.缓存文件名称
    $cachefile = "./cache/5.php.html";
    //2.缓存的生命周期
    $cache_lifetime = 30;
    
    //判断缓存文件是否存在,并且是否过期,如果缓存文件存在,并且没有过期,直接加载缓存文件即可,否则,读取数据库,并且更新数据
    if(file_exists($cachefile)&&time()-filemtime($cachefile)<=$cache_lifetime){
        include $cachefile;
    }else{
    
        //查询数据库
        try{
            $pdo = new PDO("mysql:host=localhost;dbname=psd1412","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
            $pdo->exec("set names utf8");
            $stmt = $pdo->prepare("select * from cms_user");
            $stmt->execute();
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

        }catch(PDOException $e){
            echo $e->getMessage();
        }
            
        //遍历数据,拼接字符串,写入缓存文件
        $html = "<table border='1' width='500'>";
        $html .= "<tr>";
        $html .= "<th>ID</th>";
        $html .= "<th>username</th>";
        $html .= "<th>rtime</th>";
        $html .= "<th>rip</th>";
        $html .= "</tr>";
        foreach($data as $user){
            $html .= "<tr>";
            $html .= "<td>".$user['id']."</td>";
            $html .= "<td>".$user['username']."</td>";
            $html .= "<td>".date("Y-m-d H:i:s",$user['rtime'])."</td>";
            $html .= "<td>".long2ip($user['rip'])."</td>";
            $html .= "</tr>";
        }
        $html .= "</table>";
        file_put_contents($cachefile,$html);
        
        echo $html;
    }

 

posted @ 2015-08-27 13:07  tiandi2050  阅读(201)  评论(0编辑  收藏  举报