wiki:AutoHardLinking

Version 3 (modified by moo, 5 years ago) (diff)

--

Auto Hard Linking

The problem

Many of you run your own server/vps that install only 1 copy of each web applications under your server document root, while some other of you, the vhost provider, allow every user install their own copy. For example, vBulletin may be installed under each user's vhost document root. This result in not only disk read/seek without XCache installed, but also consume huge memory for caching them with XCache installed. Most *.php file is bigger after compiled/cached.

The Solution

XCache has introduced a way to solve this headache once and for all. This is called auto hard linking. Whatever it called, let's see what it does.

It's easier to demonstrate in PHP pseudo code.

<?php
function file_entry($filename) {
  if (hash_on_inode()) {
    $stat = stat($filename);
    $hash = hash($stat['inode']);
  }
  else {
    $hash = hash($filename);
  }

  if (!isset($cache[$hash])) {
    $md5 = md5sum($filename);
    if (!isset($phpcache[$md5])) {
      $phpcache[$md5] = php_compile_file($compile);
    }
    $cache[$hash] = &$phpcache[$md5]; // reference, important
  }
  return $cache[$hash];
}

restore_cached_php(find_cached_php($filename));
?>

FAQ

  • How about the __FILE__ and __DIR__ in *.php that get cached? Don't worry, i had a back-patching to fix it already.
  • "I have hard link in filesystem, is __FILE__ ok?". Nope, not yet. But it is not necessary anymore.