deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject deltaspike git commit: DELTASPIKE-1117 add a simple caching logic for TypedResolver
Date Wed, 06 Apr 2016 15:03:53 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 2f143e0ca -> e78c329d0


DELTASPIKE-1117 add a simple caching logic for TypedResolver


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/e78c329d
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/e78c329d
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/e78c329d

Branch: refs/heads/master
Commit: e78c329d00ea4dceda070319628c96d24cbd6e61
Parents: 2f143e0
Author: Mark Struberg <struberg@apache.org>
Authored: Wed Apr 6 17:02:27 2016 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Wed Apr 6 17:02:27 2016 +0200

----------------------------------------------------------------------
 .../core/api/config/ConfigResolver.java         | 40 +++++++++++++++++++-
 .../test/api/config/TypedResolverTest.java      | 21 ++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e78c329d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
index d350ae7..075b59f 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -594,6 +595,17 @@ public final class ConfigResolver
         TypedResolver<T> withStringDefault(String value);
 
         /**
+         * Specify that a resolved value will get cached for a certain amount of time.
+         * After the time expires the next {@link #getValue()} will again resolve the value
+         * from the underlying {@link ConfigResolver}.
+         *
+         * @param timeUnit the TimeUnit for the value
+         * @param value the amount of the TimeUnit to wait
+         * @return This builder
+         */
+        TypedResolver<T> cacheFor(TimeUnit timeUnit, long value);
+
+        /**
          * Returns the converted resolved filtered value.
          * @return the resolved value
          */
@@ -692,6 +704,10 @@ public final class ConfigResolver
 
         private Converter<?> converter;
 
+        private long cacheTimeMs = -1;
+        private volatile long reloadAfter = -1;
+        private T lastValue = null;
+
 
         private PropertyBuilder()
         {
@@ -741,6 +757,13 @@ public final class ConfigResolver
         }
 
         @Override
+        public TypedResolver<T> cacheFor(TimeUnit timeUnit, long value)
+        {
+            this.cacheTimeMs = timeUnit.toMillis(value);
+            return this;
+        }
+
+        @Override
         public TypedResolver<T> parameterizedBy(String propertyName)
         {
             this.propertyParameter = propertyName;
@@ -778,10 +801,25 @@ public final class ConfigResolver
         @Override
         public T getValue()
         {
+            if (cacheTimeMs > 0)
+            {
+                long now = System.currentTimeMillis();
+                if (now <= reloadAfter)
+                {
+                    return lastValue;
+                }
+                reloadAfter = now + cacheTimeMs;
+            }
+
             String valueStr = resolveStringValue();
             T value = convert(valueStr);
 
-            return fallbackToDefaultIfEmpty(keyResolved, value, defaultValue);
+            value = fallbackToDefaultIfEmpty(keyResolved, value, defaultValue);
+            if (cacheTimeMs > 0)
+            {
+                lastValue = value;
+            }
+            return value;
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e78c329d/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TypedResolverTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TypedResolverTest.java
b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TypedResolverTest.java
index 38b273f..3a8c144 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TypedResolverTest.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TypedResolverTest.java
@@ -27,6 +27,7 @@ import org.junit.Test;
 
 import java.util.Date;
 import java.util.GregorianCalendar;
+import java.util.concurrent.TimeUnit;
 
 public class TypedResolverTest
 {
@@ -199,6 +200,26 @@ public class TypedResolverTest
         Assert.assertEquals("testkey2", resolver2.getResolvedKey());
     }
 
+    @Test
+    public void testWithCacheTime() throws Exception
+    {
+        ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve("dataSource")
+            .withCurrentProjectStage(true)
+            .parameterizedBy("dbvendor")
+            .cacheFor(TimeUnit.MILLISECONDS, 5)
+            .withDefault("TESTDEFAULT");
+
+        Assert.assertEquals("TestDataSource", resolver.getValue());
+        Assert.assertEquals("TestDataSource", resolver.getValue());
+        Assert.assertEquals("dataSource", resolver.getKey());
+        Assert.assertEquals("TESTDEFAULT", resolver.getDefaultValue());
+        Assert.assertEquals("dataSource.mysql.UnitTest", resolver.getResolvedKey());
+
+        // because the clock steps in certain OS is only 16ms
+        Thread.sleep(35L);
+        Assert.assertEquals("TestDataSource", resolver.getValue());
+    }
+
     public static class TestDateConverter implements ConfigResolver.Converter<Date>
{
 
         @Override


Mime
View raw message