I do not understand what is happening with file_exists and scandir

I help a friend write a somewhat reasonable caching function for one of his scripts. It essentially uses 9 different SQL queries to retrieve leaderboard data (one of which is highly taxable)

I came up with the idea of ​​creating a cache starting with this:

  // Cache directory
  $cacheDir = "/cache";

  // Path to cache directory
  $cachePath = "/var/www/html/test";

  // Better safe than sorry
  if (!file_exists($cachePath . $cacheDir))
  {
        mkdir($cachePath . $cacheDir, 0777, true);
  }

  // Cache rebuild switches
  $monsterCache = false;
  $pvpCache = false;
  $guildCache = false;

  // Get the size and all files within the cache directory
  $cache = array_slice(scandir($cachePath . $cacheDir), 2);
  $cacheSize = sizeof($cache);

Continuing work, I install some keys to determine whether we need to update or not, and then extract all the files and the size of the array containing all the files in the cache folder.

I follow this:

  // Validate the cached files
  if ($cacheSize < 1) {
    // None present. Rebuild all.
    $monsterCache = true;
    $pvpCache = true;
    $guildCache = true;
  } else {
    for ($i = 0; $i < $cacheSize; $i++) {
      // Check the monster kill cache
      if (preg_match('/^[0-9]+_monster_kills_leaderboard\.php/', $cache[$i], $cacheFile)) {
        if (time() >= explode('_', $cacheFile[0])[0]) {
          unlink($cachePath . $cacheDir . "/{$cache[$i]}");
          $monsterCache = true;
        }
      } else {
        $monsterCache = true;
      }

      // Check the PVP cache
      if (preg_match('/^[0-9]+_pvp_leaderboard\.php/', $cache[$i], $cacheFile)) {
        if (time() >= explode('_', $cacheFile[0])[0]) {
          unlink($cachePath . $cacheDir . "/{$cache[$i]}");
          $pvpCache = true;
        }
      } else {
        $pvpCache = true;
      }

      // Check the Castle Guild leader cache
      if (preg_match('/^[0-9]+_guild_leader\.php/', $cache[$i], $cacheFile)) {
        if (time() >= explode('_', $cacheFile[0])[0]) {
          unlink($cachePath . $cacheDir . "/{$cache[$i]}");
          $guildCache = true;
        }
      } else {
        $guildCache = true;
      }
    }
  }

What I do when creating and writing a cache file, I add a unix timestamp to represent how long it has been valid, separate it from the file name and compare the current time with the timestamp to determine if you want to delete the file and recreate it. (Timestamp_pvp_leaderboard.php)

:

  if ($monsterCache) {
    $monsterCache = false;

    // This decides how long the cache is valid
    // Updates every hour from initialization.
    $cacheTTL = strtotime('+1 Hour', time());

    // Fetch the monster data
    <snip>

    // Construct data
    $data = array(
      'Name, Kills' => $result[0]->__get("name") . ', ' . $result[0]->__get("kills"),
      'Name, Kills' => $result[1]->__get("name") . ', ' . $result[1]->__get("kills"),
      'Name, Kills' => $result[2]->__get("name") . ', ' . $result[2]->__get("kills")
    );

    // Populate the cache
    foreach($data as $key => $val) {
      file_put_contents($cachePath . $cacheDir . "/{$cacheTTL}_monster_kills_leaderboard.php", $key.', '.$val.PHP_EOL, FILE_APPEND | LOCK_EX);
    }
  }

, , , - (, , script ) . 3 .

, , .

- ? ? ?

, ! - , , , PHP.

+4
1

, , APC. ( TTL) . .

0

Source: https://habr.com/ru/post/1678649/


All Articles