首頁 >後端開發 >php教程 >php頁面緩存

php頁面緩存

WBOY
WBOY原創
2016-07-29 08:51:021244瀏覽

  這幾天接觸了phpcms的頁面緩存,有些感觸。其好處就不多說了,它一般是用在資料庫查詢較多的頁面中,對於插入修改刪除的頁面就不太合適了。

  這裡有快取技術的簡單介紹:http://www.cnblogs.com/penghcn/articles/2720202.html

  php頁面快取主要用到的是ob系列函數,如同ob ob_get_contents()

  下面是編碼部分。

  1.初始化函數,一般是設定頁面快取路徑、快取檔案命名格式等,可依個人喜好自訂。這裡用到的識別ID是經加密的$_SERVER[REQUEST_URI]參數。這個函數中最後還有一個if判斷:若未過快取期,則載入快取文件,否則載入來源檔案。

php頁面緩存

<span> 1</span><span>function</span><span> page_init()
</span><span> 2</span><span>{    
</span><span> 3</span><span>$url</span> = <span>$_SERVER</span>['REQUEST_URI'];<span>//</span><span>子url,该参数一般是唯一的</span><span> 4</span><span>$pageid</span> = <span>md5</span>(<span>$url</span><span>);
</span><span> 5</span><span>$dir</span> = <span>str_replace</span>('/','_',<span>substr</span>(<span>$_SERVER</span>['SCRIPT_NAME'],1,-4<span>));
</span><span> 6</span><span>//</span><span>目录命名方式,如exp_index</span><span> 7</span><span>if</span>(!<span>file_exists</span>(<span>$pd</span> = PAGE_PATH.<span>$dir</span>.'/'))@<span>mkdir</span>(<span>$pd</span>,0777) or <span>die</span>("<span>$pd目录创建失败</span>"<span>);
</span><span> 8</span><span>//</span><span>如cache/page/exp_index/</span><span> 9</span><span>define</span>('PAGE_FILE',<span>$pd</span>.<span>$pageid</span>.'.html'<span>);
</span><span>10</span><span>//</span><span>如cache/page/exp_index/cc8ef22b405566745ed21305dd248f0e.html</span><span>11</span><span>$contents</span> = <span>file_get_contents</span>(PAGE_FILE);<span>//</span><span>读出</span><span>12</span><span>13</span><span>if</span>(<span>$contents</span> && <span>substr</span>(<span>$contents</span>, 13, 10) ><span> time() )<span>//</span><span>对应page_cache()函数中加上的自定义头部</span></span><span>14</span>     {
<span>15</span><span>echo</span><span>substr</span>(<span>$contents</span>, 27<span>);
</span><span>16</span><span>exit</span>(0<span>);
</span><span>17</span><span>    }
</span><span>18</span><span>return</span><span>true</span><span>;    
</span><span>19</span> }        

php頁面緩存

  2.頁面快取函數,這裡使用到一個技巧:在快取檔案的內容中只需要時間後,所以只需要時間加上一個頭部資訊--頭部中的過期時間和當前時間進行比較(在page_init()函數中進行)就能判斷快取是否過期了。

php頁面緩存

<span>1</span><span>function</span> page_cache(<span>$ttl</span> = 0<span>)
</span><span>2</span><span>{    
</span><span>3</span><span>$ttl</span> = <span>$ttl</span> ? <span>$ttl</span> : PAGE_TTL;<span>//</span><span>缓存时间,默认3600s</span><span>4</span><span>$contents</span> = <span>ob_get_contents</span>();<span>//</span><span>从缓存中获取内容</span><span>5</span><span>$contents</span> = "<!--page_ttl:".(time() + <span>$ttl</span>)."-->\n".<span>$contents</span><span>;
</span><span>6</span><span>//</span><span>加上自定义头部:过期时间=生成时间+缓存时间</span><span>7</span><span>file_put_contents</span>(PAGE_FILE, <span>$contents</span>);<span>//</span><span>写入缓存文件中</span><span>8</span><span>ob_end_flush</span>();<span>//</span><span>释放缓存</span><span>9</span> }    

php頁面緩存

  3.函數使用,注意這兩個函數有先執行順序,還有別

php頁面緩存

以上就介紹了php頁面緩存,包括了方面的內容,希望對PHP教學有興趣的朋友有幫助。 php頁面緩存

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn