Fixed Guzzle 30000ms error and improve cache usage

This commit is contained in:
2026-06-30 20:07:38 +02:00
parent aefe03ed67
commit 8ca82fd0c8
2 changed files with 22 additions and 16 deletions

View File

@@ -6,6 +6,8 @@ use App\Models\Entry;
use App\Models\EntryFile; use App\Models\EntryFile;
use App\Models\News; use App\Models\News;
use App\Services\XenforoApiService; use App\Services\XenforoApiService;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@@ -74,8 +76,9 @@ class EntryHelpers {
$cacheKey = "news_comments_{$entry->id}"; $cacheKey = "news_comments_{$entry->id}";
else else
$cacheKey = "entry_comments_{$entry->id}"; $cacheKey = "entry_comments_{$entry->id}";
return Cache::remember($cacheKey, now()->addDays(1), function () use ($entry, $limit) { return Cache::flexible($cacheKey, [now()->addMinutes(30), now()->addMinutes(60)], function () use ($entry, $limit) {
try {
$service = app(XenforoApiService::class); $service = app(XenforoApiService::class);
// Get thread infos and pagination. // Get thread infos and pagination.
@@ -86,12 +89,15 @@ class EntryHelpers {
$lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos; $lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos;
$posts = $lastPageData['posts'] ?? []; $posts = $lastPageData['posts'] ?? [];
if( count( $posts ) < $limit && $lastPage > 1 ){ if (count($posts) < $limit && $lastPage > 1) {
$previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1 ); $previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1);
$posts = array_merge( $posts, $previousPageData['posts'] ?? [] ); $posts = array_merge($posts, $previousPageData['posts'] ?? []);
} }
return collect( $posts )->slice(-$limit)->reverse()->values()->toArray(); return collect($posts)->slice(-$limit)->reverse()->values()->toArray();
} catch (ConnectException|RequestException $e ){
return [];
}
}); });
} }

View File

@@ -25,7 +25,7 @@ class XenforoApiService {
*/ */
private function get(string $endpoint, ?int $customUserId = null ): mixed private function get(string $endpoint, ?int $customUserId = null ): mixed
{ {
$response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([
'XF-Api-Key' => $this->apiKey, 'XF-Api-Key' => $this->apiKey,
'XF-Api-User' => $customUserId ?? $this->superUserId, 'XF-Api-User' => $customUserId ?? $this->superUserId,
])->get("{$this->apiUrl}/{$endpoint}"); ])->get("{$this->apiUrl}/{$endpoint}");
@@ -40,7 +40,7 @@ class XenforoApiService {
private function post(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed private function post(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed
{ {
$response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([
'XF-Api-Key' => $this->apiKey, 'XF-Api-Key' => $this->apiKey,
'XF-Api-User' => $customUserId ?? $this->superUserId, 'XF-Api-User' => $customUserId ?? $this->superUserId,
])->post("{$this->apiUrl}/{$endpoint}", $data); ])->post("{$this->apiUrl}/{$endpoint}", $data);
@@ -55,7 +55,7 @@ class XenforoApiService {
private function delete(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed private function delete(string $endpoint, ?int $customUserId = null, array $data = [] ): mixed
{ {
$response = Http::timeout(30)->withOptions(['verify' => false])->withHeaders([ $response = Http::timeout(8)->connectTimeout(4)->withOptions(['verify' => false])->withHeaders([
'XF-Api-Key' => $this->apiKey, 'XF-Api-Key' => $this->apiKey,
'XF-Api-User' => $customUserId ?? $this->superUserId, 'XF-Api-User' => $customUserId ?? $this->superUserId,
])->delete("{$this->apiUrl}/{$endpoint}", $data); ])->delete("{$this->apiUrl}/{$endpoint}", $data);