Fixed Guzzle 30000ms error and improve cache usage
This commit is contained in:
@@ -6,6 +6,8 @@ use App\Models\Entry;
|
||||
use App\Models\EntryFile;
|
||||
use App\Models\News;
|
||||
use App\Services\XenforoApiService;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -74,25 +76,29 @@ class EntryHelpers {
|
||||
$cacheKey = "news_comments_{$entry->id}";
|
||||
else
|
||||
$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) {
|
||||
|
||||
$service = app(XenforoApiService::class);
|
||||
try {
|
||||
$service = app(XenforoApiService::class);
|
||||
|
||||
// Get thread infos and pagination.
|
||||
$paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1);
|
||||
$lastPage = $paginationInfos['pagination']['last_page'] ?? 1;
|
||||
// Get thread infos and pagination.
|
||||
$paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1);
|
||||
$lastPage = $paginationInfos['pagination']['last_page'] ?? 1;
|
||||
|
||||
// Get last threads
|
||||
$lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos;
|
||||
$posts = $lastPageData['posts'] ?? [];
|
||||
// Get last threads
|
||||
$lastPageData = $lastPage > 1 ? $service->getThreadPosts($entry->comments_thread_id, $lastPage) : $paginationInfos;
|
||||
$posts = $lastPageData['posts'] ?? [];
|
||||
|
||||
if( count( $posts ) < $limit && $lastPage > 1 ){
|
||||
$previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1 );
|
||||
$posts = array_merge( $posts, $previousPageData['posts'] ?? [] );
|
||||
if (count($posts) < $limit && $lastPage > 1) {
|
||||
$previousPageData = $service->getThreadPosts($entry->comments_thread_id, $lastPage - 1);
|
||||
$posts = array_merge($posts, $previousPageData['posts'] ?? []);
|
||||
}
|
||||
|
||||
return collect($posts)->slice(-$limit)->reverse()->values()->toArray();
|
||||
} catch (ConnectException|RequestException $e ){
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect( $posts )->slice(-$limit)->reverse()->values()->toArray();
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class XenforoApiService {
|
||||
*/
|
||||
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-User' => $customUserId ?? $this->superUserId,
|
||||
])->get("{$this->apiUrl}/{$endpoint}");
|
||||
@@ -40,7 +40,7 @@ class XenforoApiService {
|
||||
|
||||
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-User' => $customUserId ?? $this->superUserId,
|
||||
])->post("{$this->apiUrl}/{$endpoint}", $data);
|
||||
@@ -55,7 +55,7 @@ class XenforoApiService {
|
||||
|
||||
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-User' => $customUserId ?? $this->superUserId,
|
||||
])->delete("{$this->apiUrl}/{$endpoint}", $data);
|
||||
|
||||
Reference in New Issue
Block a user