From e623c8cb69556ee0a7d6f3cc058110e0c30bb22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20St=C3=B6rzbach?= Date: Sun, 12 Feb 2023 22:15:48 +0100 Subject: [PATCH] fix caching --- classes/GoogleCalendarService.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/classes/GoogleCalendarService.php b/classes/GoogleCalendarService.php index 984d677..0b0f3a0 100755 --- a/classes/GoogleCalendarService.php +++ b/classes/GoogleCalendarService.php @@ -2,11 +2,12 @@ use Log; use Cache; +use Carbon\Carbon; use NicoSt\GCalendar\Models\Settings; class GoogleCalendarService { - private $cacheTimeDefault = 15; + private $cacheTimeMinutesDefault = 15; private $cacheKeyCalendarList = 'GCalendar-CalendarList'; private $cacheKeyCalendarGet = 'GCalendar-CalendarGet#'; @@ -20,9 +21,9 @@ class GoogleCalendarService { public function getCalendar($calendarId, $maxEvents = '10') { $cachedCalendarGet = Cache::get($this->cacheKeyCalendarGet.$calendarId); - $cacheTime = Settings::get('cache_time', $this->cacheTimeDefault); + $cacheTimeMinutes = Settings::get('cache_time', $this->cacheTimeMinutesDefault); - if(!isset($cachedCalendarGet) || $cacheTime == 0) { + if(!isset($cachedCalendarGet) || $cacheTimeMinutes == 0) { // "timeMin" -> Set param to only get upcoming events // "singleEvents" -> Split recurring events into single events // "orderBy" -> Order events by startTime @@ -35,10 +36,11 @@ class GoogleCalendarService { ); try { - //Log::debug('G-Calendar: Call google with #getCalendar for calendar with id: ' . $calendarId); + Log::debug('G-Calendar: Call google with #getCalendar for calendar with id: ' . $calendarId); $response = $this->service->events->listEvents($calendarId, $params); - Cache::put($this->cacheKeyCalendarGet.$calendarId, $response, $cacheTime); + $expiresAt = Carbon::now()->addMinutes($cacheTimeMinutes); + Cache::put($this->cacheKeyCalendarGet.$calendarId, $response, $expiresAt); return $response; } catch (\Exception $e) { $json = json_decode($e->getMessage(), true); @@ -52,14 +54,15 @@ class GoogleCalendarService { public function calendarList() { $cachedCalendarList = Cache::get($this->cacheKeyCalendarList); - $cacheTime = Settings::get('cache_time', $this->cacheTimeDefault); + $cacheTimeMinutes = Settings::get('cache_time', $this->cacheTimeMinutesDefault); - if(!isset($cachedCalendarList) || $cacheTime == 0) { + if(!isset($cachedCalendarList) || $cacheTimeMinutes == 0) { try { - //Log::debug('G-Calendar: Call google with #calendarList.'); + Log::debug('G-Calendar: Call google with #calendarList.'); $response = $this->service->calendarList->listCalendarList()->getItems(); - Cache::put($this->cacheKeyCalendarList, $response, $cacheTime); + $expiresAt = Carbon::now()->addMinutes($cacheTimeMinutes); + Cache::put($this->cacheKeyCalendarList, $response, $expiresAt); return $response; } catch (\Exception $e) { $json = json_decode($e->getMessage(), true);