分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

php 的file 缓存

发布时间:2023-09-06 01:56责任编辑:傅花花关键词:缓存
  1. 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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved