From 6da3af1364069bb950adeee9b9e5e598e10210de Mon Sep 17 00:00:00 2001 From: cp6 Date: Mon, 9 May 2022 14:57:38 +1000 Subject: [PATCH] Fixed OS cache Fixed OS cache --- app/Http/Controllers/ApiController.php | 2 +- app/Http/Controllers/OsController.php | 6 +++--- app/Http/Controllers/ServerController.php | 2 +- app/Models/OS.php | 9 +++++++++ app/View/Components/OsSelect.php | 5 +---- resources/views/components/os-select.blade.php | 4 ++-- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index 9c82e6f..89b64dd 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -230,7 +230,7 @@ class ApiController extends Controller protected function getAllOs() { - $os = OS::all()->toJson(JSON_PRETTY_PRINT); + $os = OS::allOS()->toJson(JSON_PRETTY_PRINT); return response($os, 200); } diff --git a/app/Http/Controllers/OsController.php b/app/Http/Controllers/OsController.php index 784b6b5..9a8b5e2 100644 --- a/app/Http/Controllers/OsController.php +++ b/app/Http/Controllers/OsController.php @@ -10,7 +10,7 @@ class OsController extends Controller { public function index() { - $os = OS::all(); + $os = OS::allOS(); return view('os.index', compact(['os'])); } @@ -29,7 +29,7 @@ class OsController extends Controller 'name' => $request->os_name ]); - Cache::forget('all_os'); + Cache::forget('operating_systems'); return redirect()->route('os.index') ->with('success', 'OS Created Successfully.'); @@ -41,7 +41,7 @@ class OsController extends Controller $items->delete(); - Cache::forget('all_os'); + Cache::forget('operating_systems'); return redirect()->route('os.index') ->with('success', 'OS was deleted Successfully.'); diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 9a274e8..eb9a1ab 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -83,7 +83,7 @@ class ServerController extends Controller { $Providers = Providers::all(); $Locations = Locations::all(); - $Os = OS::all(); + $Os = OS::allOS(); return view('servers.create', compact(['Providers', 'Locations', 'Os'])); } diff --git a/app/Models/OS.php b/app/Models/OS.php index 6e28104..9b2e7e5 100644 --- a/app/Models/OS.php +++ b/app/Models/OS.php @@ -4,6 +4,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; class OS extends Model { @@ -12,4 +14,11 @@ class OS extends Model protected $fillable = ['name']; protected $table = 'os'; + + public static function allOS(): array + { + return Cache::remember("operating_systems", now()->addMonth(1), function () { + return DB::table('os')->get()->toArray(); + }); + } } diff --git a/app/View/Components/OsSelect.php b/app/View/Components/OsSelect.php index e7f8b07..2036155 100644 --- a/app/View/Components/OsSelect.php +++ b/app/View/Components/OsSelect.php @@ -10,11 +10,8 @@ class OsSelect extends Component { public function render() { - $all_os = Cache::rememberForever('all_os', function () { - return OS::all(); - }); return view('components.os-select', [ - 'os' => $all_os + 'os' => OS::allOS() ]); } } diff --git a/resources/views/components/os-select.blade.php b/resources/views/components/os-select.blade.php index 09749ad..fdbf0ee 100644 --- a/resources/views/components/os-select.blade.php +++ b/resources/views/components/os-select.blade.php @@ -2,8 +2,8 @@
{{ $title ??'OS'}}