in reply to Safe Counter Follow Up # 2
in thread Safe Counter

I've pretty much just taken peoples' word for the following statement, but it has worked out ok so far: File writes in append mode are atomic for most operating systems.

That being the case, one thing you could do is to, instead of keeping a count, keep a tally. Open the file for append, and add "HIT\n".

Once a day (adjust to taste) a cron job could then be used to add up the tallies. The cron job's script would add all the tallies, plus a number you've stored in the first line of this file. Then rewrite the file (using a temp file) with only the total number at the top. rename() the temp file back over the original, and you've now got a fresh file.

For example:

Kind of elaborate, but if your existing course of action isn't working out for you this may be an alternative, and it's certanly less prone to race conditions. You won't have accurate up to the minute stats, but you can have up to date stats as often as you set your job to run.


Dave