Files
Gnuboard7/app/Seo/SeoCacheManager.php
T
2026-04-01 10:30:52 +09:00

232 lines
5.4 KiB
PHP

<?php
namespace App\Seo;
use App\Seo\Contracts\SeoCacheManagerInterface;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class SeoCacheManager implements SeoCacheManagerInterface
{
/**
* 캐시 키 프리픽스
*/
private const CACHE_PREFIX = 'seo:page:';
/**
* 캐시된 URL 인덱스 키
*/
private const INDEX_KEY = 'seo:cached_urls';
/**
* {@inheritdoc}
*/
public function get(string $url, string $locale): ?string
{
if (! $this->isEnabled()) {
return null;
}
$key = $this->buildKey($url, $locale);
return Cache::get($key);
}
/**
* {@inheritdoc}
*/
public function put(string $url, string $locale, string $html): void
{
if (! $this->isEnabled()) {
return;
}
$key = $this->buildKey($url, $locale);
$ttl = (int) g7_core_settings('seo.cache_ttl', 7200);
Cache::put($key, $html, $ttl);
// URL 인덱스 업데이트
$this->addToIndex($url, $locale, $key);
}
/**
* {@inheritdoc}
*/
public function invalidateByUrl(string $urlPattern): int
{
$count = 0;
$index = $this->getIndex();
foreach ($index as $entry) {
if ($this->matchesPattern($entry['url'], $urlPattern)) {
Cache::forget($entry['key']);
$count++;
}
}
if ($count > 0) {
$this->rebuildIndex();
}
return $count;
}
/**
* {@inheritdoc}
*/
public function invalidateByLayout(string $layoutName): int
{
$count = 0;
$index = $this->getIndex();
foreach ($index as $entry) {
if (($entry['layout'] ?? '') === $layoutName) {
Cache::forget($entry['key']);
$count++;
}
}
if ($count > 0) {
$this->rebuildIndex();
}
return $count;
}
/**
* {@inheritdoc}
*/
public function clearAll(): void
{
$index = $this->getIndex();
foreach ($index as $entry) {
Cache::forget($entry['key']);
}
Cache::forget(self::INDEX_KEY);
Log::info('[SEO] All cache cleared', ['count' => count($index)]);
}
/**
* {@inheritdoc}
*/
public function getCachedUrls(): array
{
return array_map(fn ($entry) => $entry['url'], $this->getIndex());
}
/**
* 캐시 키 빌드 (URL + 로케일)
*
* @param string $url URL
* @param string $locale 로케일
* @return string 캐시 키
*/
private function buildKey(string $url, string $locale): string
{
return self::CACHE_PREFIX.md5($url.'|'.$locale);
}
/**
* 캐시 활성화 여부 확인
*/
private function isEnabled(): bool
{
return (bool) g7_core_settings('seo.cache_enabled', true);
}
/**
* URL 인덱스에 항목을 추가합니다.
*
* @param string $url URL
* @param string $locale 로케일
* @param string $key 캐시 키
*/
private function addToIndex(string $url, string $locale, string $key): void
{
$index = $this->getIndex();
$index[$key] = [
'url' => $url,
'locale' => $locale,
'key' => $key,
'cached_at' => now()->toIso8601String(),
];
Cache::put(self::INDEX_KEY, $index, 86400 * 30); // 30일
}
/**
* 캐시 인덱스를 조회합니다.
*/
private function getIndex(): array
{
return Cache::get(self::INDEX_KEY, []);
}
/**
* 유효한 캐시만 남겨 인덱스를 재구성합니다.
*/
private function rebuildIndex(): void
{
$index = $this->getIndex();
$validIndex = [];
foreach ($index as $key => $entry) {
if (Cache::has($entry['key'])) {
$validIndex[$key] = $entry;
}
}
Cache::put(self::INDEX_KEY, $validIndex, 86400 * 30);
}
/**
* URL이 패턴에 매칭되는지 확인합니다.
*
* @param string $url URL
* @param string $pattern 패턴 (와일드카드 * 지원)
*/
private function matchesPattern(string $url, string $pattern): bool
{
// 와일드카드를 정규식으로 변환
$regex = str_replace(['*', '/'], ['.*', '\/'], $pattern);
return (bool) preg_match('/^'.$regex.'$/', $url);
}
/**
* 캐시 저장 시 레이아웃 정보를 함께 저장합니다.
*
* @param string $url URL
* @param string $locale 로케일
* @param string $html HTML
* @param string $layoutName 레이아웃명
*/
public function putWithLayout(string $url, string $locale, string $html, string $layoutName): void
{
if (! $this->isEnabled()) {
return;
}
$key = $this->buildKey($url, $locale);
$ttl = (int) g7_core_settings('seo.cache_ttl', 7200);
Cache::put($key, $html, $ttl);
// 레이아웃 정보 포함하여 인덱스 업데이트
$index = $this->getIndex();
$index[$key] = [
'url' => $url,
'locale' => $locale,
'key' => $key,
'layout' => $layoutName,
'cached_at' => now()->toIso8601String(),
];
Cache::put(self::INDEX_KEY, $index, 86400 * 30);
}
}