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 缓存的更多相关文章

  1. File缓存

    /**      * 保存对象      * @param ser      * @param file      * @throws IOException      */     public b ...

  2. Laravel之路——file缓存修改为redis缓存

    1.Session: 修改.evn文件: SESSION_DRIVER:redis (如果还不行的话,修改config/session.php的driver) 2.缓存修改为redis 注意:使用 L ...

  3. django 缓存、中间件、信号、CSRF 详解

    中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项 ...

  4. php使用文件缓存

    使用php读取mysql中的数据很简单,数据量不大的时候,mysql的性能还是不错的.但是有些查询可能比较耗时,这时可以把查询出的结果,缓存起来,减轻mysql的查询压力. 缓存的方法有几种:使用me ...

  5. ThinkPHP的缓存技术

    原文:ThinkPHP的缓存技术 如果没有缓存的网站是百万级或者千万级的访问量,会给数据库或者服务器造成很大的压力,通过缓存,大幅减少服务器和数据库的负荷.假如我们 把读取数据的过程分为三个层,第一个 ...

  6. Discuz的缓存体系

    参考文档:<http://dev.discuz.org/wiki/index.php?title=缓存机制> Discuz中涉及数据缓存的地方有: 1. session Dz的sessio ...

  7. php 文件缓存

    http://www.oschina.net/code/snippet_162279_6098 <?php class cache {       private static $_instan ...

  8. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  9. php 缓存 加速缓存

    PHP四大加速缓存器opcache,apc,xcache,eAccelerator eAccelerator,xcache,opcache,apc(偏数据库缓存,分系统和用户缓存)是PHP缓存扩展, ...

随机推荐

  1. redis 其他操作

    1.设定服务端密码 1.1.编辑 redis的配置文件 [root@centos7 ~]# vim /usr/local/redis/etc/redis.conf requirepass 123 # ...

  2. MySQL8.0.19安装

    官网下载安装包:mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz 安装环境:CentOS Linux release 7.5.1804 (Core) 解压安装包: ...

  3. Excel-条件格式

    今天运用了一下条件格式中的自建规则进行公式筛选, 设置格式那里一定要将$P$8修改为$P8 然后双击修改后的第一项进行单元格的自动填充

  4. MySql查看修改l时区

    # 查看时区 show variables like '%time_zone%'; # 设置全局 set global time_zone='+8:00';  # 设置当前会话 set time_zo ...

  5. 3种基础的 REST 安全机制

    安全是 RESTful web service 的基石,我们主要讨论以下3种主要的方法: Basic authentication Oauth 2.0 Oauth 2.0 + JWT 1. Basic ...

  6. DSSA特定领域软件体系结构

    一.何为DSSA 特定领域软件架构(Domain Specific Software Architecture,DSSA)是一种有效实现特定领域软件重用的手段.简单地说,DSSA就是在一个特定应用领域 ...

  7. Windows Server 2016 Active Directory 图文搭建指南

    1. 首先打开Manage --> Add Roles and Features 2. 点击Next 3. 不做修改,点击Next 4. 不做修改,点击Next 5. 选择Active Dire ...

  8. (ES6)JavaScript中面向对象类的实现

    在ES5中,我们就知道JS是不支持面向对象的,所以用函数模拟了一个构造函数来实现类的.那么在ES6中,在ES5的原理基础上,将代码书写更为简单,明了. 一.怎样用ES6创建类?首先看一看例子: cla ...

  9. Java多线程:实现API接口创建线程方式详解

    先看例子: /**实现Runnable接口创建线程步骤: * 1.创建一个实现Runnable接口的类 * 2.重写Runnable类中抽象的run()方法 * 3.创建实现类的对象 * 4.声明Th ...

  10. Selenium(一):元素定位

    一.Selenium 8种定位方式 baidu.html <form id="form" name="f" action="/s" c ...