|
@@ -18,6 +18,7 @@ use App\JsonRpc\UserServiceInterface;
|
|
use PhpParser\Node\Stmt\Return_;
|
|
use PhpParser\Node\Stmt\Return_;
|
|
use Hyperf\Redis\Redis;
|
|
use Hyperf\Redis\Redis;
|
|
use function Hyperf\Support\env;
|
|
use function Hyperf\Support\env;
|
|
|
|
+use Swoole\Coroutine;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Class WebsiteController
|
|
* Class WebsiteController
|
|
@@ -757,6 +758,7 @@ class WebsiteController extends AbstractController
|
|
$typeId = intval(Context::get("TypeId"));
|
|
$typeId = intval(Context::get("TypeId"));
|
|
$websiteId = intval(Context::get("WebsiteId"));
|
|
$websiteId = intval(Context::get("WebsiteId"));
|
|
$cacheKey = "admin_index_processed_data_{$typeId}";
|
|
$cacheKey = "admin_index_processed_data_{$typeId}";
|
|
|
|
+ $cacheKeyLong = "admin_index_processed_data_{$typeId}_long";
|
|
$originUrl = (string)Context::get("originUrl");
|
|
$originUrl = (string)Context::get("originUrl");
|
|
// 如果originUrl含有localhost,就根据env是dev 换成 http://nwpre.bjzxtw.org.cn,根据env是prod 换成 http://nwpre.bjzxtw.org.cn
|
|
// 如果originUrl含有localhost,就根据env是dev 换成 http://nwpre.bjzxtw.org.cn,根据env是prod 换成 http://nwpre.bjzxtw.org.cn
|
|
if (strpos($originUrl, "localhost") !== false) {
|
|
if (strpos($originUrl, "localhost") !== false) {
|
|
@@ -806,18 +808,40 @@ class WebsiteController extends AbstractController
|
|
case 10000:
|
|
case 10000:
|
|
// 检查 Redis 中是否存在缓存
|
|
// 检查 Redis 中是否存在缓存
|
|
$cachedData = $this->redis->get($cacheKey);
|
|
$cachedData = $this->redis->get($cacheKey);
|
|
|
|
+ $cachedDataLong = $this->redis->get($cacheKeyLong);
|
|
var_dump("缓存数据:", $cachedData);
|
|
var_dump("缓存数据:", $cachedData);
|
|
- if ($cachedData) {
|
|
|
|
- // 如果缓存存在,直接返回缓存数据
|
|
|
|
|
|
+ if ($cachedDataLong) {
|
|
|
|
+ // 情况1:有长缓存,直接返回长缓存数据
|
|
|
|
+ $result = json_decode($cachedDataLong, true);
|
|
|
|
+
|
|
|
|
+ // 异步刷新短缓存和长缓存(可以放后台执行)
|
|
|
|
+ Coroutine::create(function () use ($cacheKey, $cacheKeyLong, $typeId) {
|
|
|
|
+ $freshResult = $this->websiteServiceClient->getAdminIndex(['type_id' => $typeId]);
|
|
|
|
+ if ($freshResult && $freshResult['code'] == 200) {
|
|
|
|
+ $data = json_encode($freshResult);
|
|
|
|
+ $this->redis->setex($cacheKey, 60 * 5, $data);
|
|
|
|
+ $this->redis->setex($cacheKeyLong, 60 * 60 * 24, $data);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } elseif ($cachedData) {
|
|
|
|
+ // 情况2:没有长缓存,但有短缓存
|
|
$result = json_decode($cachedData, true);
|
|
$result = json_decode($cachedData, true);
|
|
|
|
+
|
|
|
|
+ // 用短缓存刷新长缓存
|
|
|
|
+ $this->redis->setex($cacheKeyLong, 60 * 60 * 24, $cachedData); // 设置长缓存
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
- // 如果缓存不存在,处理数据并存入缓存
|
|
|
|
|
|
+ // 情况3:都没有,查数据库
|
|
$result = $this->websiteServiceClient->getAdminIndex(['type_id' => $typeId]);
|
|
$result = $this->websiteServiceClient->getAdminIndex(['type_id' => $typeId]);
|
|
|
|
+
|
|
if (!$result || $result['code'] != 200) {
|
|
if (!$result || $result['code'] != 200) {
|
|
return Result::error($result['message'] ?? '获取数据失败');
|
|
return Result::error($result['message'] ?? '获取数据失败');
|
|
}
|
|
}
|
|
- // 将处理后的数据存入 Redis,设置 1 天(86400 秒)的过期时间
|
|
|
|
- $this->redis->setex($cacheKey, 60 * 5, json_encode($result));
|
|
|
|
|
|
+
|
|
|
|
+ // 同时写入短缓存和长缓存
|
|
|
|
+ $jsonData = json_encode($result);
|
|
|
|
+ $this->redis->setex($cacheKey, 60 * 5, $jsonData); // 短缓存 5分钟
|
|
|
|
+ $this->redis->setex($cacheKeyLong, 60 * 60 * 24, $jsonData); // 长缓存 24小时
|
|
}
|
|
}
|
|
$data = $result['data'];
|
|
$data = $result['data'];
|
|
$userTypeMapping = [
|
|
$userTypeMapping = [
|