繁体   English   中英

获取有关缓存的更新数据

[英]to get updated data on caching

我已经在我的PHP脚本中包括了缓存。.缓存进行​​得很好,但是问题是即使数据库中的数据发生了变化,它也没有得到反映..它显示的是相同的旧数据..我想要的是如果任何数据已更改数据库应该获取新数据。

<?php 
// Load the cache process i
include("cache.php"); 
// Connect to database 
include("config.php"); 
mysql_connect($db_host, $db_username, $db_password) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error()); ?>
 <html> 
<body>
 <h1>Articles</h1>
 <ul> 
<?php // Some query
 $q = mysql_query("SELECT * FROM articles ORDER BY id");
 while ($r = mysql_fetch_array($q)) { 
  echo '<li><a href="view_article.php?id='.$r['id'].'">'.$r['title'].'</a></li>';
 } ?>
 </ul>
 </body> 
</html> 
<?php // Save the cache include("cache_footer.php"); ?> 

在这里,许多方法都带有特定的标记。 文件,变量或其他。 如果您进行数据库查询,请将标志更改为其他标志,并且每次需要高速缓存时,请检查该标志是否过期,以及是否为->重建高速缓存

编辑:

好吧,非常简单,这样您便会明白:

在查询之前(或之后)执行以下操作:

file_put_contents('path/to/cache/cache.flag', md5(time()));

在您的缓存文件中:

$ cacheversion ='someHashWhichYouUpdateEverytimeYouBuildANewCache';

if($cacheversion != file_get_contents(__DIR__.'/cache.flag') {
  //build a new cachefile and put the latest hash here
} else {
  echo $html;
}

非常简单,更改任何数据后删除缓存文件。 您还可以为每个标签/主题创建一个单独的文件夹,因此您只需要...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM