Welcome Guest, Not a member yet? Register   Sign In
Cache Functions Suggestions
#10

[eluser]Rick Jolly[/eluser]
[quote author="Elliot Haughin" date="1202853370"]
...
* The first user finishes building the page, and begins writing the file, locking it.
* The second user also finishes building the page, and attempts to write it.

* Because the first user has locked the file, the second user can't write it...
* This is a problem for the second user, so you simply build a little fix to make it all work like magic.

If the second user can't write the the cache (because it is locked), simply echo the contents of the rendered page, not saving it to the cache.
The first user is dealing with the cache, so just render the output.

With CI, this is how it works...

Line 299 of Output.php:




Code:
if ( ! $fp = @fopen($cache_path, 'wb'))
        {
            log_message('error', "Unable to write cache file: ".$cache_path);
            return;
        }

The cache file is only written if it's 'really_writable' (not locked)

Line 59 of Output.php:

Code:
elseif (($fp = @fopen($file, 'ab')) === FALSE)
    {
        return FALSE;

So... if we cant write it, just render the second users' request.
[/quote]

Untrue.

The second user will wait for the first user to finish writing, and then the second user will overwrite the cache. In fact, on excessively busy sites, many users will stack up waiting to write to the cache. The way to prevent waits and make sure that the second user doesn't write to cache is to use a non-blocking lock in combination with the exclusive lock, but it doesn't work on windows. fopen() won't return false when a file is locked for writing.

Here is some code to demonstrate. Put this method in a controller and open a few browsers each with a different id as a url parameter.
Code:
// Create a file "myfile.txt" in the controllers directory and call
// this method from a few browsers like so:
// http://localhost/controller/test/1, http://localhost/controller/test/2, etc.
function test($id)
{
   $file_name = APPPATH . 'controllers/myfile.txt';

   if ( ! $fp = @fopen($file_name, 'wb'))
   {
       echo('Cannot write to file. Segment id: ' . $id);
   }
   else
   {
      flock( $fp, LOCK_EX );
      sleep(10);
      fwrite( $fp, 'Writing to file. Segment id: ' . $id);
      echo('Successfully wrote file. Segment id: ' . $id);
      flock($fp, LOCK_UN);
      fclose($fp);
   }
}


Messages In This Thread
Cache Functions Suggestions - by El Forum - 02-01-2008, 04:35 PM
Cache Functions Suggestions - by El Forum - 02-08-2008, 03:11 AM
Cache Functions Suggestions - by El Forum - 02-08-2008, 07:45 AM
Cache Functions Suggestions - by El Forum - 02-11-2008, 03:26 PM
Cache Functions Suggestions - by El Forum - 02-11-2008, 04:24 PM
Cache Functions Suggestions - by El Forum - 02-12-2008, 09:56 AM
Cache Functions Suggestions - by El Forum - 02-12-2008, 10:49 AM
Cache Functions Suggestions - by El Forum - 02-12-2008, 11:00 AM
Cache Functions Suggestions - by El Forum - 02-12-2008, 09:51 PM
Cache Functions Suggestions - by El Forum - 02-13-2008, 03:54 PM
Cache Functions Suggestions - by El Forum - 02-13-2008, 04:35 PM
Cache Functions Suggestions - by El Forum - 02-13-2008, 06:51 PM
Cache Functions Suggestions - by El Forum - 02-14-2008, 04:00 AM
Cache Functions Suggestions - by El Forum - 02-14-2008, 11:58 AM
Cache Functions Suggestions - by El Forum - 02-15-2008, 08:48 AM
Cache Functions Suggestions - by El Forum - 02-15-2008, 08:55 AM
Cache Functions Suggestions - by El Forum - 02-15-2008, 09:02 AM
Cache Functions Suggestions - by El Forum - 02-15-2008, 05:50 PM
Cache Functions Suggestions - by El Forum - 02-15-2008, 06:19 PM
Cache Functions Suggestions - by El Forum - 02-15-2008, 06:34 PM
Cache Functions Suggestions - by El Forum - 03-08-2008, 10:23 AM
Cache Functions Suggestions - by El Forum - 03-13-2008, 08:20 PM
Cache Functions Suggestions - by El Forum - 03-13-2008, 08:25 PM
Cache Functions Suggestions - by El Forum - 03-25-2008, 06:12 AM
Cache Functions Suggestions - by El Forum - 03-25-2008, 06:58 AM
Cache Functions Suggestions - by El Forum - 03-25-2008, 07:27 AM
Cache Functions Suggestions - by El Forum - 03-25-2008, 10:51 AM
Cache Functions Suggestions - by El Forum - 03-25-2008, 11:31 AM
Cache Functions Suggestions - by El Forum - 03-25-2008, 11:37 AM
Cache Functions Suggestions - by El Forum - 04-24-2008, 06:33 AM
Cache Functions Suggestions - by El Forum - 04-25-2008, 11:25 AM
Cache Functions Suggestions - by El Forum - 04-25-2008, 02:03 PM
Cache Functions Suggestions - by El Forum - 04-25-2008, 04:50 PM
Cache Functions Suggestions - by El Forum - 04-29-2008, 05:36 AM
Cache Functions Suggestions - by El Forum - 04-29-2008, 06:15 AM
Cache Functions Suggestions - by El Forum - 04-29-2008, 09:39 AM
Cache Functions Suggestions - by El Forum - 04-29-2008, 10:10 AM
Cache Functions Suggestions - by El Forum - 04-29-2008, 10:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB