summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
blob: 8b5498002b210e63c51c9c241e889498e90f0321 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.openslx.bwlp.sat.thrift.cache;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

/**
 * 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 = LogManager.getLogger(CacheBase.class);

	/** Keep cached data for 10 minutes at a time */
	private static final int TIMEOUT = 600_000;

	private T cachedInstance = null;

	/** Deadline after which cache is considered stale */
	private long cacheTimeout = 0;

	/** For timed waiting on new data */
	private CountDownLatch latch = null;

	protected abstract T getCallback() throws TException;

	protected T getInternal() {
		boolean doFetch = false;
		final CountDownLatch localLatch;
		synchronized (this) {
			if (cachedInstance != null && System.currentTimeMillis() < cacheTimeout)
				return cachedInstance;
			if (latch == null) {
				latch = new CountDownLatch(1);
				doFetch = true;
			}
			localLatch = latch; // Fetch a local reference while still synchronized
		}
		// Need update
		if (doFetch) {
			// This invocation found latch == null, which means it is
			// responsible for triggering the actual update.
			QuickTimer.scheduleOnce(new Task() {
				@Override
				public void fire() {
					T freshInstance = null;
					try {
						freshInstance = getCallback();
					} catch (TException e) {
						LOGGER.warn("Could not retrieve fresh instance of " + getClass().getSimpleName(), e);
					} finally {
						synchronized (CacheBase.this) {
							if (freshInstance != null) {
								cachedInstance = freshInstance;
								cacheTimeout = System.currentTimeMillis() + TIMEOUT;
							}
							latch = null;
						}
						localLatch.countDown();
					}
				}
			});
		}
		// Now just wait for latch, regardless of whether we triggered the update or not
		boolean ok = false;
		try {
			// In case the cache is still empty, we wait for long. Otherwise, bail out after
			// one second so we'll rather use stale data than blocking the caller for too long.
			int waitTime = cachedInstance == null ? 600 : 1;
			ok = localLatch.await(waitTime, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
		}
		if (!ok) {
			LOGGER.warn("CacheUpdate for " + getClass().getSimpleName() + " timed out, using old data.");
		}
		synchronized (this) {
			return cachedInstance;
		}
	}

}