缓存分为:数据缓存,页面缓存,内存缓存(memcache,redis)

ob,输出缓冲区,是output buffering的简称

FileCache.php

 <?php
//本文件用来存储和读取文件中的数据
class FileCache{
//使用单例(一个静态变量->保存对象;一个静态方法->判断是否属于自身)
private static $config;//用来存储配置信息
private static $obj;
private function __construct() {//禁止外部实例化对象
self::$config= include 'filecache_config.php';
}
private function __clone() {//禁止外部复制对象 }
public static function getinstance(){ //静态方法
if(!(self::$obj instanceof self)){
self::$obj=new self;
}
return self::$obj;
}
//存储文件缓存,设置两个值 文件名和数据
public function setcache($key,$data){
$path=self::$config['cache_path'].$key.'.php';
$str=var_export($data,TRUE);//var_export()把数组转换成数组格式的字符串。
$content="<?php\r\nreturn ".$str.";\r\n?>";// \r是回车,使光标回到行首;\n是换行,使光标移动到下一行
file_put_contents($path,$content);//存储文件(路径,内容)
}
//获取缓存文件
public function getcache($key){
$path=self::$config['cache_path'].$key.'.php';
if(!file_exists($path)){//判断文件是否存在
return FALSE;
}
if((time()-filemtime($path))>self::$config['cache_time']){//判断时间是否超时,filemtime()取得文件的修改时间
unlink($path);//删除文件用unlink
return FALSE;
}
$data=include $path;
return $data;
}
}
//$obj= FileCache::getinstance();
////$obj->setcache(1,['aa'=>5]);
//$content=$obj->getcache(1);
//var_dump($content);

filecache_config.php

 <?php
return array(
'cache_path'=>'cache/',// 设置缓存存储目录,cache文件夹
'cache_time'=>60 // 设置缓存保存时间60秒
);

fruitclass.php

 <?php

 class Fruit{
private $db;//成员变量
private $cache;
public function __construct() {
include '../single/db_mysql single.php';
include '../Cache/FileCache.php';
$this->db= db_mysql::getinstance();//打开了数据库连接。
$this->cache= FileCache::getinstance();
}
public function listdata(){
$key=md5($_SERVER['REQUEST_URI']);
$data=$this->cache->getcache($key);//获取缓存
if(!$data){//判断缓存文件是否存在(时间是否超时),如果不存在,要去数据库中查询数据
$data=$this->db->getlist("*","fruit",array("fruit_name"=>'苹果'));//调用getlist方法。从数据库查询
$this->cache->setcache($key,$data);//存储缓存
}
return $data;
}
}
$fruit=new Fruit();
echo "<pre>";
var_dump($fruit->listdata());

上边输出的结果为:(即获取到的缓存)

 <?php
return array (
0 =>
array (
'id' => '118',
'supplier_id' => '2',
'fruit_name' => '苹果',
'price' => '2.00',
),
1 =>
array (
'id' => '114',
'supplier_id' => '1',
'fruit_name' => '苹果',
'price' => '3.00',
),
2 =>
array (
'id' => '117',
'supplier_id' => '2',
'fruit_name' => '苹果',
'price' => '3.00',
),
);
?>

db_mysql single.php

