簡體   English   中英

我不明白file_exists和scandir發生了什么

[英]I don't understand what's going on with file_exists and scandir

我正在幫助一位朋友為他的一個劇本編寫一個有點合理的緩存功能。 它主要使用9種不同的SQL查詢來獲取排行榜數據(其中一種非常繁瑣)

我提出了為它創建緩存的想法,從這開始:

  // 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);

繼續,我設置一些開關來確定是否需要更新,然后獲取包含緩存文件夾中所有文件的數組的所有文件和大小。

我跟進這個:

  // 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;
      }
    }
  }

我做的是創建和寫入緩存文件,我附加一個unix時間戳來表示它的有效時間,從文件名中拆分它並將當前時間與時間戳的時間進行比較以確定是否刪除文件並重新創建它。 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);
    }
  }

這在我的計算機上工作得很好,使用多個瀏覽器坐在頁面上並發送垃圾郵件,但是第二次觸摸緩存文件(比如第一次讀取它,或者在腳本運行時打開它)文件本身被垃圾郵件發送添加。 這是相同的3個領域重復自己。

到目前為止,我嘗試了幾種不同的方法,但我完全無法弄清楚發生了什么。

有沒有其他人遇到過這個? 你是怎么解決的? 我在這里做錯了什么或錯過了什么?

我今天晚些時候會繼續關注它,但要提前感謝任何見解! 不幸的是,自從我輕松使用PHP以來已經有一段時間了。

我想你可以嘗試使用任何現有的緩存系統,例如APC。 它允許您設置(使用所需的TTL)並獲取大部分數據。 它非常可靠且易於使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM