08 读取缓存方式开发首页接口

一:读取缓存方式开发首页接口

流程图:从数据库获取信息--->>>封装[同时把一份数据存入缓存]----->>>>返回数据

file.php


<?php class File { private $_dir; const EXT = '.txt'; public function __construct() { $this->_dir = dirname(__FILE__) . '/files/'; } public function cacheData($key, $value = '', $cacheTime = 0) { $filename = $this->_dir . $key . self::EXT; if($value !== '') { // 将value值写入缓存 if(is_null($value)) { return @unlink($filename); } $dir = dirname($filename); if(!is_dir($dir)) { mkdir($dir, 0777); } $cacheTime = sprintf('%011d', $cacheTime); return file_put_contents($filename,$cacheTime . json_encode($value)); } if(!is_file($filename)) { return FALSE; } $contents = file_get_contents($filename); $cacheTime = (int)substr($contents, 0 ,11); $value = substr($contents, 11); if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) { unlink($filename); return FALSE; } return json_decode($value, true); } } $file = new File(); echo $file->cacheData('test1');

 

posted @ 2015-03-24 09:45  麦田守望者~  阅读(135)  评论(0编辑  收藏  举报