私は友人が彼のスクリプトのためにやや賢明なキャッシング機能を書こうとしています。file_existsとscandirで何が起こっているのか分かりません
に引き続き// 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);
:基本的に私はこのような発進、それのためにキャッシュを作成するためのアイデアを思い付いた(非常に課税であるそのうちの一つ)リーダーボードのデータをフェッチするために
を9種類のSQLクエリを使用しています更新する必要があるかどうかを判断するためにいくつかのスイッチを設定し、キャッシュフォルダ内のすべてのファイルを含む配列のすべてのファイルとサイズを取得します。
私はこれをフォローアップ:私は何
// 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を手に入れて以来、しばらくしてきました。
ありがとうございます。私はすでに存在しているようなものを知らなかった(memcachedを除く)、私はそれにスピンを与えるだろう。 – Dixos