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); return json_decode($cachedData, true);
} }
// 没有缓存时重新计算 // 没有缓存时重新计算
$countData = self::calculateCountData($merchantId,$customerIds, $startDate, $endDate); $countData = self::calculateCountData($merchantId, $customerIds, $startDate, $endDate);
// 缓存到 Redis有效期 10 分钟 // 缓存到 Redis有效期 10 分钟
Redis::setex($redisKey, 600, json_encode($countData)); Redis::setex($redisKey, 600, json_encode($countData));
@ -89,7 +89,7 @@ class AdsInsightService
[$startDate, $endDate] = self::getLastWeekDateRange(); [$startDate, $endDate] = self::getLastWeekDateRange();
} }
return [ 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), 'campaign_list_count' => self::getCampaignListCount($customerIds, $startDate, $endDate),
'adset_list_count' => self::getAdsetListCount($customerIds, $startDate, $endDate), 'adset_list_count' => self::getAdsetListCount($customerIds, $startDate, $endDate),
'ad_list_count' => self::getAdListCount($customerIds, $startDate, $endDate), 'ad_list_count' => self::getAdListCount($customerIds, $startDate, $endDate),
@ -133,9 +133,9 @@ class AdsInsightService
/** /**
* 获取账户列表的 count * 获取账户列表的 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) 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); $creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $requireSpend, false);
@ -1428,26 +1428,47 @@ class AdsInsightService
$adds_to_cart = array_sum(array_column($allAds, 'adds_to_cart')); $adds_to_cart = array_sum(array_column($allAds, 'adds_to_cart'));
$cost_per_purchase = $total_purchases == 0 ? 0 : round($total_spend / $total_purchases, 2); $cost_per_purchase = $total_purchases == 0 ? 0 : round($total_spend / $total_purchases, 2);
$statistics = [ // 生成唯一缓存键
'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))), $cacheKey = 'adlist_stats:' . md5(serialize([
'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))), $platformType,
'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2) . 'x', $customerIds,
'amount_spend' => '$' . number_format($total_spend, 2) ?: '$0.00', // 格式化支出 $page,
'clicks' => number_format($total_clicks), $pageSize,
'impressions' => number_format($total_impressions), $keyword,
'adds_to_cart' => number_format($adds_to_cart), $startDate,
'cost_per_atc' => $adds_to_cart == 0 ? '-' : '$' . number_format(($total_spend / $adds_to_cart), 2), $endDate,
'purchases' => number_format($total_purchases), $status,
'purchases_value' => array_sum(array_column($allAds, 'purchases_value')), $requireSpend
'cost_per_purchase' => '$' . number_format($cost_per_purchase, 2) ?: '$0.00', ]));
'revenue' => '$' . number_format(array_sum(array_column($allAds, 'revenue')), 2) ?: '$0.00', // 格式化收入 // 尝试从缓存读取统计数据
'total_cost' => '$' . number_format($total_cost, 2) ?: '$0.00', // 格式化总成本 if (!$countOnly && $cached = Redis::get($cacheKey)) {
'conversion_rate' => $total_clicks == 0 ? '-' : round(($total_purchases / $total_clicks) * 100, 2) . '%', // 转换率 $statistics = json_decode($cached, true);
'net_profit' => ($total_revenue - $total_cost) >= 0 ? '+$' . number_format($total_revenue - $total_cost, 2) : '-$' . number_format(abs($total_revenue - $total_cost), 2), // 净利润 // 跳过后续统计计算直接使用缓存数据...
'net_profit_margin' => $total_revenue == 0 ? '-' : round(($total_revenue - $total_cost) / $total_revenue, 2) * 100 . '%', // 净利润率 } else {
'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 平均广告花费的净利润 $statistics = [
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比 'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))),
]; 'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))),
'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2) . 'x',
'amount_spend' => '$' . number_format($total_spend, 2) ?: '$0.00', // 格式化支出
'clicks' => number_format($total_clicks),
'impressions' => number_format($total_impressions),
'adds_to_cart' => number_format($adds_to_cart),
'cost_per_atc' => $adds_to_cart == 0 ? '-' : '$' . number_format(($total_spend / $adds_to_cart), 2),
'purchases' => number_format($total_purchases),
'purchases_value' => array_sum(array_column($allAds, 'purchases_value')),
'cost_per_purchase' => '$' . number_format($cost_per_purchase, 2) ?: '$0.00',
'revenue' => '$' . number_format(array_sum(array_column($allAds, 'revenue')), 2) ?: '$0.00', // 格式化收入
'total_cost' => '$' . number_format($total_cost, 2) ?: '$0.00', // 格式化总成本
'conversion_rate' => $total_clicks == 0 ? '-' : round(($total_purchases / $total_clicks) * 100, 2) . '%', // 转换率
'net_profit' => ($total_revenue - $total_cost) >= 0 ? '+$' . number_format($total_revenue - $total_cost, 2) : '-$' . number_format(abs($total_revenue - $total_cost), 2), // 净利润
'net_profit_margin' => $total_revenue == 0 ? '-' : round(($total_revenue - $total_cost) / $total_revenue, 2) * 100 . '%', // 净利润率
'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2) * 100 . '%', // 平均广告花费的净利润
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比
];
// 将新生成的统计数据存入缓存10分钟有效期
Redis::setex($cacheKey, 600, json_encode($statistics));
}
// 获取分页数据 // 获取分页数据