From 179233385b520dccd753734550f26f0b30120744 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 24 Oct 2024 17:00:14 +1100 Subject: [PATCH] Cache our echo and file area stats for page rendering performance reasons --- app/Models/Domain.php | 62 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/app/Models/Domain.php b/app/Models/Domain.php index 20ba0f4..190f4eb 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -88,22 +88,24 @@ class Domain extends Model public function echoarea_stats(): Collection { - $dt = Carbon::now()->startOfday(); - $case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDay()->format('Y-m-d'))->thenRaw("'day'") - ->whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'") - ->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'") - ->elseRaw("'all'"); + return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() { + $dt = Carbon::now()->startOfday(); + $case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDay()->format('Y-m-d'))->thenRaw("'day'") + ->whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'") + ->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'") + ->elseRaw("'all'"); - return Echoarea::select(['echoareas.id','name','description','active',DB::raw('count(echomails.id) AS count'),DB::raw('min(datetime) as first_message'),DB::raw('max(datetime) as last_message')]) - ->selectRaw($case->toRaw().' AS stats') - ->join('echomails',['echomails.echoarea_id'=>'echoareas.id'],NULL,NULL,'left outer') - ->where('domain_id',$this->id) - ->groupBy('echoareas.id') - ->groupBy('echoareas.name') - ->groupBy('stats') - ->orderBy('echoareas.name') - ->orderBy('last_message','DESC') - ->get(); + return Echoarea::select(['echoareas.id','name','description','active',DB::raw('count(echomails.id) AS count'),DB::raw('min(datetime) as first_message'),DB::raw('max(datetime) as last_message')]) + ->selectRaw($case->toRaw().' AS stats') + ->join('echomails',['echomails.echoarea_id'=>'echoareas.id'],NULL,NULL,'left outer') + ->where('domain_id',$this->id) + ->groupBy('echoareas.id') + ->groupBy('echoareas.name') + ->groupBy('stats') + ->orderBy('echoareas.name') + ->orderBy('last_message','DESC') + ->get(); + }); } public function echoarea_total_daily(Collection $systems=NULL): Collection @@ -126,21 +128,23 @@ class Domain extends Model public function filearea_stats() { - $dt = Carbon::now()->startOfday(); - $case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'") - ->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'") - ->elseRaw("'all'"); + return Cache::remember(md5(sprintf('%d-%s',$this->id,__METHOD__)),self::CACHE_TIME,function() { + $dt = Carbon::now()->startOfday(); + $case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'") + ->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'") + ->elseRaw("'all'"); - return Filearea::select(['fileareas.id','fileareas.name','description','active',DB::raw('count(files.id) AS count'),DB::raw('min(datetime) as first_file'),DB::raw('max(datetime) as last_file')]) - ->selectRaw($case->toRaw().' AS stats') - ->join('files',['files.filearea_id'=>'fileareas.id'],NULL,NULL,'left outer') - ->where('domain_id',$this->id) - ->groupBy('fileareas.id') - ->groupBy('fileareas.name') - ->groupBy('stats') - ->orderBy('fileareas.name') - ->orderBy('last_file','DESC') - ->get(); + return Filearea::select(['fileareas.id','fileareas.name','description','active',DB::raw('count(files.id) AS count'),DB::raw('min(datetime) as first_file'),DB::raw('max(datetime) as last_file')]) + ->selectRaw($case->toRaw().' AS stats') + ->join('files',['files.filearea_id'=>'fileareas.id'],NULL,NULL,'left outer') + ->where('domain_id',$this->id) + ->groupBy('fileareas.id') + ->groupBy('fileareas.name') + ->groupBy('stats') + ->orderBy('fileareas.name') + ->orderBy('last_file','DESC') + ->get(); + }); } /**