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缓存扩展, ...
随机推荐
- stars-one原创工具——蓝奏云批量下载工具
一款可以批量下载蓝奏云分享的文件夹下的所有文件 基于HtmlUnit和okhttp开源库,所以打包后的jar包文件有点大 蓝奏云下载地址 github地址 需求 之前找电子书资源的时候,网友分享的蓝奏 ...
- Spring-cloud微服务实战【六】:接口服务feign
在上一篇文章中,我们使用了ribbon进行负载均衡,但是仔细思考一下,我们的请求封装和调用以及结果的返回都是我们自己编码完成的,如果需要调用的接口很多,那么无疑开发量是比较大的,那有没有比较好的方式呢 ...
- 高通量计算框架HTCondor(五)——分布计算
目录 1. 正文 1.1. 任务描述文件 1.2. 提交任务 1.3. 返回结果 2. 相关 1. 正文 1.1. 任务描述文件 前文提到过,HTCondor是通过condor_submit命令将提交 ...
- 博客与微信小程序的同步
在此之前,先说说自己最近的打算,才购买了阿里云的服务器,想做一个网站和图床网盘之类的方便自己使用. 考虑到小程序,又打算将自己的博客内容放到小程序中.从零开发实属困难,应该还要一段时间才能完成. 目前 ...
- [ZJOI2008]树的统计(树链剖分)
[ZJOI2008]树的统计(luogu) Description 一棵树上有 n 个节点,编号分别为 1 到 n,每个节点都有一个权值 w.我们将以下面的形式来要求你对这棵树完成一些操作: I. C ...
- linux--->redis php扩展安装
阿里云centos6.9下 redis php扩展安装 下载phpredis wget http://pecl.php.net/get/redis-3.1.0.tgz 或 wget https://g ...
- Linux测试环境简单使用教程
0. 本blog 简单说明一下 Linux测试环境尤其是 CentOS测试环境的开发测试使用, 教程可能不会很长, 主要是入门. 0.1 Linux简介: Linux 的历史基本上不用阐述, linu ...
- geoserver wfs属性查询
Geoserver参考连接:http://docs.geoserver.org/latest/en/user/services/wfs/reference.html 使用实例: http://loca ...
- Linux文件内容重定向
文件描述符 可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件的读写操作. 用户可以自定义文件描述符范围是:3-num,这个最大数字, ...
- springBoot 启动没有数据库配置报错
在没有配置数据库的时候, 直接启动springBoot 项目 会有报错 Description: Failed to configure a DataSource: 'url' attribute i ...