Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:19:42+00:00 2026-05-27T15:19:42+00:00

I made a simple database driven php website. Now i am trying to implement

  • 0

I made a simple database driven php website. Now i am trying to implement some simple caching to the site. i tried this from somewhere

<?php
      $reqfilename="test";
      $cachetime = 60*5; // 5 minutes
      $cachefile = "cache/".$reqfilename.".html";
      $cf2="cache/".$reqfilename."2.html";
      if (file_exists($cachefile) && ((time() - $cachetime) < filemtime($cachefile))) 
      { 
         include($cachefile);
         exit;
      }
      ob_start(); 
?>
CONTENT OF THE PAGE GOES HERE
<?php   
        $fp = @fopen($cf2, 'w'); 
        if($fp){
            fwrite($fp, ob_get_contents());
            fclose($fp); 
            rename($cf2,$cachefile);
        }   
        ob_end_flush(); 
?>

But what if the cache file is being renamed and someone requests the page. Will there be an error displayed or just the user will experience a delay?

To reduce the time of the cache file being modified only, I am using rename instead of directly writing on the original cache file

Correct code to do this (based on answer by webbiedave below)

<?php
      $reqfilename="test";
      $cachetime = 60*5; // 5 minutes
      $cachefile = "cache/".$reqfilename.".html";

      if (file_exists($cachefile) && ((time() - $cachetime) < filemtime($cachefile))) 
      { 
         include($cachefile);
         exit;
      }
      ob_start(); 
?>
CONTENT OF THE PAGE GOES HERE
<?php   
        $fp = @fopen($cachefile, 'w'); 
    if (flock($fp, LOCK_EX | LOCK_NB)) {
    fwrite($fp, ob_get_contents());
    flock($fp, LOCK_UN);
    fclose($fp); 
    }

    ob_end_flush(); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:19:43+00:00Added an answer on May 27, 2026 at 3:19 pm

    Renaming the file isn’t the source of your problem. The main race condition with your script is multiple requests detecting an expired mtime and all writing to test2.html.

    A better approach is to perform an exclusive, non-blocking flock (assuming non-windows) immediately after detecting an expired mtime, buffer the output, overwrite the file and release the lock. If flock returns false, then another process is writing to it and the current process should skip the write entirely.

    If a request is made during the lock, the web server will wait until the file write is complete (and the lock released), then serve the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm teaching myself CSS through books and websites. I've made a simple site with
I have made the following code to retrive data from SQLite database. public Cursor
I've recently inherited a database driven e-commerce site written in C# ASP.Net, with an
I made a simple map, and loaded all my points from a xml file,
I am playing with MVC and I made a simple site. I have an
In college today we made a simple forum in procedural PHP. My homework is
Considering the following simple entity association: (EntityA) *-1 (EntityB) made in database with a
I made simple plugin and i would like to work this plugin only below
I have made simple and thin database layer to help me with normal operation.
I made a simple WPF application in Visual Studio. It accesses its own database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.