I'll assume the lock_retrieve() and lock_nstore() both obtain a lock and then operate on the cachefile.

First off, you have some problematic races. There is one between -e $lockfile and open (LOCK, ">$lockfile"); where multiple script instances can fall through to the non-cached path (and then sequentially update the cache file).

Secondly, I think there's a race between close LOCK and unlink $lockfile. After the close, another process can obtain the lockfile, only for this to be promptly deleted, allowing a third process to grab the lock as well...

Thirdly, the locking will needlessly hinder cached reads when it happens.

Now about the fix. 1) Open the cache-file. 2) Stat the open file. 3) If age is less than 2*thresh, use the cached content. 4) If age is more than 1*thresh, trigger $update_needed. 5) When update is needed, launch the updater, or, if access delays are acceptable, perform the locked-update right there. 6) The updater can LOCK_EX|LOCK_NB the cache file, write a new tmpfile and rename that. Coded this way, the cached fast path needs no locking at all. HtH.

Edit. added LOCK_NB above, probably the simplest way to ensure a single updater is run. flock+fork should be acceptable for running the update.


In reply to Re: Faster locking by oiskuu
in thread Faster locking by bbs2web

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.