微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 我不明白file_exists和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/',$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/',$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"),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)并获取大部分数据.它非常可靠且易于使用.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