简体   繁体   中英

to get updated data on caching

i have included caching in my php script..caching is happening good but the problem is even if the data is changed in the database its not getting reflected..it shows same old data.. what i want is if any data had changed in database it should get new data.

<?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"); ?> 

many approaches here i'd go with a certain flag. A file, a variable or what ever. If you make a db-query change the flag to something else and everytime the cache is required check if the flag is expired and if it is-> rebuild the cache

Edit:

OK very simple so you get the idea:

Before(or after) you query do:

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

in your cache file:

$cacheversion = 'someHashWhichYouUpdateEverytimeYouBuildANewCache';

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

Very easy, delete your cache files when any data is changed. Also you can create for each tag/thema a seperate folder and so you must only this ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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