diff options
author | Simon Rettberg | 2015-07-10 17:23:47 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-07-10 17:23:47 +0200 |
commit | 5b9c7a46e3b0046d15486a63b872fb69a8ee4d2d (patch) | |
tree | 04afc13d97b8d912a9bf076245c849a6a127943c /src | |
parent | Fix behavior of thrift proxy when getting a new client fails by throwing an e... (diff) | |
download | master-sync-shared-5b9c7a46e3b0046d15486a63b872fb69a8ee4d2d.tar.gz master-sync-shared-5b9c7a46e3b0046d15486a63b872fb69a8ee4d2d.tar.xz master-sync-shared-5b9c7a46e3b0046d15486a63b872fb69a8ee4d2d.zip |
Moved some common classes from dozmod server/client to this lib
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/openslx/util/GenericDataCache.java | 94 | ||||
-rw-r--r-- | src/main/java/org/openslx/util/QuickTimer.java | 61 |
2 files changed, 155 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/util/GenericDataCache.java b/src/main/java/org/openslx/util/GenericDataCache.java new file mode 100644 index 0000000..5852c94 --- /dev/null +++ b/src/main/java/org/openslx/util/GenericDataCache.java @@ -0,0 +1,94 @@ +package org.openslx.util; + +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.log4j.Logger; + +public abstract class GenericDataCache<T> { + + private static final Logger LOGGER = Logger.getLogger(GenericDataCache.class); + + /** + * How long the cached data is valid after fetching + */ + private final int validMs; + + /** + * Deadline when the cache goes invalid + */ + private long validUntil = 0; + + /** + * The data being held + */ + private final AtomicReference<T> item = new AtomicReference<>(); + + public GenericDataCache(int validMs) { + this.validMs = validMs; + } + + /** + * Get the cached object, but refresh the cache first if + * the cached instance is too old. + * + * @return + */ + public T get() { + return get(CacheMode.DEFAULT); + } + + /** + * Get the cached object, using the given cache access strategy. + * ALWAYS_CACHED: Never refresh the cache, except if it has never been fetched before + * DEFAULT: Only fetch from remote if the cached value is too old + * NEVER_CACHED: Always fetch from remote. If it fails, return null + * + * @param mode Cache access strategy as described above + * @return T + */ + public T get(CacheMode mode) { + switch (mode) { + case ALWAYS_CACHED: + if (validUntil == 0) + ensureUpToDate(true); + break; + case DEFAULT: + ensureUpToDate(false); + break; + case NEVER_CACHED: + if (!ensureUpToDate(true)) + return null; + break; + } + return item.get(); + } + + private synchronized boolean ensureUpToDate(boolean force) { + final long now = System.currentTimeMillis(); + if (!force && now < validUntil) + return true; + T fetched; + try { + fetched = update(); + if (fetched == null) + return false; + } catch (Exception e) { + LOGGER.warn("Could not fetch fresh data", e); + return false; + } + item.set(fetched); + validUntil = now + validMs; + return true; + } + + protected abstract T update() throws Exception; + + // + + public static enum CacheMode { + ALWAYS_CACHED, + DEFAULT, + NEVER_CACHED + } + +} diff --git a/src/main/java/org/openslx/util/QuickTimer.java b/src/main/java/org/openslx/util/QuickTimer.java new file mode 100644 index 0000000..7a728d2 --- /dev/null +++ b/src/main/java/org/openslx/util/QuickTimer.java @@ -0,0 +1,61 @@ +package org.openslx.util; + +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.log4j.Logger; + +/** + * This is a global, static {@link Timer} you can use anywhere for repeating + * tasks that will <b>not take a significant amount of time to execute</b>. This + * means they should not run any complex data base queries (better yet, none at + * all) or do heavy file I/O, etc.. + * The main reason for this class is to prevent having {@link Timer} threads + * everywhere in the server for trivial tasks. + */ +public class QuickTimer { + + private static final Logger LOGGER = Logger.getLogger(QuickTimer.class); + + private static final Timer timer = new Timer("QuickTimer"); + + public static void scheduleAtFixedDelay(Task task, long delay, long period) { + timer.schedule(task, delay, period); + } + + public static void scheduleAtFixedRate(Task task, long delay, long period) { + timer.scheduleAtFixedRate(task, delay, period); + } + + public static void scheduleOnce(Task task, long delay) { + timer.schedule(task, delay); + } + + public static void scheduleOnce(Task timerTask) { + scheduleOnce(timerTask, 1); + } + + /** + * Cancel this timer. Should only be called when the server is shutting + * down. + */ + public static void cancel() { + timer.cancel(); + } + + public static abstract class Task extends TimerTask { + + @Override + public final void run() { + try { + fire(); + } catch (Throwable t) { + LOGGER.warn("A Task threw an exception!", t); + } + } + + public abstract void fire(); + + } + +} |