【问题标题】:I don't understand what's going on with file_exists and scandir我不明白 file_exists 和 scandir 发生了什么
【发布时间】:2017-06-06 17:16:49
【问题描述】:

我正在帮助一位朋友为他的一个脚本编写一个合理的缓存功能。它本质上是使用 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 了。

【问题讨论】:

    标签: php file preg-match browser-cache


    【解决方案1】:

    我认为您可以尝试使用任何现有的缓存系统,例如 APC。它允许您设置(使用所需的 TTL)并获取几乎所有数据。它非常可靠且易于使用。

    【讨论】:

    • 谢谢伙计。我不知道已经存在类似的东西(memcached 除外),我会试一试。
    猜你喜欢
    • 2012-10-18
    • 2021-10-24
    • 1970-01-01
    • 2021-01-05
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多