chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting
This commit is contained in:
parent
425fed6ff6
commit
0e565970a5
42 changed files with 223 additions and 217 deletions
|
@ -8,8 +8,6 @@ abstract class ApiHandler extends BaseHandler
|
|||
{
|
||||
protected function ensureCliAccess(): void
|
||||
{
|
||||
if (php_sapi_name() !== 'cli' && $_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
$this->sendErrorResponse("Not Found", 404);
|
||||
}
|
||||
if (php_sapi_name() !== 'cli' && $_SERVER['REQUEST_METHOD'] !== 'POST') $this->sendErrorResponse("Not Found", 404);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,15 @@ class ArtistFetcher extends PageFetcher
|
|||
{
|
||||
$cacheKey = "artist_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$artist = $this->fetchSingleFromApi("optimized_artists", $url);
|
||||
|
||||
if (!$artist) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $artist);
|
||||
|
||||
return $artist;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,13 +31,16 @@ abstract class BaseHandler
|
|||
try {
|
||||
$redis = new \Redis();
|
||||
$redis->connect("127.0.0.1", 6379);
|
||||
|
||||
$this->cache = $redis;
|
||||
} catch (\Exception $e) {
|
||||
error_log("Redis connection failed: " . $e->getMessage());
|
||||
|
||||
$this->cache = null;
|
||||
}
|
||||
} else {
|
||||
error_log("Redis extension not found — caching disabled.");
|
||||
|
||||
$this->cache = null;
|
||||
}
|
||||
}
|
||||
|
@ -56,12 +59,12 @@ abstract class BaseHandler
|
|||
], $options));
|
||||
|
||||
$responseBody = $response->getBody()->getContents();
|
||||
|
||||
if (empty($responseBody)) return [];
|
||||
|
||||
$data = json_decode($responseBody, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new \Exception("Invalid JSON: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) throw new \Exception("Invalid JSON: " . json_last_error_msg());
|
||||
|
||||
return $data;
|
||||
} catch (RequestException $e) {
|
||||
|
@ -78,6 +81,7 @@ abstract class BaseHandler
|
|||
protected function fetchFromApi(string $endpoint, string $query = ""): array
|
||||
{
|
||||
$url = $endpoint . ($query ? "?{$query}" : "");
|
||||
|
||||
return $this->makeRequest("GET", $url);
|
||||
}
|
||||
|
||||
|
@ -85,7 +89,9 @@ abstract class BaseHandler
|
|||
{
|
||||
http_response_code($statusCode);
|
||||
header("Content-Type: application/json");
|
||||
|
||||
echo json_encode($data);
|
||||
|
||||
exit();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,15 @@ class BookFetcher extends PageFetcher
|
|||
{
|
||||
$cacheKey = "book_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$book = $this->fetchSingleFromApi("optimized_books", $url);
|
||||
|
||||
if (!$book) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $book);
|
||||
|
||||
return $book;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,15 @@ class GenreFetcher extends PageFetcher
|
|||
{
|
||||
$cacheKey = "genre_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$genre = $this->fetchSingleFromApi("optimized_genres", $url);
|
||||
|
||||
if (!$genre) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $genre);
|
||||
|
||||
return $genre;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,15 @@ class MovieFetcher extends PageFetcher
|
|||
{
|
||||
$cacheKey = "movie_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$movie = $this->fetchSingleFromApi("optimized_movies", $url);
|
||||
|
||||
if (!$movie) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $movie);
|
||||
|
||||
return $movie;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ abstract class PageFetcher extends BaseHandler
|
|||
protected function fetchSingleFromApi(string $endpoint, string $url): ?array
|
||||
{
|
||||
$data = $this->fetchFromApi($endpoint, "url=eq./{$url}");
|
||||
|
||||
return $data[0] ?? null;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,15 @@ class ShowFetcher extends PageFetcher
|
|||
{
|
||||
$cacheKey = "show_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$show = $this->fetchSingleFromApi("optimized_shows", $url);
|
||||
|
||||
if (!$show) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $show);
|
||||
|
||||
return $show;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@ class TagFetcher extends PageFetcher
|
|||
{
|
||||
$offset = ($page - 1) * $pageSize;
|
||||
$cacheKey = "tag_" . md5("{$tag}_{$page}");
|
||||
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
|
||||
if ($cached) return $cached;
|
||||
|
||||
$results = $this->fetchPostRpc("rpc/get_tagged_content", [
|
||||
|
@ -21,6 +21,7 @@ class TagFetcher extends PageFetcher
|
|||
if (!$results || count($results) === 0) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $results);
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue