php – 我不明白file_exists和scandir发生了什么

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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)并获取大部分数据.它非常可靠且易于使用.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 我不明白file_exists和scandir发生了什么全部内容,希望文章能够帮你解决php – 我不明白file_exists和scandir发生了什么所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。