有些信息比方經(jīng)常不變的,但是還是能變的信息放在緩存中以加快顯示速度,這是很有價值的,所謂的緩存,通俗的理解就是一些保存在服務(wù)器端的共用信息.它是于服務(wù)器同生死的,我們在保存緩存的時候可以指定下次更新的時間的判斷,比方要在5分鐘更新一次

數(shù)據(jù)緩存:這里所說的數(shù)據(jù)緩存是指數(shù)據(jù)庫查詢PHP緩存機(jī)制,每次訪問頁面的時候,都會先檢測相應(yīng)的緩存數(shù)據(jù)是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù),并把查詢結(jié)果序列化后保存到文件中,以后同樣的查詢結(jié)果就直接從緩存表或文件中獲得。

用的最廣的例子看Discuz的搜索功能,把結(jié)果ID緩存到一個表中,下次搜索相同關(guān)鍵字時先搜索緩存表。

舉個常用的方法,多表關(guān)聯(lián)的時候,把附表中的內(nèi)容生成數(shù)組保存到主表的一個字段中,需要的時候數(shù)組分解一下,這樣的好處是只讀一個表,壞處就是兩個數(shù)據(jù)同步會多不少步驟,數(shù)據(jù)庫永遠(yuǎn)是瓶頸,用硬盤換速度,是這個的關(guān)鍵點。

頁面緩存

每次訪問頁面的時候,都會先檢測相應(yīng)的緩存頁面文件是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù),顯示頁面并同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發(fā)揮作用了。(模板引擎和網(wǎng)上常見的一些PHP緩存機(jī)制類通常有此功能)

時間觸發(fā)緩存

檢查文件是否存在并且時間戳小于設(shè)置的過期時間,如果文件修改的時間戳比當(dāng)前時間戳減去過期時間戳大,那么就用緩存,否則更新緩存。

內(nèi)容觸發(fā)緩存

當(dāng)插入數(shù)據(jù)或更新數(shù)據(jù)時,強(qiáng)制更新PHP緩存機(jī)制。

靜態(tài)緩存

這里所說的靜態(tài)緩存是指靜態(tài)化,直接生成HTML或XML等文本文件,有更新的時候重生成一次,適合于不太變化的頁面,這就不說了。

以上內(nèi)容是代碼級的解決方案,我直接CP別的框架,也懶得改,內(nèi)容都差不多,很容易就做到,而且會幾種方式一起用,但下面的內(nèi)容是服務(wù)器端的緩存方案,非代碼級的,要有多方的合作才能做到

內(nèi)存緩存:

Memcached是高性能的,分布式的內(nèi)存對象PHP緩存機(jī)制系統(tǒng),用于在動態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度。

php的緩沖器:

有eaccelerator, apc, phpa,xcache,這個這個就不說了吧,搜索一堆一堆的,自己看啦,知道有這玩意就OK

MYSQL緩存

這也算非代碼級的,經(jīng)典的數(shù)據(jù)庫就是用的這種方式,看下面的運行時間,0.09xxx之類的
我貼段根據(jù)藍(lán)色那家伙修改后部分my.ini吧,2G的MYISAM表可以在0.05S左右,據(jù)說他前后改了有快一年

基于反向代理的Web緩存:

如Nginx,SQUID,mod_proxy(apache2以上又分為mod_proxy和mod_cache)
NGINX的例子

用google找到一些 php緩存技術(shù)方法

發(fā)個PHP緩存實現(xiàn),實現(xiàn)了apc和文件緩存,繼承Cache_Abstract即可實現(xiàn)調(diào)用第三方的緩存工具。

參考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="
 */  
