feat(controllers): optimize caching and view data passing

- Refactored the ChampionController, ChampionSkinController, HomeController, SaleController, SummonerEmoteController, and SummonerIconController to use arrow functions for cache callbacks.
- Updated the view data passing in the ChampionController, ChampionSkinController, HomeController, PostsController, SaleController, SummonerEmoteController, and SummonerIconController to use associative arrays instead of compact().
- Removed unused imports from web.php.
This commit is contained in:
Rico van Zelst
2024-01-06 00:15:19 +01:00
parent 3717d3836e
commit 85e3c4ff2e
8 changed files with 43 additions and 87 deletions

View File

@@ -17,15 +17,11 @@ class ChampionController extends Controller
{ {
$eightHoursInSeconds = 60 * 60 * 8; $eightHoursInSeconds = 60 * 60 * 8;
$champions = Cache::remember('championsListAllCache', $eightHoursInSeconds, function () { $champions = Cache::remember('championsListAllCache', $eightHoursInSeconds, static fn() => Champion::orderBy('name')->get());
return Champion::orderBy('name')->get();
});
$roles = Cache::remember('championsRolesCache', $eightHoursInSeconds, function () { $roles = Cache::remember('championsRolesCache', $eightHoursInSeconds, static fn() => ChampionRoles::orderBy('champion_name')->get());
return ChampionRoles::orderBy('champion_name')->get();
});
return view('champions.index', compact('champions', 'roles')); return view('champions.index', ['champions' => $champions, 'roles' => $roles]);
} }
/** /**
@@ -52,21 +48,17 @@ class ChampionController extends Controller
$eightHoursInSeconds = 60 * 60 * 8; $eightHoursInSeconds = 60 * 60 * 8;
$dayInSeconds = 60 * 60 * 24; $dayInSeconds = 60 * 60 * 24;
$champion = Cache::remember('championShowCache' . $champion->slug, $eightHoursInSeconds, function () use ($champion) { $champion = Cache::remember('championShowCache' . $champion->slug, $eightHoursInSeconds, static fn() => $champion->load('skins', 'lanes'));
return $champion->load('skins', 'lanes');
});
$splashColor = Cache::remember( $splashColor = Cache::remember(
'championSplashColorCache' . $champion->slug, 'championSplashColorCache' . $champion->slug,
$dayInSeconds, $dayInSeconds,
function () use ($champion) { static fn() => getAverageColorFromImageUrl($champion->getChampionImageAttribute())
return getAverageColorFromImageUrl($champion->getChampionImageAttribute());
}
); );
$champion->splash_color = $splashColor; $champion->splash_color = $splashColor;
return view('champions.show', compact('champion')); return view('champions.show', ['champion' => $champion]);
} }
/** /**

View File

@@ -30,7 +30,7 @@ class ChampionSkinController extends Controller
'Ultimate' => 'text-yellow-400', 'Ultimate' => 'text-yellow-400',
]; ];
return view('skins.index', compact('skins', 'rarityColor')); return view('skins.index', ['skins' => $skins, 'rarityColor' => $rarityColor]);
} }
/** /**
@@ -57,22 +57,18 @@ class ChampionSkinController extends Controller
$skin = Cache::remember( $skin = Cache::remember(
'championSkinShowCache' . $championSkin->slug, 'championSkinShowCache' . $championSkin->slug,
60 * 60 * 8, 60 * 60 * 8,
function () use ($championSkin) { static fn() => $championSkin->load('champion', 'chromas')
return $championSkin->load('champion', 'chromas');
}
); );
$splashColor = Cache::remember( $splashColor = Cache::remember(
'championSkinSplashColorCache' . $championSkin->slug, 'championSkinSplashColorCache' . $championSkin->slug,
60 * 60 * 24, 60 * 60 * 24,
function () use ($championSkin) { static fn() => getAverageColorFromImageUrl($championSkin->getSkinImageAttribute())
return getAverageColorFromImageUrl($championSkin->getSkinImageAttribute());
}
); );
$skin->splash_color = $splashColor; $skin->splash_color = $splashColor;
return view('skins.show', compact('skin')); return view('skins.show', ['skin' => $skin]);
} }
/** /**

View File

@@ -9,15 +9,11 @@ class HomeController extends Controller
{ {
public function index() public function index()
{ {
$upcomingSkins = Cache::remember('upcomingSkins_home', 60 * 4, function () { $upcomingSkins = Cache::remember('upcomingSkins_home', 60 * 4, static fn() => ChampionSkin::where('availability', 'Upcoming')
return ChampionSkin::where('availability', 'Upcoming') ->orderBy('release_date', 'desc')->get());
->orderBy('release_date', 'desc')->get();
});
$latestSkins = Cache::remember('latestSkins_home', 60 * 4, function () { $latestSkins = Cache::remember('latestSkins_home', 60 * 4, static fn() => ChampionSkin::where('availability', 'Available')
return ChampionSkin::where('availability', 'Available') ->orderBy('release_date', 'desc')->take(9)->get());
->orderBy('release_date', 'desc')->take(9)->get();
});
return view('home', [ return view('home', [
'latestSkins' => $latestSkins, 'latestSkins' => $latestSkins,

View File

@@ -20,6 +20,6 @@ class PostsController extends Controller
public function show(Sheet $post) public function show(Sheet $post)
{ {
return view('posts.show', compact('post')); return view('posts.show', ['post' => $post]);
} }
} }

View File

@@ -8,17 +8,15 @@ class SaleController extends Controller
{ {
public function index() public function index()
{ {
$sales = Cache::remember('sales_data', 60 * 60 * 8, function () { $sales = Cache::remember('sales_data', 60 * 60 * 8, static function () {
$shopData = json_decode( $shopData = json_decode(
file_get_contents('https://api.shop.riotgames.com/v3/collections/'), file_get_contents('https://api.shop.riotgames.com/v3/collections/'),
true true
); );
$salesData = array_filter($shopData, function ($collection) { $salesData = array_filter($shopData, static fn($collection) => $collection['path'] === '/event/sales');
return $collection['path'] === '/event/sales';
});
return reset($salesData)['dynamicCollection']['discountedProductsByProductType'] ?? []; return reset($salesData)['dynamicCollection']['discountedProductsByProductType'] ?? [];
}); });
return view('sales.index', compact('sales')); return view('sales.index', ['sales' => $sales]);
} }
} }

View File

@@ -16,7 +16,7 @@ class SummonerEmoteController extends Controller
->paginate(72) ->paginate(72)
->appends(request()->query()); ->appends(request()->query());
return view('emotes.index', compact('emotes')); return view('emotes.index', ['emotes' => $emotes]);
} }
public function store(Request $request) public function store(Request $request)

View File

@@ -16,7 +16,7 @@ class SummonerIconController extends Controller
->paginate(72) ->paginate(72)
->appends(request()->query()); ->appends(request()->query());
return view('icons.index', compact('icons')); return view('icons.index', ['icons' => $icons]);
} }
public function store(Request $request) public function store(Request $request)
@@ -40,7 +40,7 @@ class SummonerIconController extends Controller
{ {
$icon = $summonerIcon; $icon = $summonerIcon;
return view('icons.show', compact('icon')); return view('icons.show', ['icon' => $icon]);
} }
public function update(Request $request, SummonerIcon $summonerIcon) public function update(Request $request, SummonerIcon $summonerIcon)

View File

@@ -3,14 +3,17 @@
use App\Http\Controllers\AboutController; use App\Http\Controllers\AboutController;
use App\Http\Controllers\AssetsController; use App\Http\Controllers\AssetsController;
use App\Http\Controllers\ChampionController; use App\Http\Controllers\ChampionController;
use App\Http\Controllers\ChampionSkinController;
use App\Http\Controllers\FAQController; use App\Http\Controllers\FAQController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostsController; use App\Http\Controllers\PostsController;
use App\Http\Controllers\SaleController; use App\Http\Controllers\SaleController;
use App\Http\Controllers\SummonerEmoteController; use App\Http\Controllers\SummonerEmoteController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ChampionSkinController;
use App\Http\Controllers\SummonerIconController; use App\Http\Controllers\SummonerIconController;
use App\Models\Champion;
use App\Models\SummonerIcon;
use Illuminate\Support\Facades\Route;
use Spatie\Sheets\Sheet;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -23,72 +26,43 @@ use App\Http\Controllers\SummonerIconController;
| |
*/ */
Route::get('/', [HomeController::class, 'index']); Route::get('/', static fn () => (new HomeController())->index());
// Champions // Champions
Route::get('/champions', [ChampionController::class, 'index'])->name('champions.index'); Route::get('/champions', static fn () => (new ChampionController())->index())->name('champions.index');
Route::get('/champion/{champion}', [ChampionController::class, 'show'])->name('champions.show'); Route::get('/champion/{champion}', static fn (Champion $champion) => (new ChampionController())->show($champion))->name('champions.show');
// Skins // Skins
Route::get('/skins', [ChampionSkinController::class, 'index'])->name('skins.index'); Route::get('/skins', static fn () => (new ChampionSkinController())->index())->name('skins.index');
Route::get( Route::get(
'/skin/{championSkin}', '/skin/{championSkin}',
[ChampionSkinController::class, 'show'] static fn (\App\Models\ChampionSkin $championSkin) => (new ChampionSkinController())->show($championSkin)
)->name('skins.show'); )->name('skins.show');
// Icons // Icons
Route::get('/icons', [ Route::get('/icons', static fn () => (new SummonerIconController())->index())->name('assets.icons.index');
SummonerIconController::class, Route::get('/icon/{summonerIcon}', static fn (SummonerIcon $summonerIcon) => (new SummonerIconController())->show($summonerIcon))->name('assets.icons.show');
'index'
])->name('assets.icons.index');
Route::get('/icon/{summonerIcon}', [
SummonerIconController::class,
'show'
])->name('assets.icons.show');
// Emotes // Emotes
Route::get('/emotes', [ Route::get('/emotes', static fn () => (new SummonerEmoteController())->index())->name('assets.emotes.index');
SummonerEmoteController::class,
'index'
])->name('assets.emotes.index');
// Assets // Assets
Route::get('/assets', [ Route::get('/assets', static fn () => (new AssetsController())->index())->name('assets.index');
AssetsController::class,
'index'
])->name('assets.index');
// Sales // Sales
Route::get('/sale-rotation', [SaleController::class, 'index'])->name('sales.index'); Route::get('/sale-rotation', static fn () => (new SaleController())->index())->name('sales.index');
// About // About
Route::get('/about', [ Route::get('/about', static fn () => (new AboutController())->index())->name('about.index');
AboutController::class,
'index'
])->name('about.index');
// About.FAQController // About.FAQController
Route::get('/about/faq/league-of-legends', [ Route::get('/about/faq/league-of-legends', static fn () => (new FAQController())->leagueoflegends())->name('about.faq.leagueoflegends');
FAQController::class,
'leagueoflegends'
])->name('about.faq.leagueoflegends');
Route::get('/about/faq/heimerdinger', [ Route::get('/about/faq/heimerdinger', static fn () => (new FAQController())->heimerdinger())->name('about.faq.heimerdinger');
FAQController::class,
'heimerdinger'
])->name('about.faq.heimerdinger');
// Posts // Posts
Route::get('/posts', [ Route::get('/posts', static fn () => (new PostsController())->index())->name('posts.index');
PostsController::class,
'index'
])->name('posts.index');
Route::get('/post/{post}', [ Route::get('/post/{post}', static fn (Sheet $post) => (new PostsController())->show($post))->name('posts.show');
PostsController::class,
'show'
])->name('posts.show');
// Pulse // Pulse
Route::get(config('app.login_route'), function () { Route::get(config('app.login_route'), static fn () => redirect('/pulse'))->name('login')->middleware('auth.basic');
return redirect('/pulse');
})->name('login')->middleware('auth.basic');