From daf46b843a56d69231d717a3d20c7a091add3456 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Sat, 25 Jan 2020 13:36:57 +0300 Subject: [PATCH] added report cache --- app/Http/Controllers/Common/Reports.php | 37 +++++++++++++++++-- app/Utilities/Reports.php | 4 +- resources/lang/en-GB/general.php | 13 +++---- .../views/common/reports/index.blade.php | 1 + routes/admin.php | 1 + 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Common/Reports.php b/app/Http/Controllers/Common/Reports.php index a5052ed27..3287754fb 100644 --- a/app/Http/Controllers/Common/Reports.php +++ b/app/Http/Controllers/Common/Reports.php @@ -9,6 +9,7 @@ use App\Jobs\Common\DeleteReport; use App\Jobs\Common\UpdateReport; use App\Models\Common\Report; use App\Utilities\Reports as Utility; +use Illuminate\Support\Facades\Cache; class Reports extends Controller { @@ -28,9 +29,14 @@ class Reports extends Controller continue; } - $class = Utility::getClassInstance($report); + $class = Utility::getClassInstance($report, false); + + $ttl = 3600 * 6; // 6 hours + + $totals[$report->id] = Cache::remember('reports.totals.' . $report->id, $ttl, function () use ($class) { + return $class->getTotal(); + }); - $totals[$report->id] = $class->getTotal(); $icons[$report->id] = $class->getIcon(); $categories[$class->getCategory()][] = $report; } @@ -50,7 +56,12 @@ class Reports extends Controller abort(403); } - return Utility::getClassInstance($report)->show(); + $class = Utility::getClassInstance($report); + + // Update cache + Cache::put('reports.totals.' . $report->id, $class->getTotal()); + + return $class->show(); } /** @@ -103,7 +114,7 @@ class Reports extends Controller { $classes = Utility::getClasses(); - $class = Utility::getClassInstance($report); + $class = Utility::getClassInstance($report, false); return view('common.reports.edit', compact('report', 'classes', 'class')); } @@ -221,4 +232,22 @@ class Reports extends Controller 'html' => $html, ]); } + + /** + * Clear the cache of the resource. + * + * @return Response + */ + public function clear() + { + Report::all()->each(function ($report) { + if (!Utility::canRead($report->class)) { + return; + } + + Cache::put('reports.totals.' . $report->id, Utility::getClassInstance($report)->getTotal()); + }); + + return redirect()->back(); + } } diff --git a/app/Utilities/Reports.php b/app/Utilities/Reports.php index 21906f9ad..843ff7bd9 100644 --- a/app/Utilities/Reports.php +++ b/app/Utilities/Reports.php @@ -41,7 +41,7 @@ class Reports return $classes; } - public static function getClassInstance($model, $get_totals = true) + public static function getClassInstance($model, $load_data = true) { if (is_string($model)) { $model = Report::where('class', $model)->first(); @@ -53,7 +53,7 @@ class Reports $class = $model->class; - return new $class($model, $get_totals); + return new $class($model, $load_data); } public static function canRead($class) diff --git a/resources/lang/en-GB/general.php b/resources/lang/en-GB/general.php index f676b05f2..0dee65202 100644 --- a/resources/lang/en-GB/general.php +++ b/resources/lang/en-GB/general.php @@ -145,13 +145,12 @@ return [ 'accounting' => 'Accounting', 'sort' => 'Sort', 'width' => 'Width', - - 'month' => 'Month', - 'year' => 'Year', - - 'type_item_name' => 'Type an item name', - 'no_data' => 'No data', - 'no_matching_data' => 'No matching data', + 'month' => 'Month', + 'year' => 'Year', + 'type_item_name' => 'Type an item name', + 'no_data' => 'No data', + 'no_matching_data' => 'No matching data', + 'clear_cache' => 'Clear Cache', 'card' => [ 'name' => 'Name on Card', diff --git a/resources/views/common/reports/index.blade.php b/resources/views/common/reports/index.blade.php index 3de7091d1..d0645049d 100644 --- a/resources/views/common/reports/index.blade.php +++ b/resources/views/common/reports/index.blade.php @@ -6,6 +6,7 @@ @permission('create-common-reports')  {{ trans('general.add_new') }} @endpermission +  {{ trans('general.clear_cache') }} @endsection @section('content') diff --git a/routes/admin.php b/routes/admin.php index 30364de60..0517000f4 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -37,6 +37,7 @@ Route::group(['prefix' => 'common'], function () { Route::get('reports/{report}/print', 'Common\Reports@print')->name('reports.print'); Route::get('reports/{report}/export', 'Common\Reports@export')->name('reports.export'); + Route::get('reports/clear', 'Common\Reports@clear')->name('reports.clear'); Route::get('reports/fields', 'Common\Reports@fields')->name('reports.fields'); Route::resource('reports', 'Common\Reports'); });