added report cache

This commit is contained in:
denisdulici 2020-01-25 13:36:57 +03:00
parent b92d0aa9cd
commit daf46b843a
5 changed files with 43 additions and 13 deletions

View File

@ -9,6 +9,7 @@ use App\Jobs\Common\DeleteReport;
use App\Jobs\Common\UpdateReport; use App\Jobs\Common\UpdateReport;
use App\Models\Common\Report; use App\Models\Common\Report;
use App\Utilities\Reports as Utility; use App\Utilities\Reports as Utility;
use Illuminate\Support\Facades\Cache;
class Reports extends Controller class Reports extends Controller
{ {
@ -28,9 +29,14 @@ class Reports extends Controller
continue; 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(); $icons[$report->id] = $class->getIcon();
$categories[$class->getCategory()][] = $report; $categories[$class->getCategory()][] = $report;
} }
@ -50,7 +56,12 @@ class Reports extends Controller
abort(403); 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(); $classes = Utility::getClasses();
$class = Utility::getClassInstance($report); $class = Utility::getClassInstance($report, false);
return view('common.reports.edit', compact('report', 'classes', 'class')); return view('common.reports.edit', compact('report', 'classes', 'class'));
} }
@ -221,4 +232,22 @@ class Reports extends Controller
'html' => $html, '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();
}
} }

View File

@ -41,7 +41,7 @@ class Reports
return $classes; return $classes;
} }
public static function getClassInstance($model, $get_totals = true) public static function getClassInstance($model, $load_data = true)
{ {
if (is_string($model)) { if (is_string($model)) {
$model = Report::where('class', $model)->first(); $model = Report::where('class', $model)->first();
@ -53,7 +53,7 @@ class Reports
$class = $model->class; $class = $model->class;
return new $class($model, $get_totals); return new $class($model, $load_data);
} }
public static function canRead($class) public static function canRead($class)

View File

@ -145,13 +145,12 @@ return [
'accounting' => 'Accounting', 'accounting' => 'Accounting',
'sort' => 'Sort', 'sort' => 'Sort',
'width' => 'Width', 'width' => 'Width',
'month' => 'Month',
'month' => 'Month', 'year' => 'Year',
'year' => 'Year', 'type_item_name' => 'Type an item name',
'no_data' => 'No data',
'type_item_name' => 'Type an item name', 'no_matching_data' => 'No matching data',
'no_data' => 'No data', 'clear_cache' => 'Clear Cache',
'no_matching_data' => 'No matching data',
'card' => [ 'card' => [
'name' => 'Name on Card', 'name' => 'Name on Card',

View File

@ -6,6 +6,7 @@
@permission('create-common-reports') @permission('create-common-reports')
<a href="{{ route('reports.create') }}" class="btn btn-success btn-sm header-button-top"><span class="fa fa-plus"></span> &nbsp;{{ trans('general.add_new') }}</a> <a href="{{ route('reports.create') }}" class="btn btn-success btn-sm header-button-top"><span class="fa fa-plus"></span> &nbsp;{{ trans('general.add_new') }}</a>
@endpermission @endpermission
<a href="{{ route('reports.clear') }}" class="btn btn-warning btn-sm header-button-top"><span class="fa fa-history"></span> &nbsp;{{ trans('general.clear_cache') }}</a>
@endsection @endsection
@section('content') @section('content')

View File

@ -37,6 +37,7 @@ Route::group(['prefix' => 'common'], function () {
Route::get('reports/{report}/print', 'Common\Reports@print')->name('reports.print'); 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/{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::get('reports/fields', 'Common\Reports@fields')->name('reports.fields');
Route::resource('reports', 'Common\Reports'); Route::resource('reports', 'Common\Reports');
}); });