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:
- At midnight the job runs and resets the file to look like this:
234428\n
- By 2:00am the file has accumulated some hits and looks like this:
234428\n
HIT\n
HIT\n
HIT\n
HIT\n
- At the following midnight the cron job runs again and adds up all "HIT\n"s. Let's say there were 1000. So it writes a temp file that looks like:
235428\n
It then renames the tempfile over the original file, and starts a new day.
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.