perf: optimize database queries and caching

This commit is contained in:
Rico van Zelst
2026-01-01 00:41:38 +01:00
parent 329e071e2b
commit dd79ffcf98
9 changed files with 147 additions and 101 deletions

View File

@@ -15,7 +15,7 @@ function getRoleIcon($roleName): string
'Support' => 'gm-support.png',
];
return asset('img/'.$roleIcons[$roleName]);
return asset('img/' . $roleIcons[$roleName]);
}
function getAverageColorFromImageUrl($imageUrl): string
@@ -75,9 +75,13 @@ function getRoleIconSvg($roleName): string
*/
function getChampionImage($full_id, $type): string
{
$championImage = ChampionImage::where('full_id', $full_id)->where('type', $type)->first();
$cacheKey = "champion_image_{$full_id}_{$type}";
if (! $championImage) {
$championImage = Cache::remember($cacheKey, 60 * 60 * 24, static function () use ($full_id, $type) {
return ChampionImage::where('full_id', $full_id)->where('type', $type)->first();
});
if (!$championImage) {
return '';
}
@@ -92,7 +96,7 @@ function getCommitHash(): string
/**
* @var string $commit
*/
$commit = Cache::remember('commit_hash', 60 * 72, fn () => trim(exec('git log --pretty="%h" -n1 HEAD')));
$commit = Cache::remember('commit_hash', 60 * 72, fn() => trim(exec('git log --pretty="%h" -n1 HEAD')));
return $commit;
}

View File

@@ -31,7 +31,7 @@ class ChampionController extends Controller
$champion = Cache::remember('championShowCache' . $champion->slug, $threeDaysInSeconds, static fn() => $champion->load('streamers', 'skins', 'lanes'));
$streamers = $champion->load('streamers')->streamers;
$streamers = $champion->streamers;
return view('champions.show', ['champion' => $champion, 'streamers' => $streamers]);
}

View File

@@ -14,13 +14,13 @@ class HomeController extends Controller
$query->where('release_date', '0000-00-00')
->orWhere('release_date', '>', now());
})
->orderBy('release_date', 'desc')->get());
->orderBy('release_date', 'desc')->limit(6)->get());
$latestSkins = Cache::remember('latestSkins_home', 60 * 4, static fn() => ChampionSkin::where('release_date', '!=', '0000-00-00')
->where('availability', '!=', 'Upcoming')
->where('release_date', '<=', now())
->orderBy('release_date', 'desc')->get());
->orderBy('release_date', 'desc')->limit(6)->get());
return view('home', [
'latestSkins' => $latestSkins,

View File

@@ -4,17 +4,22 @@ namespace App\Http\Controllers;
use App\Models\SummonerEmote;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Spatie\QueryBuilder\QueryBuilder;
class SummonerEmoteController extends Controller
{
public function index()
{
$emotes = QueryBuilder::for(SummonerEmote::class)
->allowedFilters('title')
->defaultSort('-emote_id')
->paginate(72)
->appends(request()->query());
$cacheKey = 'emotes_' . md5(serialize(request()->query()));
$emotes = Cache::remember($cacheKey, 60 * 60, function () {
return QueryBuilder::for(SummonerEmote::class)
->allowedFilters('title')
->defaultSort('-emote_id')
->paginate(72)
->appends(request()->query());
});
return view('emotes.index', ['emotes' => $emotes]);
}

View File

@@ -4,17 +4,22 @@ namespace App\Http\Controllers;
use App\Models\SummonerIcon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Spatie\QueryBuilder\QueryBuilder;
class SummonerIconController extends Controller
{
public function index()
{
$icons = QueryBuilder::for(SummonerIcon::class)
->allowedFilters(['title', 'esports_team', 'release_year'])
->defaultSort('-icon_id')
->paginate(72)
->appends(request()->query());
$cacheKey = 'icons_' . md5(serialize(request()->query()));
$icons = Cache::remember($cacheKey, 60 * 60, function () {
return QueryBuilder::for(SummonerIcon::class)
->allowedFilters(['title', 'esports_team', 'release_year'])
->defaultSort('-icon_id')
->paginate(72)
->appends(request()->query());
});
return view('icons.index', ['icons' => $icons]);
}

View File

@@ -22,7 +22,7 @@ class ChampionSkin extends Model
'availability',
'loot_eligible',
'rp_price',
'raritiy',
'rarity',
'release_date',
'associated_skinline',
'new_effects',
@@ -70,12 +70,12 @@ class ChampionSkin extends Model
public function getSkinImageLoadingAttribute(): string
{
return 'https://cdn.communitydragon.org/latest/champion/'.$this->champion_id.'/portrait/skin/'.$this->skin_id;
return 'https://cdn.communitydragon.org/latest/champion/' . $this->champion_id . '/portrait/skin/' . $this->skin_id;
}
public function getSkinImageTileAttribute(): string
{
return 'https://cdn.communitydragon.org/latest/champion/'.$this->champion_id.'/tile/skin/'.$this->skin_id;
return 'https://cdn.communitydragon.org/latest/champion/' . $this->champion_id . '/tile/skin/' . $this->skin_id;
}
protected function casts(): array