[英]PHP Cache specific parts of a page
我在頁面上有一些部分需要相當多的資源我想要緩存,這里是一個頁面示例。
[=== Some Static HTML ===]
[=== PHP Script 1 ===]
[=== Some Static HTML ===]
[=== PHP Script 2 ===]
我想將“PHP腳本1”放入緩存文件,例如script1.html並包含它而不是處理整個腳本,對於腳本2也是如此。
我遇到的問題是我可以輕松地緩存整個頁面並且它可以工作,但我想只緩存特定部分(如上所述),因為有些事情需要用戶會話數據。
我有這個類,意味着能夠停止和啟動緩沖區,所以我可以拉出特定的部分,而不會破壞頁面的其余部分,但它不能做我想要的。 http://pastebin.com/Ua6DDExw
我希望能夠像下面這樣,雖然它會將該部分存儲在一個文件中,其中包含一個簡單的php inlcude而不是數據庫。
HTML Content
<?php
$cache->start_buffer("cache_name");
// PHP Script
$cache->end_buffer("cache_name");
?>
HTML Content
<?php
$cache->start_buffer("cache_name");
// PHP Script
$cache->end_buffer("cache_name");
?>
我沒有訪問memcache或其他任何類似的東西,因為這將在共享主機上進行。
任何幫助都會很棒,謝謝
研究使用ob_start()
和ob_flush()
它可以做你想做的事情。 您需要手動將其寫入文件。 在野外也有cache.php類。
http://php.net/manual/en/function.ob-start.php
<?php
$cache_time = 3600; // Time in seconds to keep a page cached
$cache_folder = '/cache'; // Folder to store cached files (no trailing slash)
// Think outside the box the original said to use the URI instead use something else.
$cache_filename = $cache_folder.md5(",MyUniqueStringForMyCode"); // Location to lookup or store cached file
//Check to see if this file has already been cached
// If it has get and store the file creation time
$cache_created = (file_exists($cache_file_name)) ? filemtime($this->filename) : 0;
if ((time() - $cache_created) < $cache_time) {
$storedData = readCacheFile($cache_filename);
}
else
{
// Alternatively you can ignore the ob_start/get_contents/end_flush code
// and just call a function and store it directly to the variable.
// Start saving stuff
ob_start();
/** do your work here echoing data to the screen */
$storedData = ob_get_contents();
ob_end_flush();
// create the cachefile for the data.
createCacheFile($cache_filename);
}
// Do stuff with $storedData.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.