added cache to preordercampaign

This commit is contained in:
Luca Haid
2025-03-06 13:28:43 +01:00
parent a486c1521c
commit fc222cd7b9

View File

@@ -27,6 +27,28 @@ class PreordercampaignController extends mfBaseController {
$campaigns = array_map(function ($c) {
// Helper function for field caching
$getCachedField = function ($campaignId, $cacheType, $callback) {
$cacheKey = md5($campaignId . $cacheType);
$cacheDir = TEMP_DIR . "/Preordercampaign/";
$cacheFile = $cacheDir . $cacheKey . ".json";
// Create directory if missing
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
// Return cached value if fresh
if (file_exists($cacheFile) && time() - filemtime($cacheFile) < 3600) {
return json_decode(file_get_contents($cacheFile), true);
}
// Generate and cache new value
$value = $callback();
file_put_contents($cacheFile, json_encode($value));
return $value;
};
return [
'id' => $c->id,
'network_id' => $c->network_id,
@@ -36,10 +58,16 @@ class PreordercampaignController extends mfBaseController {
'area' => $c->area,
'from' => $c->from,
'to' => $c->to,
'rimo_workoders' => $c->workorder_count,
'active_preorder_count' => PreorderModel::countActivePreorder($c->id),
'count_total_units' => PreorderModel::countTotalUnits($c->id),
];
'rimo_workoders' => $getCachedField($c->id, 'workorders', function() use ($c) {
return $c->workorder_count;
}),
'active_preorder_count' => $getCachedField($c->id, 'active_count', function() use ($c) {
return PreorderModel::countActivePreorder($c->id);
}),
'count_total_units' => $getCachedField($c->id, 'total_units', function() use ($c) {
return PreorderModel::countTotalUnits($c->id);
}),
];
}, PreordercampaignModel::search($filter));
$net_owners = array_map(function ($n) {