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\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,25 +76,29 @@ 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) {
|
||||||
|
|
||||||
$service = app(XenforoApiService::class);
|
try {
|
||||||
|
$service = app(XenforoApiService::class);
|
||||||
|
|
||||||
// Get thread infos and pagination.
|
// Get thread infos and pagination.
|
||||||
$paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1);
|
$paginationInfos = $service->getThreadPosts($entry->comments_thread_id, 1);
|
||||||
$lastPage = $paginationInfos['pagination']['last_page'] ?? 1;
|
$lastPage = $paginationInfos['pagination']['last_page'] ?? 1;
|
||||||
|
|
||||||
// Get last threads
|
// Get last threads
|
||||||
$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();
|
||||||
|
} 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
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user