feat(cache): implement centralized caching layer for improved performance and reduced API calls

feat(microsoft-integration): enhance inbox and events fetching with caching support
feat(auth): include theme and notification settings in user data retrieval
feat(profile): improve password visibility toggle functionality
feat(planner): optimize event synchronization with Outlook using caching
fix(notifications): update mail badge handling for mobile and desktop
This commit is contained in:
2026-09-10 15:19:46 +02:00
parent 5310b87fe5
commit 72ad1fb70e
15 changed files with 334 additions and 85 deletions
+21 -12
View File
@@ -6,6 +6,7 @@
require_once __DIR__ . '/../http.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../cache.php';
/**
* Normaliseert het Jira domein naar een schone URL zonder slash aan het einde
@@ -823,15 +824,18 @@ function jira_get_dashboard_data(int $userId, bool $forceRefresh = false): array
];
}
// Sessiecache voor 60 seconden
if (session_status() === PHP_SESSION_NONE) {
@session_start();
}
// Cache controle (60 seconden)
$cacheKey = "jira_dashboard_user_{$userId}";
$cached = $_SESSION[$cacheKey] ?? null;
if (!$forceRefresh && $cached && is_array($cached) && ($cached['timestamp'] ?? 0) > (time() - 60)) {
return array_merge($cached['data'], ['from_cache' => true]);
if (!$forceRefresh) {
$cachedData = fociz_cache_get($cacheKey);
if ($cachedData !== null && is_array($cachedData)) {
return array_merge($cachedData, ['from_cache' => true]);
}
if (session_status() === PHP_SESSION_ACTIVE && !empty($_SESSION[$cacheKey]['data'])) {
if (($_SESSION[$cacheKey]['timestamp'] ?? 0) > (time() - 60)) {
return array_merge($_SESSION[$cacheKey]['data'], ['from_cache' => true]);
}
}
}
// Ophalen van toegewezen taken
@@ -865,10 +869,14 @@ function jira_get_dashboard_data(int $userId, bool $forceRefresh = false): array
'default_queue_filter' => $userQueuePref,
];
$_SESSION[$cacheKey] = [
'timestamp' => time(),
'data' => $data,
];
fociz_cache_set($cacheKey, $data, 60);
if (session_status() === PHP_SESSION_ACTIVE) {
$_SESSION[$cacheKey] = [
'timestamp' => time(),
'data' => $data,
];
}
// Werk last_sync_at bij in database
$stmtUpdate = $db->prepare("UPDATE integrations SET last_sync_at = NOW() WHERE id = ?");
@@ -902,6 +910,7 @@ function jira_disconnect(int $userId): bool {
}
unset($_SESSION["jira_issues_user_{$userId}"]);
unset($_SESSION["jira_dashboard_user_{$userId}"]);
fociz_cache_delete("jira_dashboard_user_{$userId}");
return $stmt->execute([$userId]);
}
+40 -6
View File
@@ -6,6 +6,7 @@
require_once __DIR__ . '/../http.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../cache.php';
const MS_OAUTH_AUTHORIZE_URL = 'https://login.microsoftonline.com/%s/oauth2/v2.0/authorize';
const MS_OAUTH_TOKEN_URL = 'https://login.microsoftonline.com/%s/oauth2/v2.0/token';
@@ -279,7 +280,15 @@ function microsoft_get_valid_token(int $userId, bool $forceRefresh = false): ?st
/**
* Haalt agenda-afspraken van Microsoft Outlook op voor een datumbereik (bijv. een hele week)
*/
function microsoft_fetch_events_range(int $userId, string $startDate, string $endDate): array {
function microsoft_fetch_events_range(int $userId, string $startDate, string $endDate, bool $forceRefresh = false): array {
$cacheKey = "ms_events_{$userId}_{$startDate}_{$endDate}";
if (!$forceRefresh) {
$cached = fociz_cache_get($cacheKey);
if ($cached !== null && is_array($cached)) {
return $cached;
}
}
$token = microsoft_get_valid_token($userId);
if (!$token) {
return [];
@@ -400,6 +409,7 @@ function microsoft_fetch_events_range(int $userId, string $startDate, string $en
];
}
fociz_cache_set($cacheKey, $events, 60);
return $events;
}
@@ -520,6 +530,7 @@ function microsoft_create_event(int $userId, array $eventData): array {
}
if ($response['status'] === 201 && is_array($response['json'])) {
fociz_cache_delete_pattern("ms_events_{$userId}_");
return [
'success' => true,
'event_id' => $response['json']['id'] ?? null,
@@ -559,7 +570,11 @@ function microsoft_delete_event(int $userId, string $eventId): bool {
'timeout' => 12,
]);
return in_array($response['status'], [200, 204, 404]);
$ok = in_array($response['status'], [200, 204, 404]);
if ($ok) {
fociz_cache_delete_pattern("ms_events_{$userId}_");
}
return $ok;
}
/**
@@ -628,7 +643,11 @@ function microsoft_update_event(int $userId, string $eventId, array $eventData):
}
}
return in_array($response['status'], [200, 204]);
$ok = in_array($response['status'], [200, 204]);
if ($ok) {
fociz_cache_delete_pattern("ms_events_{$userId}_");
}
return $ok;
}
/**
@@ -985,7 +1004,15 @@ function microsoft_get_mail_demo_data(): array {
/**
* Haalt inbox berichten op van Microsoft Graph API (of retourneert demo data)
*/
function microsoft_fetch_inbox_messages(int $userId, int $limit = 25, bool $unreadOnly = false): array {
function microsoft_fetch_inbox_messages(int $userId, int $limit = 25, bool $unreadOnly = false, bool $forceRefresh = false): array {
$cacheKey = "ms_inbox_{$userId}_{$limit}_" . ($unreadOnly ? '1' : '0');
if (!$forceRefresh) {
$cached = fociz_cache_get($cacheKey);
if ($cached !== null && is_array($cached)) {
return $cached;
}
}
$db = get_db_connection();
$stmt = $db->prepare("SELECT * FROM integrations WHERE user_id = ? AND provider = 'OUTLOOK' AND is_enabled = 1");
$stmt->execute([$userId]);
@@ -1118,13 +1145,15 @@ function microsoft_fetch_inbox_messages(int $userId, int $limit = 25, bool $unre
$unreadCount = count(array_filter($messages, function($m) { return empty($m['is_read']); }));
return [
$result = [
'success' => true,
'is_demo' => false,
'messages' => $messages,
'unread_count' => $unreadCount,
'total_count' => count($messages),
];
fociz_cache_set($cacheKey, $result, 45); // 45 seconden cache
return $result;
}
/**
@@ -1239,7 +1268,11 @@ function microsoft_mark_message_read(int $userId, string $messageId, bool $isRea
'timeout' => 10,
]);
return ($res['status'] >= 200 && $res['status'] < 300);
$ok = ($res['status'] >= 200 && $res['status'] < 300);
if ($ok) {
fociz_cache_delete_pattern("ms_inbox_{$userId}_");
}
return $ok;
}
/**
@@ -1271,6 +1304,7 @@ function microsoft_reply_message(int $userId, string $messageId, string $comment
]);
if ($res['status'] >= 200 && $res['status'] < 300) {
fociz_cache_delete_pattern("ms_inbox_{$userId}_");
return ['success' => true, 'message' => 'Antwoord succesvol verzonden via Outlook.'];
}