From d31140b9b72e6808c125d02b65b6dae58ee9c044 Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Mon, 25 Jul 2016 14:46:05 +0200
Subject: [PATCH] Cache statistics data for a fixed amount of time

Real-time updates for global statistics are unnecessary and put a lot of
pressure on the database. Data is now cached for five minutes. The count
of online users is still updated in real-time.
---
 .../de/thm/arsnova/services/StatisticsService.java   | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/services/StatisticsService.java b/src/main/java/de/thm/arsnova/services/StatisticsService.java
index b27e347b..0071325b 100644
--- a/src/main/java/de/thm/arsnova/services/StatisticsService.java
+++ b/src/main/java/de/thm/arsnova/services/StatisticsService.java
@@ -20,10 +20,12 @@ package de.thm.arsnova.services;
 import de.thm.arsnova.dao.IDatabaseDao;
 import de.thm.arsnova.entities.Statistics;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 /**
- * Performs all statistics related operations.
+ * Performs all statistics related operations. To reduce pressure on the database, data is cached for a fixed amount of
+ * time.
  */
 @Service
 public class StatisticsService implements IStatisticsService {
@@ -34,9 +36,15 @@ public class StatisticsService implements IStatisticsService {
 	@Autowired
 	private IUserService userService;
 
+	private Statistics statistics = new Statistics();
+
+	@Scheduled(initialDelay = 0, fixedRate = 300000)
+	private void refreshStatistics() {
+		statistics = databaseDao.getStatistics();
+	}
+
 	@Override
 	public Statistics getStatistics() {
-		final Statistics statistics = databaseDao.getStatistics();
 		statistics.setActiveUsers(userService.loggedInUsers());
 		return statistics;
 	}
-- 
GitLab