getAdList方法中实现对$statistics的缓存10分钟

This commit is contained in:
huangguancheng 2025-02-22 11:10:33 +08:00 committed by hgc
parent ec69bc1d0a
commit 2744512839

View File

@ -1428,6 +1428,23 @@ class AdsInsightService
$adds_to_cart = array_sum(array_column($allAds, 'adds_to_cart'));
$cost_per_purchase = $total_purchases == 0 ? 0 : round($total_spend / $total_purchases, 2);
// 生成唯一缓存键
$cacheKey = 'adlist_stats:' . md5(serialize([
$platformType,
$customerIds,
$page,
$pageSize,
$keyword,
$startDate,
$endDate,
$status,
$requireSpend
]));
// 尝试从缓存读取统计数据
if (!$countOnly && $cached = Redis::get($cacheKey)) {
$statistics = json_decode($cached, true);
// 跳过后续统计计算直接使用缓存数据...
} else {
$statistics = [
'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))),
'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))),
@ -1449,6 +1466,10 @@ class AdsInsightService
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比
];
// 将新生成的统计数据存入缓存10分钟有效期
Redis::setex($cacheKey, 600, json_encode($statistics));
}
// 获取分页数据
$page = max(1, (int)$page); // 确保页码不小于 1