PHP后端参考我用帝国写的哈,其他的只需要改下常量,变量。如果是框架的,自行把自定义函数集成到类里面去。- <?phpheader('Access-Control-Allow-Origin: ' . rtrim($public_r['add_murl'], '/'));defined("ECMSAPI_MOD") or exit;$redisKey = 'seolinks_api:all_data';$redisMetaKey = 'seolinks_api:meta';// 检查文件更新并刷新缓存function checkAndRefreshCache($filePath, $redis) {global $redis, $redisKey, $redisMetaKey;$currentMtime = filemtime($filePath);$cachedMtime = $redis->hGet($redisMetaKey, 'mtime');if ($currentMtime > $cachedMtime) {$count = countLines($filePath);$redis->multi()->set($redisKey, json_encode(['count' => $count]))->hSet($redisMetaKey, 'mtime', $currentMtime)->hSet($redisMetaKey, 'update_time', time())->exec();return $count;}return false;}// 计算行数function countLines($filePath) {if (!file_exists($filePath)) return 0;$fp = fopen($filePath, 'rb');if (!$fp) return 0; $count = 0;$prev = null; while (($byte = fgetc($fp)) !== false) {if ($byte === "\n" || ($prev === "\r" && $byte !== "\n")) {$count++;}$prev = $byte;} fclose($fp);return $count;}// 获取分页数据function getPageData($filePath, $page, $num) {$offset = ($page - 1) * $num;$data = [];$fp = fopen($filePath, 'r');for ($i = 0; $i < $offset && !feof($fp); $i++) {fgets($fp);}for ($i = 0; $i < $num && ($line = fgets($fp)) !== false; $i++) {$data[] = trim($line);} fclose($fp);return $data;}// 获取外链留痕数据function getData($filePath, $redis) {global $redis, $redisKey, $redisMetaKey;$cachedData = $redis->get($redisKey); if ($cachedData) {$data = json_decode($cachedData, true);$freshCount = checkAndRefreshCache($filePath, $redis);return ['count' => $freshCount !== false ? (int)$freshCount : (int)($data['count'] ?? 0),'cached' => true];} $count = countLines($filePath);$currentMtime = filemtime($filePath);$redis->multi()->set($redisKey, json_encode(['count' => $count]))->hMSet($redisMetaKey, ['mtime' => $currentMtime,'create_time' => time(),'update_time' => time()])->exec(); return ['count' => (int)$count,'cached' => false];}$filePath = ECMS_PATH.'e/extend/wlseo/links.txt';//外链留痕数据文件if (!file_exists($filePath)) {die(json_encode(['code' => 404, 'msg' => '文件不存在'], 320));}try {$dataInfo = getData($filePath, $redis);$count = $dataInfo['count'];$isCached = $dataInfo['cached'];} catch (Exception $e) {$count = (int)countLines($filePath);$isCached = false;}$page = max(1, (int)($_GET['page'] ?? 1));$num = min(30, max(1, (int)($_GET['num'] ?? 15)));$maxPage = $count > 0 ? max(1, ceil($count / $num)) : 1; if ($page > $maxPage) {die(json_encode(['code' => 1, 'msg' => '已完成!'], 320));}$data = getPageData($filePath, $page, $num);$cacheStatus = $isCached ? ($dataInfo['freshCount'] ?? 'hit') : 'miss';if (isset($dataInfo['freshCount'])) {$cacheStatus = $dataInfo['freshCount'] !== false ? 'refreshed' : 'hit';} else {$cacheStatus = $isCached ? 'hit' : 'miss';}echo json_encode(['code' => 200,'title' =>'SEO超级外链工具','data' => $data,'sitename' => $public_r['sitename'],'count' => $count,'page' => $page,'pagesize' => $num,'maxpage' => $maxPage,'cache_status' => $cacheStatus,'cache_meta' => $redis->hGetAll($redisMetaKey)], 320);
复制代码 不能照抄,因为小编在帝国的核心函数里面已经把redis链接已经初始化了,自定义函数里面只需要作为全局变量就缓存生成读取都生效了。外链留痕数据TXT文件更新了会自动更新缓存的。 |