fix caching

This commit is contained in:
2023-02-12 22:15:48 +01:00
parent 07ae847443
commit e623c8cb69

View File

@@ -2,11 +2,12 @@
use Log; use Log;
use Cache; use Cache;
use Carbon\Carbon;
use NicoSt\GCalendar\Models\Settings; use NicoSt\GCalendar\Models\Settings;
class GoogleCalendarService { class GoogleCalendarService {
private $cacheTimeDefault = 15; private $cacheTimeMinutesDefault = 15;
private $cacheKeyCalendarList = 'GCalendar-CalendarList'; private $cacheKeyCalendarList = 'GCalendar-CalendarList';
private $cacheKeyCalendarGet = 'GCalendar-CalendarGet#'; private $cacheKeyCalendarGet = 'GCalendar-CalendarGet#';
@@ -20,9 +21,9 @@ class GoogleCalendarService {
public function getCalendar($calendarId, $maxEvents = '10') { public function getCalendar($calendarId, $maxEvents = '10') {
$cachedCalendarGet = Cache::get($this->cacheKeyCalendarGet.$calendarId); $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 // "timeMin" -> Set param to only get upcoming events
// "singleEvents" -> Split recurring events into single events // "singleEvents" -> Split recurring events into single events
// "orderBy" -> Order events by startTime // "orderBy" -> Order events by startTime
@@ -35,10 +36,11 @@ class GoogleCalendarService {
); );
try { 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); $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; return $response;
} catch (\Exception $e) { } catch (\Exception $e) {
$json = json_decode($e->getMessage(), true); $json = json_decode($e->getMessage(), true);
@@ -52,14 +54,15 @@ class GoogleCalendarService {
public function calendarList() { public function calendarList() {
$cachedCalendarList = Cache::get($this->cacheKeyCalendarList); $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 { try {
//Log::debug('G-Calendar: Call google with #calendarList.'); Log::debug('G-Calendar: Call google with #calendarList.');
$response = $this->service->calendarList->listCalendarList()->getItems(); $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; return $response;
} catch (\Exception $e) { } catch (\Exception $e) {
$json = json_decode($e->getMessage(), true); $json = json_decode($e->getMessage(), true);