Opened 7 years ago

Last modified 7 years ago

#112 new enhancement

better vardata caching by implementing pre-check/post-check algo

Reported by: moo Owned by: moo
Priority: major Milestone: 5.0.0
Component: cacher Version: 2.0.0
Keywords: Cc:
Application: PHP Version:
Other Exts: SAPI:
Probability: Blocked By:
Blocking:

Description (last modified by moo)

  • Update API: xcache_isset($name [, $post_check_ttl])

Example usage:


<?php
define(TMPDIR, '/tmp');

function load_abc_data()
{
  $name = "abc_data";
  $ttl = 120; // cache for 2 minutes
  $post_check_ttl = 60; // reload every 1 minutes, while other reader keep using the stale data
  if (!xcache_isset($name, $post_check_ttl)) {
    // it worth a lock here to avoid useless yet harmful concurrent load from any slow backend (backend=mysql here).
    $fp = fopen(TMPDIR . "/abc_data.lock", "w");
    // only one worker update it and other keep on reading, if it's in post-check time
    $nb = xcache_isset($name) ? 0 : LOCK_NB;
    if (flock($fp, LOCK_EX | $nb)) {
      // check AGAIN after we get the lock
      if (!xcache_isset($name, $post_check_ttl)) {
        mysql_query .... and get $data
        xcache_set($name, $data, 120);
        fclose($fp);
        return $data;
      }
      else {
        fclose($fp);
      }
    }
  }
  return xcache_get($name);
}

?>

See MSDN for Cache-Control pre-check/post-check algo

Change History (2)

comment:1 Changed 7 years ago by moo

  • Type changed from defect to enhancement

comment:2 Changed 7 years ago by moo

  • Description modified (diff)
Note: See TracTickets for help on using tickets.