php 的file 缓存
- 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 缓存的更多相关文章
- File缓存
/** * 保存对象 * @param ser * @param file * @throws IOException */ public b ...
- Laravel之路——file缓存修改为redis缓存
1.Session: 修改.evn文件: SESSION_DRIVER:redis (如果还不行的话,修改config/session.php的driver) 2.缓存修改为redis 注意:使用 L ...
- django 缓存、中间件、信号、CSRF 详解
中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...
- php使用文件缓存
使用php读取mysql中的数据很简单,数据量不大的时候,mysql的性能还是不错的.但是有些查询可能比较耗时,这时可以把查询出的结果,缓存起来,减轻mysql的查询压力. 缓存的方法有几种:使用me ...
- ThinkPHP的缓存技术
原文:ThinkPHP的缓存技术 如果没有缓存的网站是百万级或者千万级的访问量,会给数据库或者服务器造成很大的压力,通过缓存,大幅减少服务器和数据库的负荷.假如我们 把读取数据的过程分为三个层,第一个 ...
- Discuz的缓存体系
参考文档:<http://dev.discuz.org/wiki/index.php?title=缓存机制> Discuz中涉及数据缓存的地方有: 1. session Dz的sessio ...
- php 文件缓存
http://www.oschina.net/code/snippet_162279_6098 <?php class cache { private static $_instan ...
- php文件缓存
1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...
- php 缓存 加速缓存
PHP四大加速缓存器opcache,apc,xcache,eAccelerator eAccelerator,xcache,opcache,apc(偏数据库缓存,分系统和用户缓存)是PHP缓存扩展, ...
随机推荐
- 交换一个数字的任意两个位置,指定K次的最值
Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautifu ...
- Linux下安装Ant
前言 Apache Ant 是一个构建工具,可以将软件编译.测试.部署等步骤联系在一起完成自动化工作,比如可以利用Ant来执行Jmeter的 jmx 脚本,生成了 jtl 测试结果文件,接着再利用An ...
- Java入门 - 语言基础 - 11.switch_case
原文地址:http://www.work100.net/training/java-switch-case.html 更多教程:光束云 - 免费课程 switch_case 序号 文内章节 视频 1 ...
- Mysql一分钟定位 Next-Key Lock,你需要几分钟
连接与线程 查看连接信息 show processlist +----+------+------------------+------+---------+------+----------+--- ...
- 学习Python中遇到的各种错误
错误列表 TypeError : 'moudle' object is not callable 错误:TypeError : 'moudle' object is not callable 代码: ...
- Frameworks.Entity.Core 1
CommonEnums 1系统模块BlockType 2证件类型IDType 3在线支付类型OnLineType 4操作权限,支持位移运算OperatorAuthority 5订单状态: 1000-待 ...
- Spring Cloud Alibaba 之 Sentinel 限流规则和控制台实例
这一节我们通过一个简单的实例,学习Sentinel的基本应用. 一.Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则. ...
- CSS-13-块级元素和行内元素
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CSS-09-背景属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- nginx白名单黑名单设置
nginx白名单黑名单设置 白名单设置,访问根目录 location / { allow 123.34.22.155; allow 33.56.32.1/100; deny all; } 黑名单设置, ...