 <?php
class db_mysql{// 单例中包含三个私有属性(一个静态变量,一个构造函数,一个克隆方法),一个公共静态方法。三私一公
private static $instance;// 用来存储数据库连接
private $pdo;
private function __construct(){// 私有构造函数,定义成private型,防止外部实例化对象
//echo 11;
$this->con("my_blog","root","123456");//连接数据库
}
private function __clone(){
//定义成私有的是防止对象被克隆,克隆是可以改变参数。如果$db4=$db;这是赋值操作,不能改变参数
echo 22;
}
private function con($dbname,$username,$password){//数据库连接,三种方式:mysql_connet mysqli pdo
try {
$this->pdo=new PDO("mysql:host=localhost;dbname=$dbname",$username,$password);// 创建pdo连接对象
echo "连接成功";
} catch (PDOException $ex) {
echo $ex->getMessage();
}
}
public static function getinstance(){
if(!(self::$instance instanceof self)){// self代表自身(本身是一个对象),判断$instance的值属不属于这个对象,
self::$instance=new self;// self代表 db_mysql
}
return self::$instance;
}
public function insert($data){// 添加功能
if(!is_array($data)){
return FALSE;
}
$fields= array_keys($data);
$val=array_values($data);
// var_dump($val);//数组
$str=array_walk($val,array($this,'parsestr'));// 通过循环方式,循环数组中的每一个值
//foreach($val as $k=>$v){$val[$k]=parsestr($v)};
//array_walk 相当于一个while循环,array($this,'')是一个数组,parsestr是一个回调函数
// echo "<pre>";
// var_dump($val);//数组
$str=implode(",", $val);
// echo "<br>";
// echo "<pre>";
// echo $str;
$fields= implode(",", $fields);
$sql="insert into fruit ($fields) values ($str) ";
echo $sql;
echo "<br>";
$num=$this->pdo->exec($sql);
echo $num;
echo "<br>";
$id= $this->pdo->lastInsertId();
echo $id;
}
public function getlist($fileds,$table,$condition){//$fileds 字段 $table表名 $condition 查询条件
if(!is_array($condition)){
return FALSE;
}
$where="1=1";
foreach ($condition as $key => $val){
$where .=" and $key ='".$val."'";
}
$sql="select ".$fileds." from ".$table." where ".$where;
$result=$this->pdo->query($sql);
if($result){//判断$result,是否有值
return $result->fetchall(PDO::FETCH_ASSOC);
}
} public function parsestr(&$val){//引用
$val= "'".$val."'";// $val[$k]=
// echo "<pre>";
// echo $val;//字符串
} }
//$b=new db_mysql();
$db= db_mysql::getinstance();
echo "<br>";
//$db2= db_mysql::getinstance();
echo "<br>";
//$db3= clone $db;//克隆
$db->insert(['fruit_name'=>'葡萄','supplier_id'=>2]);
echo "<br>";
$a=$db->getlist("*",'fruit',array('fruit_name'=>'苹果'));
echo "<br>";
echo "<pre>";
var_dump($a);

缓冲区的概念:(可用于页面缓存,即每次访问页面时,就检测相应的页面缓存是否存在,若不存在,则查询数据库得到相应的数据,同时生成缓存页面,这样,在下次访问时就可以直接取出缓存页面,不必再次查询数据库)

PHP文件(输入或输出的内容)->缓冲区(内存)->apache->浏览器

会引起缓冲区刷新的操作有:

1、PHP程序执行完毕。

2、缓冲区的大小超过了php.ini配置文件中设置的output_buffering的值(大小为4kb)。

3、ob_flush() 或 flush() 函数被调用时。

ob_flush();强制刷新缓冲区,把缓冲区的内容发送到Apache服务器。

flush();刷新Apache服务器,把Apache中的内容发送到浏览器,和ob_flush();要同时用。

echo,print_r,var_dump输出的内容先存到缓冲区,再通过服务器发送到浏览器。

 if(!file_exists('aa.html')){
//phpcms中的生成首页就是这个功能
echo 11;
$username = '张三';
ob_start(); //打开缓冲区(内存)
include '../d/d2-4form/form.html'; //加载动态页面,在内存中(缓冲区),(要把这个页面从内存中取出,保存下来)
$str = ob_get_contents(); //获取缓冲区中的内容,并且以字符串格式返回
file_put_contents('aa.html', $str); //保存内容
ob_end_flush();//关闭缓冲区,把PHP缓冲区的内容发送给服务器,并清除PHP缓冲区中的内容
// ob_end_clean(); //直接清空PHP缓冲区的内容,并且关闭缓冲区
}else{
include 'aa.html';//下次读取的时候直接读取静态页面
}

aa.html

 <!DOCTYPE html>
<html>
<head>
<title>表单 demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<form action="get_form.php" method="post">
<p>
用户:<input type="text" name="user" size="30" maxlength="2" value="张三" >
</p>
<P>
文件:<input type="file" name="file">
</P>
<button>登录</button>
</form>
</div>
</body>
</html>

form.html

 <!DOCTYPE html>
<html>
<head>
<title>表单 demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<form action="get_form.php" method="post">
<p>
用户:<input type="text" name="user" size="30" maxlength="2" value="<?php echo $username; ?>" >
</p>
<P>
文件:<input type="file" name="file">
</P>
<button>登录</button>
</form>
</div>
</body>
</html>

cache缓存的更多相关文章

  1. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  2. Windows Azure Cloud Service (43) 使用Azure In-Role Cache缓存(2)Dedicated Role

    <Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...

  3. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  4. paip.cache 缓存架构以及性能提升总结

    paip.cache 缓存架构以及性能提升总结 1         缓存架构以及性能(贯穿读出式(LookThrough) 旁路读出式(LookAside) 写穿式(WriteThrough) 回写式 ...

  5. Cache缓存对象缓存对象

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoCache.aspx ...

  6. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  7. MemCache缓存和C#自带的Cache缓存

    1.MemCache: //初始化 static SockIOPool _pool; // 创建Memcached private static MemcachedClient Create(stri ...

  8. google guava cache缓存基本使用讲解

    代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...

  9. Django学习之十二:Cache 缓存组件

    目录 Django Cache 缓存组件 缓存逻辑伪代码 配置缓存源 可配置参数说明 01. Django的默认缓存 02. 基于Redis的django-redis 03. 自定义cache 04. ...

  10. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...

随机推荐

  1. css中的background属性

    第一次写博客,我就写写今天在编写网页的过程中,对background的两种运用,一是background中的线性渐变,对背景的渐变我其实是很少使用的,所以今天在写的时候我用css3的帮助手册,back ...

  2. CSAW2013

    竞赛地址:https://ctf.isis.poly.edu/challenges/ 第一关:Trivia Trivia意思为琐事,每题分值50,比较简单 1.Drink all the booze, ...

  3. Learn RxJava

    Learn RxJava http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wi ...

  4. Android Content Provider Guides

    Android Content Provider Guides Content Providers管理对结构化数据集的访问.它们包装数据,并且提供一种定义数据安全的机制. Content provid ...

  5. Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端

    Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端 本博客前面博文中利用org.apache.http包中API进行Android客户端HTTP连接的例子 ...

  6. ParagraphString - 段落样式的简易处理

    ParagraphString - 段落样式的简易处理 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 Paragrap ...

  7. Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  8. 【转发】网易邮箱前端技术分享之javascript编码规范

    网易邮箱前端技术分享之javascript编码规范 发布日期:2013-11-26 10:06 来源:网易邮箱前端技术中心 作者:网易邮箱 点击:533 网易邮箱是国内最早使用ajax技术的邮箱.早在 ...

  9. NGINX高性能Web服务器详解(读书笔记)

    原文地址:NGINX高性能Web服务器详解(读书笔记) 作者:夏寥寥 第4章  Nginx服务器的高级配置 4.1 针对IPv4的内核7个参数的配置优化 说明:我们可以将这些内核参数的值追加到Linu ...

  10. 【JavaScript】EasyUI框架的Dialog控件根据浏览器分辨率自动调节宽高

    序: 如果单独一个或几个Dialog控件修改成根据浏览器可视界面自动调整高.宽很容易仅仅是一个量变的过程,但如果大量页面都引入了Dialog控件,修改起来是一个很消耗体力的工作.所以接到任务后第一想法 ...