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

[eluser]Elliot Haughin[/eluser]
Ok, there's been some great discussion here. So, I'd like to propose a method that I believe will solve the problems.

Code:
$this->lockfile = 'cache.lock';


function lock_cache()
{
    

    if ( file_exists($this->lockfile) )
    {
        $modified = file_get_contents($this->lockfile);

        // modified is a unix timestamp as the contents of the lockfile.
        // slick huh? - gets around the filemtime issue.
        // Let's check the $modified time of cache lock is reasonable;
        
        // What time is it Mr. Wolf?
        
        $now = time();

        // We want to do numeric calculations only.
        // If the cach lock contents are corrupt, this will strip out
        // bad characters. Taking the value not the string.

        $modified = intval($modified);

        // Is the locktime in the future?

        $diff = $now - $modified;

        if ($diff < 0 || $diff > 3 )
        {
            // File was written in the future? -
            // This isn't Marty McFly!
            // OR
            // The file was written more than 3 seconds before $now

            // The cache lock isn't valid... it was left behind accidentally.

            unlink($this->lockfile);
        }
    }


    // Time to test 1 last time, it might have been deleted
    // because it wasn't supposed to be there.


    if ( !file_exists($this->lockfile) )
    {
         // Write a little 'do not disturb sign and
         // put it on the door :)
         // which is the timstamp right now.

         $lockhandle = fopen($$this->lockfile, 'x+');  
         fwrite($lockhandle, time());
         fclose($lockhandle);
        
         return true;
    }
    else
    {
        return false;
    }
}

function unlock_cache()
{
    @unlink($this->lockfile);
}

function write_cache()
{
    if ( $this->lock_cache() )
    {
        // NORMAL WRITING OF CACHE FILE HERE
        
        
        
        $this->unlock_cache();
    }
}

Again, this isn't tested. I've probably missed semi-colons everywhere.
But read it through from the write_cache() function. Hopefully it should make sense, hope you can see where I'm going with this.


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