php 缓存类
来源:互联网 发布:数据挖掘跟gis的关系 编辑:程序博客网 时间:2024/05/24 15:39
在大部份情况下我们的网站都会使用数据库作为站点数据存储的容器。当你执行一个SQL查询时,典型的处理过程 是:连接数据库->准备SQL查询->发送查询到数据库->取得数据库返回结果->关闭数据库连接。但数据库中有些数据是完全静 态的或不太经常变动的,缓存系统会通过把SQL查询的结果缓存到一个更快的存储系统中存储,从而避免频繁操作数据库而很大程度上提高了程序执行时间,而且 缓存查询结果也允许你后期处理。
普遍使用的缓 存技术
数据缓存:这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存 在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存文件中获得。
页面缓存:
每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示 页面并同时生成缓存页面文件,这样下次访问的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
内存缓存:
在里就不介绍了,不是本文所要讨论的,只简单提一下:
Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速 度。
dbcached 是一款基于 Memcached 和 NMDB 的分布式 key-value 数据库内存缓存系统。
以上的缓存技术虽然能很好的解决频繁查询数据库的问题,但其缺点在在于数据无时效性,下面我给出我在项目 中常用的方法:
时间触发缓 存:
检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那 么就用缓存,否则更新缓存。
设定时间内不去判断数据是否要更新,过了设定时间再更新缓存。以上只适合对时效性要求不高的情况下使用 ,否则请看下面。
内容触发缓 存:
当插入数据或更新数据时,强制更新缓存。
在这里我们可以看到,当有大量数据频繁需要更新时,最后都要涉及磁盘读写操作。怎么解决呢?我在日常项目 中,通常并不缓存所有内容,而是缓存一部分不经常变的内容来解决。但在大负荷的情况下,最好要用共享内存做缓存系统。
到这里PHP缓存也许有点解决方案了,但其缺点是,因为每次请求仍然要经过php解析,在大负荷的情况下 效率问题还是比效严重,在这种情况下,也许会用到静态缓存。
静态缓存
这里所说的静态缓存是指HTML缓存,HTML缓存一般是无需判断数据是否要更新的,因为通常在使用 HTML的场合一般是不经常变动内容的页面。数据更新的时候把HTML也强制更新一下就可以了。
实例1:
- <?php
- /*
- 实 例:
- include("cache.php");
- $cache = new cache(30);
- $cache->cacheCheck();
- echo date("Y-m-d H:i:s");
- $cache->caching();
- */
- class cache {
- // 缓存目录
- var $cacheRoot = "./cache/";
- //缓存更新时间秒数,0为不缓存
- var $cacheLimitTime = 0;
- //缓存文件名
- var $cacheFileName = "";
- //缓存扩展名
- var $cacheFileExt = "php";
- /*
- * 构造函数
- * int $cacheLimitTime 缓存更新时间
- */
- function cache( $cacheLimitTime ) {
- if( intval( $cacheLimitTime ) )
- $this->cacheLimitTime = $cacheLimitTime;
- $this->cacheFileName = $this->getCacheFileName();
- ob_start();
- }
- /*
- * 检查缓存文件是否在设置更新时间之内
- * 返回:如果在更新时间之内则返回文件内容,反之则返回失败
- */
- function cacheCheck(){
- if( file_exists( $this->cacheFileName ) ) {
- $cTime = $this->getFileCreateTime( $this->cacheFileName );
- if( $cTime + $this->cacheLimitTime > time() ) {
- echo file_get_contents( $this->cacheFileName );
- ob_end_flush();
- exit;
- }
- }
- return false;
- }
- /*
- * 缓存文件或者输出静态
- * string $staticFileName 静态文件名(含相对路径)
- */
- function caching( $staticFileName = "" ){
- if( $this->cacheFileName ) {
- $cacheContent = ob_get_contents();
- //echo $cacheContent;
- ob_end_flush();
- if( $staticFileName ) {
- $this->saveFile( $staticFileName, $cacheContent );
- }
- if( $this->cacheLimitTime )
- $this->saveFile( $this->cacheFileName, $cacheContent );
- }
- }
- /*
- * 清除缓存文件
- * string $fileName 指定文件名(含函数)或者all(全部)
- * 返回:清除成功返回true,反之返回false
- */
- function clearCache( $fileName = "all" ) {
- if( $fileName != "all" ) {
- $fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;
- if( file_exists( $fileName ) ) {
- return @unlink( $fileName );
- }else return false;
- }
- if ( is_dir( $this->cacheRoot ) ) {
- if ( $dir = @opendir( $this->cacheRoot ) ) {
- while ( $file = @readdir( $dir ) ) {
- $check = is_dir( $file );
- if ( !$check )
- @unlink( $this->cacheRoot . $file );
- }
- @closedir( $dir );
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- /*
- * 根据当前动态文件生成缓存文件名
- */
- function getCacheFileName() {
- return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
- }
- /*
- * 缓存文件建立时间
- * string $fileName 缓存文件名(含相对路径)
- * 返回:文件生成时间秒数,文件不存在返回0
- */
- function getFileCreateTime( $fileName ) {
- if( ! trim($fileName) ) return 0;
- if( file_exists( $fileName ) ) {
- return intval(filemtime( $fileName ));
- }else return 0;
- }
- /*
- * 保存文件
- * string $fileName 文件名(含相对路径)
- * string $text 文件内容
- * 返回:成功返回ture,失败返回false
- */
- function saveFile($fileName, $text) {
- if( ! $fileName || ! $text ) return false;
- if( $this->makeDir( dirname( $fileName ) ) ) {
- if( $fp = fopen( $fileName, "w" ) ) {
- if( @fwrite( $fp, $text ) ) {
- fclose($fp);
- return true;
- }else {
- fclose($fp);
- return false;
- }
- }
- }
- return false;
- }
- /*
- * 连续建目录
- * string $dir 目录字符串
- * int $mode 权限数字
- * 返回:顺利创建或者全部已建返回true,其它方式返回false
- */
- function makeDir( $dir, $mode = "0777" ) {
- if( ! $dir ) return 0;
- $dir = str_replace( "\\", "/", $dir );
- $mdir = "";
- foreach( explode( "/", $dir ) as $val ) {
- $mdir .= $val."/";
- if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
- if( ! file_exists( $mdir ) ) {
- if(!@mkdir( $mdir, $mode )){
- return false;
- }
- }
- }
- return true;
- }
- }
- $cache = new cache(30);
- $cache->cacheCheck();
- echo date("Y-m-d H:i:s");
- $cache->caching();
- ?>
实例2: 数据缓存
- <?php
- class Cache {
- private $dir = 'cache';
- private $expiration = 3600;
- function __construct($dir = '')
- {
- if(!emptyempty($dir)){ $this->dir = $dir; }
- }
- private function _name($key)
- {
- return sprintf("%s/%s", $this->dir, sha1($key));
- }
- public function get($key, $expiration = '')
- {
- if ( !is_dir($this->dir) OR !is_writable($this->dir))
- {
- return FALSE;
- }
- $cache_path = $this->_name($key);
- if (!@file_exists($cache_path))
- {
- return FALSE;
- }
- $expiration = emptyempty($expiration) ? $this->expiration : $expiration;
- if (filemtime($cache_path) < (time() - $expiration))
- {
- $this->clear($key);
- return FALSE;
- }
- if (!$fp = @fopen($cache_path, 'rb'))
- {
- return FALSE;
- }
- flock($fp, LOCK_SH);
- $cache = '';
- if (filesize($cache_path) > 0)
- {
- $cache = unserialize(fread($fp, filesize($cache_path)));
- }
- else
- {
- $cache = NULL;
- }
- flock($fp, LOCK_UN);
- fclose($fp);
- return $cache;
- }
- public function set($key, $data)
- {
- if ( !is_dir($this->dir) OR !is_writable($this->dir))
- {
- $this->_makeDir($this->dir);
- }
- $cache_path = $this->_name($key);
- if ( ! $fp = fopen($cache_path, 'wb'))
- {
- return FALSE;
- }
- if (flock($fp, LOCK_EX))
- {
- fwrite($fp, serialize($data));
- flock($fp, LOCK_UN);
- }
- else
- {
- return FALSE;
- }
- fclose($fp);
- @chmod($cache_path, 0777);
- return TRUE;
- }
- public function clear($key)
- {
- $cache_path = $this->_name($key);
- if (file_exists($cache_path))
- {
- unlink($cache_path);
- return TRUE;
- }
- return FALSE;
- }
- public function clearAll()
- {
- $dir = $this->dir;
- if (is_dir($dir))
- {
- $dh=opendir($dir);
- while (false !== ( $file = readdir ($dh)))
- {
- if($file!="." && $file!="..")
- {
- $fullpath=$dir."/".$file;
- if(!is_dir($fullpath)) {
- unlink($fullpath);
- } else {
- delfile($fullpath);
- }
- }
- }
- closedir($dh);
- // rmdir($dir);
- }
- }
- private function _makeDir( $dir, $mode = "0777" ) {
- if( ! $dir ) return 0;
- $dir = str_replace( "\\", "/", $dir );
- $mdir = "";
- foreach( explode( "/", $dir ) as $val ) {
- $mdir .= $val."/";
- if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
- if( ! file_exists( $mdir ) ) {
- if(!@mkdir( $mdir, $mode )){
- return false;
- }
- }
- }
- return true;
- }
- }
- PHP缓存, SimpleCache 类
- php缓存类
- php的缓存类
- PHP缓存类
- PHP快速缓存类
- PHP-数据缓存类
- php缓存类
- php缓存类
- php 缓存类
- PHP 缓存类
- PHP缓存操作类
- php缓存类
- php文件缓存类
- php文件缓存类
- PHP缓存操作类
- php+redis缓存类
- php 文件缓存类
- PHP文件缓存类
- for循环正确遍历数组
- 数据库连接池
- NLP-python自然语言工具之nltk
- 谈一谈拦截导航控制器返回事件(上)——OPP
- 开发必备---你应该知道的一些 ADB 命令 144 作者 LooperJing 关注 2016.11.27 11:45* 字数 1548 阅读 1106评论 2喜欢 39 版权声明:本文为Loop
- php 缓存类
- Vue的批量删除
- html中常用的标签-表单标签
- JNDI
- 使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本
- ViewPager系列之 仿魅族应用的广告BannerView
- 淘淘商城系列——生成订单
- 利用一个Demo说明Castle+NHibernate的实现
- AngularJS 入门5-依赖注入