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

[eluser]Lewis[/eluser]
Actually, section31's solution is the best. Here's a (summarised) extract from 'Advanced PHP Programming' by George Schlossnagle.

Advisory locks work well, but there are a few reasons to consider not using them:
- If your files reside on an NFS filesystem, flock is not guaranteed to work at all.
- Certain operating systems (Windows) don't allow non-blocking flocks.
- If more than one person accesses the page during a write, each one will still generate and overwrite the content multiple times.
- If the process crashes out during writing to the cache, a reading process will read the partial and corrupt file.
- There are certain situations (although rare) that the lock is never removed. (I believe an EE user had this problem - I saw a link somewhere to a forum post about it).

File swaps work by taking advantage of a nuance. When you use unlink() on a file, what really happens is that the filename-to-inode mapping is removed. The filename not longer exists, but the storage associated with it remains unchanged (for the moment), and it still has the same inode associated with it. In fact,the operating system does not reallocate that space until all open file handles on that inode are closed. This means that any processes that are reading from that file while it is unlinked are not interrupted; they simply continue to read from the old file data. When the last of the processes holding an open descriptor on that inode closes, the space allocated for that inode is released back for reuse.

... It then goes on about and gives this example code (I've shortened it):

Code:
<?php
if (file_exists($cache_file)){
// .. Load the cache here
}
else {
// PID is unique to each process so no worrying about clashes
$cache_file_tmp = $cache_file .'.'. getmypid();

// .. Write to $cache_file_tmp

// Then rename it
rename($cache_file_tmp, $cache_file);
}

The rename() function performs atomic moves when the source and destination are on the same filesystem, meaning that the operation is instantaneous. The benefits of using this methodology:
- The code is much shorter and incurs fewer system calls (thus in general is faster)
- Because you never modify the cache file directly, you elminate the possibility of writing a partial or corrupted cache file.

So it looks like section31's method would be much more beneficial.


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