- PDO方式连接数据库类
<?php/** * @author 黄功延 * createTime 2018/5/28 0028 09:44 */class Db { ???//私有化数据库连接数据,可以通过getInstance传入自己的连接数据来进行指定数据库连接(写框架的基础) ???private $config = [ ???????‘dbname‘ ??=> ‘shunk‘, ???????‘username‘ => ‘root‘, ???????‘password‘ => ‘root‘ ???]; ???private static $instance =null; ???private $conn = null; ???//私有化构造方法,防止外部实例化这个类,即防止 new Db() ???private function __construct() { ???????$this->connect(); ???} ???//私有化克隆方法,防止外部克隆复制这个类 ???private function __clone() { ???????// TODO: Implement __clone() method. ???} ???//保证继承单一性,外部只能通过这个静态方法访问这个类 ???public static function getInstance(){ ???????if(!(self::$instance instanceof self)){ ???????????self::$instance = new self(); ???????} ???????return self::$instance; ???} ???//PDO方式连接数据库的方法 ???private function connect(){ ???????try{ ???????????$dns = ‘mysql:dbname=‘.$this->config[‘dbname‘].‘;host=localhost;port=3306;charset=utf8‘; ???????????$this->conn = new PDO($dns,$this->config[‘username‘],$this->config[‘password‘]); ???????????$this->conn->query(‘SET NAMES utf8‘); ???????}catch (PDOException $e){ ???????????die(‘数据库连接失败‘.$e->getMessage()); ???????} ???} ???//查询一条记录 ???public function fetch($sql){ ???????return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC); ???} ???//查询多条记录 ???public function fetchAll($sql){ ???????return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC); ???} ???//增删改方法,删改成功返回受影响的行数。插入成功返回插入的id值 ???public function exec($sql){ ???????$num = $this->conn->exec($sql); ???????if($num){ ???????????if(‘0‘ != $this->conn->lastInsertId()){ ???????????????return $this->conn->lastInsertId(); ???????????} ???????????return $num; ???????}else{ ???????????$error = $this->conn->errInfo(); ???????????return ‘操作失败‘.$error[0].‘:‘.$error[1].‘,‘.$error[2]; ???????} ???}}
2.缓存PHP文件
<?php/** * @author 黄功延 * createTime 2018/5/28 0028 09:06 */$fileName = ‘../runtime/fileCache.php‘;$time = 15;//判断缓存文件是否存在和缓存文件是否过期if(file_exists($fileName) && (filemtime($fileName)+$time) >= time()){ ???include (‘../runtime/fileCache.php‘);}else{ ???ob_start(); //开启内存缓存 ???include (‘../Db.php‘); ???$db = Db::getInstance(); ???$sql = ‘select * from sk_url‘; ???$info = $db->fetchAll($sql); ???include ‘index1.php‘; ???$str = ob_get_contents(); //得到缓存区内容 ???file_put_contents($fileName,$str); //写入缓存 ???ob_flush(); //关闭内存缓存}
3.html文件
<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>测试</title></head><body> ???<table width="600" border="1" style="width: 700px; margin: 0 auto;"> ???????<thead> ???????<tr> ???????????<th>id</th> ???????????<th>url</th> ???????????<th>short</th> ???????????<th>status</th> ???????????<th>create_time</th> ???????</tr> ???????</thead> ???????<tbody> ???????<?php foreach ($info as $v){;?> ???????????<tr> ???????????????<td><?php echo $v[‘id‘];?></td> ???????????????<td><?php echo $v[‘url‘];?></td> ???????????????<td><?php echo $v[‘short‘];?></td> ???????????????<td><?php echo $v[‘status‘];?></td> ???????????????<td><?php echo $v[‘create_time‘];?></td> ???????????</tr> ???????<?php };?> ???????</tbody> ???</table></body></html>
到此文件缓存的简单例子就完成了
php 的file 缓存
原文地址:https://www.cnblogs.com/wuyan717/p/9099780.html