function writeRankListCache($type) { $cachefile = str_replace('?', $type, $GLOBALS['gCacheFile']); $overTime = 0; if ($type == 2 or $type == 3) { // 周、月排名 $overTime = 18000; // 5小时 } // 缓存不存在或超时 if (!file_exists($cachefile) || time() > filemtime($cachefile) + $overTime) { $datas = getRankSet($type); // 读取结果集 file_put_contents($cachefile, serialize($datas)); //写入缓存 } }
function rankByType($type = '') { global $gBeginTime, $gEndTime; switch ($type) { case 'day': // 过去5个交易日的日排名 $tradeDays = lastFiveTradeDays(time()); for ($i = 0; $i < 5; $i++) { $beginDate = $tradeDays[$i]; $endDate = date('Y-m-d', strtotime('+1 day', strtotime($beginDate))); $beginDatetime = $beginDate . ' ' . $gBeginTime; $endDatetime = $endDate . ' ' . $gEndTime; $data = getRankSet($beginDatetime, $endDatetime); $suffix = $type . ($i + 1); writeRankCache($data, $suffix); } break; case 'week': // 过去4周的周排名 for ($i = 1; $i <= 4; $i++) { list($beginDate, $endDate) = lastTradeNWeek(time(), $i); $endDate = date('Y-m-d', strtotime('+1 day', strtotime($endDate))); $beginDatetime = $beginDate . ' ' . $gBeginTime; $endDatetime = $endDate . ' ' . $gEndTime; $data = getRankSet($beginDatetime, $endDatetime); $suffix = $type . $i; writeRankCache($data, $suffix); } break; case 'month': // 过去1个月的月排名 list($beginDate, $endDate) = lastMonth(time()); $endDate = date('Y-m-d', strtotime('+1 day', strtotime($endDate))); $beginDatetime = $beginDate . ' ' . $gBeginTime; $endDatetime = $endDate . ' ' . $gEndTime; $data = getRankSet($beginDatetime, $endDatetime); writeRankCache($data, $type); break; default: return; } }