数据缓存:这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。
用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。
举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。
页面缓存:
每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问的时候页面文件就发挥作用了。(模板引擎和网上常见的一些PHP缓存机制类通常有此功能)
时间触发缓存:
检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。
内容触发缓存:
当插入数据或更新数据时,强制更新PHP缓存机制。
静态缓存:
这里所说的静态缓存是指静态化,直接生成HTML或XML等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。
以上内容是代码级的解决方案,我直接CP别的框架,也懒得改,内容都差不多,很容易就做到,而且会几种方式一起用,但下面的内容是服务器端的缓存方案,非代码级的,要有多方的合作才能做到
内存缓存:
Memcached是高性能的,分布式的内存对象PHP缓存机制系统,用于在动态应用中减少数据库负载,提升访问速度。
php的缓冲器:
有eaccelerator, apc, phpa,xcache,这个这个就不说了吧,搜索一堆一堆的,自己看啦,知道有这玩意就OK
MYSQL缓存:
	这也算非代码级的,经典的数据库就是用的这种方式,看下面的运行时间,0.09xxx之类的
我贴段根据蓝色那家伙修改后部分my.ini吧,2G的MYISAM表可以在0.05S左右,据说他前后改了有快一年
基于反向代理的Web缓存:
	如Nginx,SQUID,mod_proxy(apache2以上又分为mod_proxy和mod_cache)
NGINX的例子
用google找到一些 php缓存技术方法
发个PHP缓存实现,实现了apc和文件缓存,继承Cache_Abstract即可实现调用第三方的缓存工具。
参考shindig的缓存类和apc。
	
| 
						 
							1
						 
						
							2
						 
						
							3
						 
						
							4
						 
						
							5
						 
						
							6
						 
						
							7
						 
						
							8
						 
						
							9
						 
						
							10
						 
						
							11
						 
						
							12
						 
						
							13
						 
						
							14
						 
						
							15
						 
						
							16
						 
						
							17
						 
						
							18
						 
						
							19
						 
						
							20
						 
						
							21
						 
						
							22
						 
						
							23
						 
						
							24
						 
						
							25
						 
						
							26
						 
						
							27
						 
						
							28
						 
						
							29
						 
						
							30
						 
						
							31
						 
						
							32
						 
						
							33
						 
						
							34
						 
						
							35
						 
						
							36
						 
						
							37
						 
						
							38
						 
						
							39
						 
						
							40
						 
						
							41
						 
						
							42
						 
						
							43
						 
						
							44
						 
						
							45
						 
						
							46
						 
						
							47
						 
						
							48
						 
						
							49
						 
						
							50
						 
						
							51
						 
						
							52
						 
						
							53
						 
						
							54
						 
						
							55
						 
						
							56
						 
						
							57
						 
						
							58
						 
						
							59
						 
						
							60
						 
						
							61
						 
						
							62
						 
						
							63
						 
						
							64
						 
						
							65
						 
						
							66
						 
						
							67
						 
						
							68
						 
						
							69
						 
						
							70
						 
						
							71
						 
						
							72
						 
						
							73
						 
						
							74
						 
						
							75
						 
						
							76
						 
						
							77
						 
						
							78
						 
						
							79
						 
						
							80
						 
						
							81
						 
						
							82
						 
						
							83
						 
						
							84
						 
						
							85
						 
						
							86
						 
						
							87
						 
						
							88
						 
						
							89
						 
						
							90
						 
						
							91
						 
						
							92
						 
						
							93
						 
						
							94
						 
						
							95
						 
						
							96
						 
						
							97
						 
						
							98
						 
						
							99
						 
						
							100
						 
						
							101
						 
						
							102
						 
						
							103
						 
						
							104
						 
						
							105
						 
						
							106
						 
						
							107
						 
						
							108
						 
						
							109
						 
						
							110
						 
						
							111
						 
						
							112
						 
						
							113
						 
						
							114
						 
						
							115
						 
						
							116
						 
						
							117
						 
						
							118
						 
						
							119
						 
						
							120
						 
						
							121
						 
						
							122
						 
						
							123
						 
						
							124
						 
						
							125
						 
						
							126
						 
						
							127
						 
						
							128
						 
						
							129
						 
						
							130
						 
						
							131
						 
						
							132
						 
						
							133
						 
						
							134
						 
						
							135
						 
						
							136
						 
						
							137
						 
						
							138
						 
						
							139
						 
						
							140
						 
						
							141
						 
						
							142
						 
						
							143
						 
						
							144
						 
						
							145
						 
						
							146
						 
						
							147
						 
						
							148
						 
						
							149
						 
						
							150
						 
						
							151
						 
						
							152
						 
						
							153
						 
						
							154
						 
						
							155
						 
						
							156
						 
						
							157
						 
						
							158
						 
						
							159
						 
						
							160
						 
						
							161
						 
						
							162
						 
						
							163
						 
						
							164
						 
						
							165
						 
						
							166
						 
						
							167
						 
						
							168
						 
						
							169
						 
						
							170
						 
						
							171
						 
						
							172
						 
						
							173
						 
						
							174
						 
						
							175
						 
						
							176
						 
						
							177
						 
						
							178
						 
						
							179
						 
						
							180
						 
						
							181
						 
						
							182
						 
						
							183
						 
						
							184
						 
						
							185
						 
						
							186
						 
						
							187
						 
						
							188
						 
						
							189
						 
						
							190
						 
						
							191
						 
						
							192
						 
						
							193
						 
						
							194
						 
						
							195
						 
						
							196
						 
						
							197
						 
						
							198
						 
						
							199
						 
						
							200
						 
						
							201
						 
						
							202
						 
						
							203
						 
						
							204
						 
						
							205
						 
						
							206
						 
						
							207
						 
						
							208
						 
						
							209
						 
						
							210
						 
						
							211
						 
						
							212
						 
						
							213
						 
						
							214
						 
						
							215
						 
						
							216
						 
						
							217
						 
						
							218
						 
						
							219
						 
						
							220
						 
						
							221
						 
						
							222
						 
						
							223
						 
						
							224
						 
						
							225
						 
						
							226
						 
						
							227
						 
						
							228
						 
						
							229
						 
						
							230
						 
						
							231
						 
						
							232
						 
						
							233
						 
						
							234
						 
						
							235
						 
						
							236
						 
						
							237
						 
						
							238
						 
						
							239
						 
						
							240
						 
						
							241
						 
						
							242
						 
						
							243
						 
						
							244
						 
						
							245
						 
						
							246
						 
						
							247
						 
						
							248
						 
						
							249
						 
						
							250
						 
						
							251
						 
						
							252
						 
						
							253
						 
						
							254
						 
						
							255
						 
						
							256
						 
						
							257
						 
						
							258
						 
						
							259
						 
						
							260
						 
						
							261
						 
						
							262
						 
						
							263
						 
						
							264
						 
						
							265
						 
						
							266
						 
						
							267
						 
						
							268
						 
						
							269
						 
						
							270
						 
						
							271
						 
						
							272
						 
						
							273
						 
						
							274
						 
						
							275
						 
						
							276
						 
						
							277
						 
						
							278
						 
						
							279
						 
						
							280
						 
						
							281
						 
						
							282
						 
						
							283
						 
						
							284
						 
						
							285
						 
						
							286
						 
						
							287
						 
						
							288
						 
						
							289
						 
						
							290
						 
						
							291
						 
						
							292
						 
						
							293
						 
						
							294
						 
						
							295
						 
						
							296
						 
						
							297
						 
						
							298
						 
						
							299
						 
						
							300
						 
						
							301
						 
						
							302
						 
						
							303
						 
						
							304
						 
						
							305
						 
						
							306
						 
						
							307
						 
						
							308
						 
						
							309
						 
						
							310
						 
						
							311
						 
						
							312
						 
						
							313
						 
						
							314
						 
						
							315
						 
						
							316
						 
						
							317
						 
						
							318
						 
						
							319
						 
						
							320
						 
						
							321
						 
						
							322
						 
						
							323
						 
						
							324
						 
						
							325
						 
						
							326
						 
						
							327
						 
						
							328
						 
						
							329
						 
						
							330
						 
						
							331
						 
						
							332
						 
						
							333
						 
						
							334
						 
						
							335
						 
					 | 
					
						 
								<?php  
							 
							
								classCacheExceptionextendsException {}  
							 
							
								/** 
							 
							
								 * <a href="http://www.php1.cn/category/79.html">缓存</a>抽象类 
							 
							
								 */  
							 
							
								abstractclassCache_Abstract {  
							 
							
								    /** 
							 
							
								     * 读<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return mixed 
							 
							
								     */  
							 
							
								    abstractpublicfunctionfetch($key);  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>变量下标 
							 
							
								     * @param string $value <a href="http://www.php1.cn/category/79.html">缓存</a>变量的值 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    abstractpublicfunctionstore($key,$value);  
							 
							
								    /** 
							 
							
								     * 删除<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return Cache_Abstract 
							 
							
								     */  
							 
							
								    abstractpublicfunctiondelete($key);  
							 
							
								    /** 
							 
							
								     * 清(删)除所有<a href="http://www.php1.cn/category/79.html">缓存</a> 
							 
							
								     * 
							 
							
								     * @return Cache_Abstract 
							 
							
								     */  
							 
							
								    abstractpublicfunctionclear();  
							 
							
								    /** 
							 
							
								     * 锁定<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return Cache_Abstract 
							 
							
								     */  
							 
							
								    abstractpublicfunctionlock($key);  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>变量解锁 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return Cache_Abstract 
							 
							
								     */  
							 
							
								    abstractpublicfunctionunlock($key);  
							 
							
								    /** 
							 
							
								     * 取得<a href="http://www.php1.cn/category/79.html">缓存</a>变量是否被锁定 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    abstractpublicfunctionisLocked($key);  
							 
							
								    /** 
							 
							
								     * 确保不是锁定状态 
							 
							
								     * 最多做$tries次睡眠等待解锁,超时则跳过并解锁 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     */  
							 
							
								    publicfunctioncheckLock($key) {  
							 
							
								        if(!$this->isLocked($key)) {  
							 
							
								            return$this;  
							 
							
								        }  
							 
							
								        $tries= 10;  
							 
							
								        $count= 0;  
							 
							
								        do{  
							 
							
								            usleep(200);  
							 
							
								            $count++;  
							 
							
								        }while($count<=$tries&&$this->isLocked($key)); // 最多做十次睡眠等待解锁,超时则跳过并解锁  
							 
							
								        $this->isLocked($key) &&$this->unlock($key);  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								}  
							 
							
								/** 
							 
							
								 * APC扩展<a href="http://www.php1.cn/category/79.html">缓存</a>实现 
							 
							
								 *  
							 
							
								 *  
							 
							
								 * @category   Mjie 
							 
							
								 * @package    Cache 
							 
							
								 * @author     流水孟春 
							 
							
								 * @copyright  Copyright (c) 2008- <cmpan(at)qq.com> 
							 
							
								 * @license    New BSD License 
							 
							
								 * @version    $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $ 
							 
							
								 */  
							 
							
								classCache_ApcextendsCache_Abstract {  
							 
							
								    protected$_prefix='cache.mjie.net';  
							 
							
								    publicfunction__construct() {  
							 
							
								        if(!function_exists('apc_cache_info')) {  
							 
							
								            thrownewCacheException('apc extension didn't installed');  
							 
							
								        }  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 保存<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @param mixed $value 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    publicfunctionstore($key,$value) {  
							 
							
								        returnapc_store($this->_storageKey($key),$value);  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 读取<a href="http://www.php1.cn/category/79.html">缓存</a> 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return mixed 
							 
							
								     */  
							 
							
								    publicfunctionfetch($key) {  
							 
							
								        returnapc_fetch($this->_storageKey($key));  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 清除<a href="http://www.php1.cn/category/79.html">缓存</a> 
							 
							
								     * 
							 
							
								     * @return Cache_Apc 
							 
							
								     */  
							 
							
								    publicfunctionclear() {  
							 
							
								        apc_clear_cache();  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 删除<a href="http://www.php1.cn/category/79.html">缓存</a>单元 
							 
							
								     * 
							 
							
								     * @return Cache_Apc 
							 
							
								     */  
							 
							
								    publicfunctiondelete($key) {  
							 
							
								        apc_delete($this->_storageKey($key));  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>单元是否被锁定 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    publicfunctionisLocked($key) {  
							 
							
								        if((apc_fetch($this->_storageKey($key) .'.lock')) === false) {  
							 
							
								            returnfalse;  
							 
							
								        }  
							 
							
								        returntrue;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 锁定<a href="http://www.php1.cn/category/79.html">缓存</a>单元 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return Cache_Apc 
							 
							
								     */  
							 
							
								    publicfunctionlock($key) {  
							 
							
								        apc_store($this->_storageKey($key) .'.lock','', 5);  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>单元解锁 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return Cache_Apc 
							 
							
								     */  
							 
							
								    publicfunctionunlock($key) {  
							 
							
								        apc_delete($this->_storageKey($key) .'.lock');  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 完整<a href="http://www.php1.cn/category/79.html">缓存</a>名 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return string 
							 
							
								     */  
							 
							
								    privatefunction_storageKey($key) {  
							 
							
								        return$this->_prefix .'_'.$key;  
							 
							
								    }  
							 
							
								}  
							 
							
								/** 
							 
							
								 * 文件<a href="http://www.php1.cn/category/79.html">缓存</a>实现 
							 
							
								 *  
							 
							
								 *  
							 
							
								 * @category   Mjie 
							 
							
								 * @package    Cache 
							 
							
								 * @author     流水孟春 
							 
							
								 * @copyright  Copyright (c) 2008- <cmpan(at)qq.com> 
							 
							
								 * @license    New BSD License 
							 
							
								 * @version    $Id: Cache/File.php 版本号 2010-04-18 16:46 cmpan $ 
							 
							
								 */  
							 
							
								classCache_FileextendsCache_Abstract {  
							 
							
								    public$useSubdir    = false;  
							 
							
								    protected$_cachesDir='cache';  
							 
							
								    publicfunction__construct() {  
							 
							
								        if(defined('DATA_DIR')) {  
							 
							
								            $this->_setCacheDir(DATA_DIR .'/cache');  
							 
							
								        }  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 获取<a href="http://www.php1.cn/category/79.html">缓存</a>文件 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return string 
							 
							
								     */  
							 
							
								    protectedfunction_getCacheFile($key) {  
							 
							
								        $subdir=$this->useSubdir ?substr($key, 0, 2) .'/':'';  
							 
							
								        return$this->_cachesDir .'/'.$subdir.$key.'.php';  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 读取<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 为防止信息泄露,<a href="http://www.php1.cn/category/79.html">缓存</a>文件格式为php文件,并以"<?php exit;?>"开头 
							 
							
								     *  
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return mixed 
							 
							
								     */  
							 
							
								    publicfunctionfetch($key) {  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        if(file_exists($cacheFile) &&is_readable($cacheFile)) {  
							 
							
								            // include 方式  
							 
							
								            //return include $cacheFile;  
							 
							
								            // 系列化方式  
							 
							
								            returnunserialize(@file_get_contents($cacheFile, false, NULL, 13));  
							 
							
								        }  
							 
							
								        returnfalse;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 为防止信息泄露,<a href="http://www.php1.cn/category/79.html">缓存</a>文件格式为php文件,并以"<?php exit;?>"开头 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>变量下标 
							 
							
								     * @param string $value <a href="http://www.php1.cn/category/79.html">缓存</a>变量的值 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    publicfunctionstore($key,$value) {  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        $cacheDir = dirname($cacheFile);  
							 
							
								        if(!is_dir($cacheDir)) {  
							 
							
								            if(!@mkdir($cacheDir, 0755, true)) {  
							 
							
								                thrownewCacheException("Could not make cache directory");  
							 
							
								            }  
							 
							
								        }  
							 
							
								    // 用include方式  
							 
							
								        //return @file_put_contents($cacheFile, '<?php return ' . var_export($value, true). ';');  
							 
							
								        return@file_put_contents($cacheFile,'<?php exit;?>'. serialize($value));  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 删除<a href="http://www.php1.cn/category/79.html">缓存</a>变量 
							 
							
								     * 
							 
							
								     * @param string $key <a href="http://www.php1.cn/category/79.html">缓存</a>下标 
							 
							
								     * @return Cache_File 
							 
							
								     */  
							 
							
								    publicfunctiondelete($key) {  
							 
							
								        if(emptyempty($key)) {  
							 
							
								            thrownewCacheException("Missing argument 1 for Cache_File::delete()");  
							 
							
								        }  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        if(!@unlink($cacheFile)) {  
							 
							
								            thrownewCacheException("Cache file could not be deleted");  
							 
							
								        }  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * <a href="http://www.php1.cn/category/79.html">缓存</a>单元是否已经锁定 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return bool 
							 
							
								     */  
							 
							
								    publicfunctionisLocked($key) {  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        clearstatcache();  
							 
							
								        returnfile_exists($cacheFile.'.lock');  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 锁定 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return Cache_File 
							 
							
								     */  
							 
							
								    publicfunctionlock($key) {  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        $cacheDir = dirname($cacheFile);  
							 
							
								        if(!is_dir($cacheDir)) {  
							 
							
								            if(!@mkdir($cacheDir, 0755, true)) {  
							 
							
								                if(!is_dir($cacheDir)) {  
							 
							
								                    thrownewCacheException("Could not make cache directory");  
							 
							
								                }  
							 
							
								            }  
							 
							
								        }  
							 
							
								        // 设定<a href="http://www.php1.cn/category/79.html">缓存</a>锁文件的访问和修改时间  
							 
							
								        @touch($cacheFile.'.lock');  
							 
							
								        return$this;  
							 
							
								    }  
							 
							
								    /** 
							 
							
								     * 解锁 
							 
							
								     * 
							 
							
								     * @param string $key 
							 
							
								     * @return Cache_File 
							 
							
								     */  
							 
							
								    publicfunctionunlock($key) {  
							 
							
								        $cacheFile= self::_getCacheFile($key);  
							 
							
								        @unlink($cacheFile.'.lock');  
							 
							
								        return
							 
						 | 
				
转载请注明来源:php缓存技术详细介绍及php缓存的实现代码
哈尔滨品用软件有限公司致力于为哈尔滨的中小企业制作大气、美观的优秀网站,并且能够搭建符合百度排名规范的网站基底,使您的网站无需额外费用,即可稳步提升排名至首页。欢迎体验最佳的哈尔滨网站建设。
