| 1 |
lars |
1 |
|
|
|
2 |
Patrick O'Lone suggests the following idea which sounds interesting to
|
|
|
3 |
add as an optional mode of Cache_Lite class.
|
|
|
4 |
(still not tested in the Cache_Lite context)
|
|
|
5 |
|
|
|
6 |
-------------------------------------------------------------------------
|
|
|
7 |
If you use the flags:
|
|
|
8 |
|
|
|
9 |
ignore_user_abort(true);
|
|
|
10 |
|
|
|
11 |
$fd = dio_open($szFilename, O_CREATE | O_EXCL | O_TRUNC | O_WRONLY,
|
|
|
12 |
0644);
|
|
|
13 |
if (is_resource($fd)) {
|
|
|
14 |
|
|
|
15 |
dio_fcntl($fd, F_SETLKW, array('type' => F_WRLCK));
|
|
|
16 |
dio_write($fd, $szBuffer);
|
|
|
17 |
dio_fcntl($fd, F_SETLK, array('type' => F_UNLCK));
|
|
|
18 |
dio_close($fd);
|
|
|
19 |
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
ignore_user_abort(false);
|
|
|
23 |
|
|
|
24 |
Only the first process will attempt to create a file. Additional
|
|
|
25 |
processes will see that a file already exists (at the system level), and
|
|
|
26 |
will fail. Another thing to note is that the file descriptor must be
|
|
|
27 |
opened using dio_open(), and certain features, like fgets() won't work
|
|
|
28 |
with it. If your just doing a raw write, dio_write() should be just
|
|
|
29 |
fine. The dio_read() function should be used like:
|
|
|
30 |
|
|
|
31 |
$fd = dio_open($szFilename, O_RDONLY|O_NONBLOCK, 0644);
|
|
|
32 |
if (is_resource($fd)) {
|
|
|
33 |
|
|
|
34 |
dio_fcntl($fd, F_SETLKW, array('type' => F_RDLCK));
|
|
|
35 |
$szBuffer = dio_read($fd, filesize($szFilename));
|
|
|
36 |
dio_fcntl($fd, F_SETLK, array('type' => F_UNLCK));
|
|
|
37 |
dio_close($fd);
|
|
|
38 |
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
You still use locking to ensure that a write process can finish before
|
|
|
42 |
another attempts to read the file. We also set non-blocking mode in read
|
|
|
43 |
mode so that multiple readers can access the same resource at the same
|
|
|
44 |
time. NOTE: Direct I/O support must be compiled into PHP for these
|
|
|
45 |
features to work (--enable-dio).
|
|
|
46 |
-------------------------------------------------------------------------
|