summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-06-16 18:22:54 +0200
committerSimon Rettberg2015-06-16 18:22:54 +0200
commit9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1 (patch)
treeb7fb7612f4319943426d8ca30d1a8a7fb68b4208 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
parent[server] On mah way (lots of restructuring, some early db classes, sql dump o... (diff)
downloadtutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.tar.gz
tutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.tar.xz
tutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.zip
[server] Add script field to lecture table; implement getImageDetails method to get detailed information about an image from the database
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
new file mode 100644
index 00000000..e42ee9fe
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
@@ -0,0 +1,43 @@
+package org.openslx.bwlp.sat.thrift.cache;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.thrift.TException;
+
+/**
+ * Class that caches an instance of a given class for 10 minutes.
+ * If the cache expired and a fresh instance cannot be acquired,
+ * the old instance will be returned.
+ *
+ * @param <T> The class to cache
+ */
+public abstract class CacheBase<T> {
+
+ private static final Logger LOGGER = Logger.getLogger(CacheBase.class);
+
+ private static final int TIMEOUT = 10 * 60 * 1000;
+
+ private T cachedInstance = null;
+
+ private long cacheTimeout = 0;
+
+ protected abstract T getCallback() throws TException;
+
+ protected synchronized T getInternal() {
+ final long now = System.currentTimeMillis();
+ if (cachedInstance == null || now > cacheTimeout) {
+ try {
+ T freshInstance = getCallback();
+ if (freshInstance != null) {
+ cachedInstance = freshInstance;
+ cacheTimeout = now + TIMEOUT;
+ }
+ } catch (TException e) {
+ LOGGER.warn("Could not retrieve fresh instance of " + getClass().getSimpleName(), e);
+ }
+ }
+ return cachedInstance;
+ }
+
+}