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

@ -71,7 +71,7 @@ class AdsInsightService
return json_decode($cachedData, true);
}
// 没有缓存时重新计算
$countData = self::calculateCountData($merchantId,$customerIds, $startDate, $endDate);
$countData = self::calculateCountData($merchantId, $customerIds, $startDate, $endDate);
// 缓存到 Redis有效期 10 分钟
Redis::setex($redisKey, 600, json_encode($countData));
@ -89,7 +89,7 @@ class AdsInsightService
[$startDate, $endDate] = self::getLastWeekDateRange();
}
return [
'account_list_count' => self::getAccountListCount($merchantId,$customerIds, $startDate, $endDate),
'account_list_count' => self::getAccountListCount($merchantId, $customerIds, $startDate, $endDate),
'campaign_list_count' => self::getCampaignListCount($customerIds, $startDate, $endDate),
'adset_list_count' => self::getAdsetListCount($customerIds, $startDate, $endDate),
'ad_list_count' => self::getAdListCount($customerIds, $startDate, $endDate),
@ -133,9 +133,9 @@ class AdsInsightService
/**
* 获取账户列表的 count
*/
protected static function getAccountListCount($merchantId,$customerIds, $startDate, $endDate)
protected static function getAccountListCount($merchantId, $customerIds, $startDate, $endDate)
{
return self::getAccountList(0, $merchantId,$customerIds, 1, 1, '', $startDate, $endDate, false, true);
return self::getAccountList(0, $merchantId, $customerIds, 1, 1, '', $startDate, $endDate, false, true);
}
/**
@ -143,7 +143,7 @@ class AdsInsightService
*/
protected static function getCreativeListCount(array $customerIds, $startDate, $endDate)
{
return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, false,true);
return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, false, true);
}
/**
@ -721,7 +721,7 @@ class AdsInsightService
}
}
public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null,$requireSpend = false)
public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false)
{
// 获取广告创意数据
$creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $requireSpend, false);
@ -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