在我解释缓存脚本的过程时,请不要理我。目前它是在第一次查看文件时完美地缓存我的文件,但是当我试图强制它重新缓存文件时没有任何反应。
首先,这个代码位于每个页面顶部的名为 header.php 的文件中:
<?php
$requestURI = rtrim($_SERVER['REQUEST_URI'], '/');
if (stripos($requestURI,'/admin') != false) { //Don't cache admin pages
$cache_time = 0;
} else{
$cache_time = 31556926;
}
$cache_folder = $_SERVER['DOCUMENT_ROOT'].'/cache/';
$cache_filename = $cache_folder.md5($requestURI).'.txt'; //Create unique name for cache
$cache_created = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;
if ($_POST['cache'] != "yes") { // Allow function to be overridden
if ((time() - $cache_created) < $cache_time) {
readfile($cache_filename); // The cached copy is still valid use it.
die();
}
}
ob_start( );
?>
它会检查此页面的缓存文件是否已存在以及是否已过期。
此代码位于每个页面底部的名为 footer.php 的文件中:
<?php
$html = ob_get_clean();
$config = array('indent' => TRUE,
'drop-empty-paras' => FALSE,
'output-xhtml' => TRUE,
'quote-ampersand' => TRUE,
'indent-cdata' => TRUE,
'tidy-mark' => FALSE,
'wrap' => 200);
$tidy = tidy_parse_string($html, $config, 'UTF8'); //Clean HTML output
file_put_contents($cache_filename, $tidy->value); //Create or update cache
mysql_select_db('reithg_UNITS');
$url = $_SERVER['DOCUMENT_ROOT'].$requestURI;
$cache = md5($requestURI);
$query = mysql_query("SELECT * FROM Cache WHERE CACHE = '$cache'") or die(mysql_error());
$exists = mysql_num_rows($query);
function getIP() //Get the client's IP
{
if (isset($_SERVER["HTTP_CLIENT_IP"]))
{
return $_SERVER["HTTP_CLIENT_IP"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
{
return $_SERVER["HTTP_X_FORWARDED"];
}
elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_FORWARDED"]))
{
return $_SERVER["HTTP_FORWARDED"];
}
else
{
return $_SERVER["REMOTE_ADDR"];
}
}
$IP = getIP();
if ($exists != 0) {
$cached = mysql_fetch_row($query);
$cachedID = $cached[0];
mysql_query("UPDATE Cache SET URL = '$url', CACHE = '$cache', UPDATED = NOW(), `UPDATED BY` = '$IP', WHERE ID = $cachedID;") or die(mysql_error());
} else {
mysql_query("INSERT INTO Cache (ID, URL, CACHE, CREATED, UPDATED, `CREATED BY`, `UPDATED BY`) VALUES('NULL', '$url', '$cache', NOW(), 'NULL', '$IP', 'NULL') ") or die(mysql_error());
}
echo $tidy;
?>
它创建缓存文件,并收集有关数据库生成的一些信息。
然后我在另一个位置有一个ajax脚本,它生成一个包含所有缓存页面的列表,并通过cache = yes
发送POST
到每个应该强制重新缓存的页面。 (停止读取缓存的文件),但没有任何反应,缓存的文件保持不变。以下是PHP生成后的jquery脚本:
function recache(){
var request1 = $.ajax({type: 'POST',url: '../units_and_evidence/flash_delivery.php?id=1',data: 'cache = yes'});
var request2 = $.ajax({type: 'POST',url: '../',data: 'cache = yes'});
var request3 = $.ajax({type: 'POST',url: '../units_and_evidence',data: 'cache = yes'});
var request4 = $.ajax({type: 'POST',url: '../units_and_evidence/flash_delivery.php?id=15',data: 'cache = yes'});
function successful () {
alert("Reset cache success");
}
function failure () {
alert("Reset cache failed");
}
var recache = $.when(request1,request2,request3,request4).then(successful, failure);
}
它返回successful()
函数,因此POST
成功,因此必须是 header.php 或 footer.php 的问题
答案 0 :(得分:0)
好的,我已经解决了这个问题。问题在于data
属性中的间距。
因此data: 'cache = yes'
需要成为data: 'cache=yes'
。