abstractclassCache_Abstract {  
    /** 
     * 讀<a href="
     * 
     * @param string $key <a href="
     * @return mixed 
     */  
    abstractpublicfunctionfetch($key);  
        
    /** 
     * <a href="
     * 
     * @param string $key <a href="
     * @param string $value <a href="
     * @return bool 
     */  
    abstractpublicfunctionstore($key,$value);  
        
    /** 
     * 刪除<a href="
     * 
     * @param string $key <a href="
     * @return Cache_Abstract 
     */  
    abstractpublicfunctiondelete($key);  
        
    /** 
     * 清(刪)除所有<a href="
     * 
     * @return Cache_Abstract 
     */  
    abstractpublicfunctionclear();  
        
    /** 
     * 鎖定<a href="
     * 
     * @param string $key <a href="
     * @return Cache_Abstract 
     */  
    abstractpublicfunctionlock($key);  
    
    /** 
     * <a href="
     * 
     * @param string $key <a href="
     * @return Cache_Abstract 
     */  
    abstractpublicfunctionunlock($key);  
    
    /** 
     * 取得<a href="
     * 
     * @param string $key <a href="
     * @return bool 
     */  
    abstractpublicfunctionisLocked($key);  
    
    /** 
     * 確保不是鎖定狀態(tài) 
     * 最多做$tries次睡眠等待解鎖,超時則跳過并解鎖 
     * 
     * @param string $key <a href="
     */  
    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擴(kuò)展<a href="
 *  
 *  
 * @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="
     * 
     * @param string $key 
     * @param mixed $value 
     * @return bool 
     */  
    publicfunctionstore($key,$value) {  
        returnapc_store($this->_storageKey($key),$value);  
    }  
        
    /** 
     * 讀取<a href="
     * 
     * @param string $key 
     * @return mixed 
     */  
    publicfunctionfetch($key) {  
        returnapc_fetch($this->_storageKey($key));  
    }  
        
    /** 
     * 清除<a href="
     * 
     * @return Cache_Apc 
     */  
    publicfunctionclear() {  
        apc_clear_cache();  
        return$this;  
    }  
        
    /** 
     * 刪除<a href="
     * 
     * @return Cache_Apc 
     */  
    publicfunctiondelete($key) {  
        apc_delete($this->_storageKey($key));  
        return$this;  
    }  
        
    /** 
     * <a href="
     * 
     * @param string $key 
     * @return bool 
     */  
    publicfunctionisLocked($key) {  
        if((apc_fetch($this->_storageKey($key) .'.lock')) === false) {  
            returnfalse;  
        }  
            
        returntrue;  
    }  
        
    /** 
     * 鎖定<a href="
     * 
     * @param string $key 
     * @return Cache_Apc 
     */  
    publicfunctionlock($key) {  
        apc_store($this->_storageKey($key) .'.lock','', 5);  
        return$this;  
    }  
        
    /** 
     * <a href="
     * 
     * @param string $key 
     * @return Cache_Apc 
     */  
    publicfunctionunlock($key) {  
        apc_delete($this->_storageKey($key) .'.lock');  
        return$this;  
    }  
        
    /** 
     * 完整<a href="
     * 
     * @param string $key 
     * @return string 
     */  
    privatefunction_storageKey($key) {  
        return$this->_prefix .'_'.$key;  
    }  
}  
    
/** 
 * 文件<a href="
 *  
 *  
 * @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="
     * 
     * @param string $key 
     * @return string 
     */  
    protectedfunction_getCacheFile($key) {  
        $subdir=$this->useSubdir ?substr($key, 0, 2) .'/':'';  
        return$this->_cachesDir .'/'.$subdir.$key.'.php';  
    }  
    
    /** 
     * 讀取<a href="
     * 為防止信息泄露,<a href="
     *  
     * @param string $key <a href="
     * @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="
     * 為防止信息泄露,<a href="
     * 
     * @param string $key <a href="
     * @param string $value <a href="
     * @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="
     * 
     * @param string $key <a href="
     * @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;  
    }  
    
    /** 

轉(zhuǎn)載請注明來源:php緩存技術(shù)詳細(xì)介紹及php緩存的實現(xiàn)代碼

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。