php – 我不明白file_exists和scandir发生了什么
发布时间:2020-12-13 22:27:56 所属栏目:PHP教程 来源:网络整理
导读:我正在帮助一位朋友为他的一个剧本编写一个有点合理的缓存功能.它主要使用9种不同的SQL查询来获取排行榜数据(其中一种非常繁琐) 我提出了为它创建缓存的想法,从这开始: // Cache directory $cacheDir = "/cache"; // Path to cache directory $cachePath =
我正在帮助一位朋友为他的一个剧本编写一个有点合理的缓存功能.它主要使用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)并获取大部分数据.它非常可靠且易于使用.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |