summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util
diff options
context:
space:
mode:
authorSimon Rettberg2015-09-09 14:25:31 +0200
committerSimon Rettberg2015-09-09 14:25:31 +0200
commit8e03d38f0c4d687162e8be1b9bbf660ed210d8f9 (patch)
treecfcd0ea0f2fc53a799e31c5ce8e68ef76c4ce6a9 /src/main/java/org/openslx/util
parentmaster server rpc (diff)
downloadmaster-sync-shared-8e03d38f0c4d687162e8be1b9bbf660ed210d8f9.tar.gz
master-sync-shared-8e03d38f0c4d687162e8be1b9bbf660ed210d8f9.tar.xz
master-sync-shared-8e03d38f0c4d687162e8be1b9bbf660ed210d8f9.zip
VCache und thrift
Diffstat (limited to 'src/main/java/org/openslx/util')
-rw-r--r--src/main/java/org/openslx/util/GenericDataCache.java87
1 files changed, 54 insertions, 33 deletions
diff --git a/src/main/java/org/openslx/util/GenericDataCache.java b/src/main/java/org/openslx/util/GenericDataCache.java
index 5852c94..5ae73fb 100644
--- a/src/main/java/org/openslx/util/GenericDataCache.java
+++ b/src/main/java/org/openslx/util/GenericDataCache.java
@@ -4,90 +4,111 @@ 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);
-
+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) {
+
+ 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);
+ 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
+ *
+ * @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);
+ public T get( CacheMode mode )
+ {
+ switch ( mode ) {
+ case FORCE_CACHED:
+ break;
+ case PREFER_CACHED:
+ if ( validUntil == 0 )
+ ensureUpToDate( true );
break;
case DEFAULT:
- ensureUpToDate(false);
+ ensureUpToDate( false );
break;
case NEVER_CACHED:
- if (!ensureUpToDate(true))
+ if ( !ensureUpToDate( true ) )
return null;
break;
}
return item.get();
}
-
- private synchronized boolean ensureUpToDate(boolean force) {
+
+ private synchronized boolean ensureUpToDate( boolean force )
+ {
final long now = System.currentTimeMillis();
- if (!force && now < validUntil)
+ if ( !force && now < validUntil )
return true;
T fetched;
try {
fetched = update();
- if (fetched == null)
+ if ( fetched == null )
return false;
- } catch (Exception e) {
- LOGGER.warn("Could not fetch fresh data", e);
+ } catch ( Exception e ) {
+ LOGGER.warn( "Could not fetch fresh data", e );
return false;
}
- item.set(fetched);
+ item.set( fetched );
validUntil = now + validMs;
return true;
}
-
+
protected abstract T update() throws Exception;
-
+
//
-
- public static enum CacheMode {
- ALWAYS_CACHED,
+
+ public static enum CacheMode
+ {
+ /**
+ * Use cache, even if the item has never been fetched before.
+ */
+ FORCE_CACHED,
+ /**
+ * Use cache if it's not empty, no matter how old it is.
+ */
+ PREFER_CACHED,
+ /**
+ * Obey the cache timeout value of this cache.
+ */
DEFAULT,
+ /**
+ * Always fetch a fresh instance of the item.
+ */
NEVER_CACHED
}