文章内容页面;
上,左侧: 标题,文章内容,彻底静态;
右侧: 文章排行,需要局部缓存,1小时时效;
 
这个静态页面是怎样生成的? 说说原理,谢谢;

解决方案 »

  1.   

    简单点的就是文件缓存,原理,第一次重db获取时,把数据也写入一个cache文件,下次再使用时,先判断cache文件是否存在和过期,如果存在且未过期,则直接输出cache文件的内容,否则重新连db获取数据,再写入缓存文件。写个简单的例子。$cacheFile = 'cache.txt';
    $data = '';
    $type = '';if(file_exists($cacheFile)){ // 缓存文件存在
        $tmp = json_decode(file_get_contents($cacheFile), true);
        if(isset($tmp['timestamp'])&& time()-$tmp['timestamp']<10){ // 缓存10秒
            $data = $tmp['data'];
            $type = 'load cache';
        }
    }if(!$data){ // 缓存不存在或过期
        // 读取db获取数据,这里直接写死来模拟
        $data = 'mydata'.microtime(true);
        file_put_contents($cacheFile, json_encode(array('timestamp'=>time(), 'data'=>$data)), true); // 写入缓存文件
        $type = 'load db';
    }echo $type.'<br>';
    echo $data;
      

  2.   

    这里有个缓存类:http://chainjoy.com/blog/php/75.html可以把整个页面缓存   排行的位置  使用ajax加载html   这个html的有效期设置为1小时   
      

  3.   

    通过redis或者memcache将数据源序列化存储,然后判断数据源是否存在,不存在就从数据库读取,存在就从缓存读取。