NFS file locking in PHP

Part of my last web application should write to the file as a sum as part of its logging. One of the problems that I noticed is that if there are several simultaneous users, the records can overwrite each other (instead of adding to the file). I assume this is because the destination file can be opened in several places at the same time.

flock(...) usually excellent, but it doesn’t work on NFS ... Which is a huge problem for me, since the production server uses an NFS array.

The closest thing I saw for the actual solution is trying to create a lock locator and wait until it is created. To say that this is a lack of elegance is an understatement of the year, perhaps a decade.

Any better ideas?

Edit: I have to add that I don’t have a root on the server, and it’s not always possible to do the repository in the near future, not least within my term.

+5
source share
6 answers

Another dirty hack would be a flock()“local” file and only open / write to the NFS file if you hold the lock on the local file.

Edit: on page flock():

flock () will not work on NFS, and many other network file systems. Check your operating system documentation for more details.

Edit 2:

, ( , db). , .

, ? ( , , )?

+2

NFSv2 NFSv3 (, " NFS" , ISBN 0-201-32570-5; - NFS ).

NFSv2 :

NFSv3 create .

, - ( , PHP):

:

while ! ln -s . lock; do :; done

:

while ! ln -s ${f} ${f}.lock; do :; done 

(, ):

:

mv lock deleteme && rm deleteme

:

mv ${f}.lock ${f}.deleteme && rm ${f}.deleteme

, ( ), .

symlink rename . : .

+14

Memcache, . ape flock() .

, "",

// Check for lock, using $filename as key
$lock = $memcache->get($filename);

if(!$lock) {
    // Set lock in memcache for $filename
    $memcache->set($filename, 1);

    // Do file operations...

    // Blow away "lock"
    $memcache->delete($filename);
}

, .

+3

dio_fcntl() NFS. dio, php.

+2

flock() NFS, I/O , NFS . , .

NFS, , , .

, , , - , .

+1

memcache .

if ($memcache->add($filename, 1, 1))
{
   $memcache->delete($filename);
}
0

All Articles