fix caching
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user