You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2017/06/22 10:47:02 UTC

[2/3] camel git commit: CAMEL-11435 - Camel-Caffeine: Support Time-based and size-based eviction in Caffeine

CAMEL-11435 - Camel-Caffeine: Support Time-based and size-based eviction in Caffeine


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

Branch: refs/heads/master
Commit: ffe1edf3bb339e7a574eab40be2054ee41383d31
Parents: 1c712e6
Author: Andrea Cosentino <an...@gmail.com>
Authored: Thu Jun 22 11:07:44 2017 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Thu Jun 22 12:28:03 2017 +0200

----------------------------------------------------------------------
 .../src/main/docs/caffeine-cache-component.adoc |  5 +-
 .../main/docs/caffeine-loadcache-component.adoc |  5 +-
 .../caffeine/CaffeineConfiguration.java         | 40 ++++++++++++++++
 .../camel/component/caffeine/EvictionType.java  | 49 ++++++++++++++++++++
 .../caffeine/cache/CaffeineCacheEndpoint.java   | 12 ++++-
 .../load/CaffeineLoadCacheEndpoint.java         | 12 ++++-
 .../CaffeineCacheComponentConfiguration.java    | 40 ++++++++++++++++
 ...CaffeineLoadCacheComponentConfiguration.java | 40 ++++++++++++++++
 8 files changed, 197 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc b/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc
index d1a95df..de0dfdd 100644
--- a/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc
+++ b/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc
@@ -61,7 +61,7 @@ with the following path and query parameters:
 | **cacheName** | *Required* the cache name |  | String
 |=======================================================================
 
-#### Query Parameters (14 parameters):
+#### Query Parameters (17 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |=======================================================================
@@ -73,6 +73,9 @@ with the following path and query parameters:
 | **action** (producer) | To configure the default cache action. If an action is set in the message header then the operation from the header takes precedence. |  | String
 | **cache** (producer) | To configure the default an already instantianted cache to be used |  | Cache
 | **cacheLoader** (producer) | To configure a CacheLoader in case of a LoadCache use |  | CacheLoader
+| **evictionType** (producer) | Set the eviction Type for this cache | size_based | EvictionType
+| **expireAfterAccessTime** (producer) | Set the expire After Access Time in case of time based Eviction (in seconds) | 300 | int
+| **expireAfterWriteTime** (producer) | Set the expire After Access Write in case of time based Eviction (in seconds) | 300 | int
 | **initialCapacity** (producer) | Set the initial Capacity for the cache | 10000 | int
 | **key** (producer) | To configure the default action key. If a key is set in the message header then the key from the header takes precedence. |  | Object
 | **maximumSize** (producer) | Set the maximum size for the cache | 10000 | int

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/docs/caffeine-loadcache-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/docs/caffeine-loadcache-component.adoc b/components/camel-caffeine/src/main/docs/caffeine-loadcache-component.adoc
index 1acb9fa..a13c1ea 100644
--- a/components/camel-caffeine/src/main/docs/caffeine-loadcache-component.adoc
+++ b/components/camel-caffeine/src/main/docs/caffeine-loadcache-component.adoc
@@ -61,7 +61,7 @@ with the following path and query parameters:
 | **cacheName** | *Required* the cache name |  | String
 |=======================================================================
 
-#### Query Parameters (14 parameters):
+#### Query Parameters (17 parameters):
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |=======================================================================
@@ -73,6 +73,9 @@ with the following path and query parameters:
 | **action** (producer) | To configure the default cache action. If an action is set in the message header then the operation from the header takes precedence. |  | String
 | **cache** (producer) | To configure the default an already instantianted cache to be used |  | Cache
 | **cacheLoader** (producer) | To configure a CacheLoader in case of a LoadCache use |  | CacheLoader
+| **evictionType** (producer) | Set the eviction Type for this cache | size_based | EvictionType
+| **expireAfterAccessTime** (producer) | Set the expire After Access Time in case of time based Eviction (in seconds) | 300 | int
+| **expireAfterWriteTime** (producer) | Set the expire After Access Write in case of time based Eviction (in seconds) | 300 | int
 | **initialCapacity** (producer) | Set the initial Capacity for the cache | 10000 | int
 | **key** (producer) | To configure the default action key. If a key is set in the message header then the key from the header takes precedence. |  | Object
 | **maximumSize** (producer) | Set the maximum size for the cache | 10000 | int

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/CaffeineConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/CaffeineConfiguration.java b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/CaffeineConfiguration.java
index ccfd1d2..2820491 100644
--- a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/CaffeineConfiguration.java
+++ b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/CaffeineConfiguration.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.caffeine;
 
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.CacheLoader;
+import com.github.benmanes.caffeine.cache.Expiry;
 
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.UriParam;
@@ -45,6 +46,12 @@ public class CaffeineConfiguration implements Cloneable {
     private int initialCapacity = 10000;
     @UriParam(label = "producer", defaultValue = "10000")
     private int maximumSize = 10000;
+    @UriParam(label = "producer", defaultValue = "size_based")
+    private EvictionType evictionType = EvictionType.SIZE_BASED;
+    @UriParam(label = "producer", defaultValue = "300")
+    private int expireAfterAccessTime = 300;
+    @UriParam(label = "producer", defaultValue = "300")
+    private int expireAfterWriteTime = 300;
 
     public CaffeineConfiguration() {
     }
@@ -162,6 +169,39 @@ public class CaffeineConfiguration implements Cloneable {
         this.maximumSize = maximumSize;
     }
 
+    public EvictionType getEvictionType() {
+        return evictionType;
+    }
+
+    /**
+     * Set the eviction Type for this cache
+     */
+    public void setEvictionType(EvictionType evictionType) {
+        this.evictionType = evictionType;
+    }
+
+    public int getExpireAfterAccessTime() {
+        return expireAfterAccessTime;
+    }
+
+    /**
+     * Set the expire After Access Time in case of time based Eviction (in seconds)
+     */
+    public void setExpireAfterAccessTime(int expireAfterAccessTime) {
+        this.expireAfterAccessTime = expireAfterAccessTime;
+    }
+
+    public int getExpireAfterWriteTime() {
+        return expireAfterWriteTime;
+    }
+
+    /**
+     * Set the expire After Access Write in case of time based Eviction (in seconds)
+     */
+    public void setExpireAfterWriteTime(int expireAfterWriteTime) {
+        this.expireAfterWriteTime = expireAfterWriteTime;
+    }
+
     // ****************************
     // Cloneable
     // ****************************

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/EvictionType.java
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/EvictionType.java b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/EvictionType.java
new file mode 100644
index 0000000..aebe5a8
--- /dev/null
+++ b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/EvictionType.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.caffeine;
+
+public enum EvictionType {
+
+    // type
+    SIZE_BASED("size_based"),
+    TIME_BASED("time_based");
+
+
+    private static EvictionType[] values = values();
+    private final String type;
+
+    EvictionType(String type) {
+        this.type = type;
+    }
+
+    public static EvictionType getEvictionType(String name) {
+        if (name == null) {
+            return null;
+        }
+        for (EvictionType evictionType : values) {
+            if (evictionType.toString().equalsIgnoreCase(name) || evictionType.name().equalsIgnoreCase(name)) {
+                return evictionType;
+            }
+        }
+        throw new IllegalArgumentException(String.format("Eviction type '%s' is not supported by this component.", name));
+    }
+
+    public String toString() {
+        return type;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java
index 00891a1..5f3169f 100644
--- a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java
+++ b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.caffeine.cache;
 
+import java.util.concurrent.TimeUnit;
+
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
 
@@ -23,6 +25,7 @@ import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.caffeine.CaffeineConfiguration;
+import org.apache.camel.component.caffeine.EvictionType;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
@@ -63,8 +66,13 @@ public class CaffeineCacheEndpoint extends DefaultEndpoint {
             cache = configuration.getCache();
         } else {
             Caffeine<Object, Object> builder = Caffeine.newBuilder();
-            builder.initialCapacity(configuration.getInitialCapacity());
-            builder.maximumSize(configuration.getMaximumSize());
+            if (configuration.getEvictionType() == EvictionType.SIZE_BASED) {
+                builder.initialCapacity(configuration.getInitialCapacity());
+                builder.maximumSize(configuration.getMaximumSize());
+            } else if (configuration.getEvictionType() == EvictionType.TIME_BASED) {
+                builder.expireAfterAccess(configuration.getExpireAfterAccessTime(), TimeUnit.SECONDS);
+                builder.expireAfterAccess(configuration.getExpireAfterWriteTime(), TimeUnit.SECONDS);
+            }
             if (configuration.isStatsEnabled()) {
                 builder.recordStats();
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/load/CaffeineLoadCacheEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/load/CaffeineLoadCacheEndpoint.java b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/load/CaffeineLoadCacheEndpoint.java
index 7cf27ce..6edd5eb 100644
--- a/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/load/CaffeineLoadCacheEndpoint.java
+++ b/components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/load/CaffeineLoadCacheEndpoint.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.caffeine.load;
 
+import java.util.concurrent.TimeUnit;
+
 import com.github.benmanes.caffeine.cache.Caffeine;
 import com.github.benmanes.caffeine.cache.LoadingCache;
 
@@ -23,6 +25,7 @@ import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.caffeine.CaffeineConfiguration;
+import org.apache.camel.component.caffeine.EvictionType;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
@@ -63,8 +66,13 @@ public class CaffeineLoadCacheEndpoint extends DefaultEndpoint {
             cache = (LoadingCache)configuration.getCache();
         } else {
             Caffeine<Object, Object> builder = Caffeine.newBuilder();
-            builder.initialCapacity(configuration.getInitialCapacity());
-            builder.maximumSize(configuration.getMaximumSize());
+            if (configuration.getEvictionType() == EvictionType.SIZE_BASED) {
+                builder.initialCapacity(configuration.getInitialCapacity());
+                builder.maximumSize(configuration.getMaximumSize());
+            } else if (configuration.getEvictionType() == EvictionType.TIME_BASED) {
+                builder.expireAfterAccess(configuration.getExpireAfterAccessTime(), TimeUnit.SECONDS);
+                builder.expireAfterAccess(configuration.getExpireAfterWriteTime(), TimeUnit.SECONDS);
+            }
             if (configuration.isStatsEnabled()) {
                 builder.recordStats();
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/cache/springboot/CaffeineCacheComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/cache/springboot/CaffeineCacheComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/cache/springboot/CaffeineCacheComponentConfiguration.java
index 1d43dad..f4b73de 100644
--- a/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/cache/springboot/CaffeineCacheComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/cache/springboot/CaffeineCacheComponentConfiguration.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.caffeine.cache.springboot;
 import javax.annotation.Generated;
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.CacheLoader;
+import org.apache.camel.component.caffeine.EvictionType;
+import org.apache.camel.component.caffeine.cache.CaffeineCacheComponent;
 import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
@@ -107,6 +109,20 @@ public class CaffeineCacheComponentConfiguration
          * Set the maximum size for the cache
          */
         private Integer maximumSize = 10000;
+        /**
+         * Set the eviction Type for this cache
+         */
+        private EvictionType evictionType = EvictionType.size_based;
+        /**
+         * Set the expire After Access Time in case of time based Eviction (in
+         * seconds)
+         */
+        private Integer expireAfterAccessTime = 300;
+        /**
+         * Set the expire After Access Write in case of time based Eviction (in
+         * seconds)
+         */
+        private Integer expireAfterWriteTime = 300;
 
         public Boolean getCreateCacheIfNotExist() {
             return createCacheIfNotExist;
@@ -187,5 +203,29 @@ public class CaffeineCacheComponentConfiguration
         public void setMaximumSize(Integer maximumSize) {
             this.maximumSize = maximumSize;
         }
+
+        public EvictionType getEvictionType() {
+            return evictionType;
+        }
+
+        public void setEvictionType(EvictionType evictionType) {
+            this.evictionType = evictionType;
+        }
+
+        public Integer getExpireAfterAccessTime() {
+            return expireAfterAccessTime;
+        }
+
+        public void setExpireAfterAccessTime(Integer expireAfterAccessTime) {
+            this.expireAfterAccessTime = expireAfterAccessTime;
+        }
+
+        public Integer getExpireAfterWriteTime() {
+            return expireAfterWriteTime;
+        }
+
+        public void setExpireAfterWriteTime(Integer expireAfterWriteTime) {
+            this.expireAfterWriteTime = expireAfterWriteTime;
+        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ffe1edf3/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/load/springboot/CaffeineLoadCacheComponentConfiguration.java
----------------------------------------------------------------------
diff --git a/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/load/springboot/CaffeineLoadCacheComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/load/springboot/CaffeineLoadCacheComponentConfiguration.java
index 426e9a6..e024b9b 100644
--- a/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/load/springboot/CaffeineLoadCacheComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-caffeine-starter/src/main/java/org/apache/camel/component/caffeine/load/springboot/CaffeineLoadCacheComponentConfiguration.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.caffeine.load.springboot;
 import javax.annotation.Generated;
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.CacheLoader;
+import org.apache.camel.component.caffeine.EvictionType;
+import org.apache.camel.component.caffeine.load.CaffeineLoadCacheComponent;
 import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
@@ -107,6 +109,20 @@ public class CaffeineLoadCacheComponentConfiguration
          * Set the maximum size for the cache
          */
         private Integer maximumSize = 10000;
+        /**
+         * Set the eviction Type for this cache
+         */
+        private EvictionType evictionType = EvictionType.size_based;
+        /**
+         * Set the expire After Access Time in case of time based Eviction (in
+         * seconds)
+         */
+        private Integer expireAfterAccessTime = 300;
+        /**
+         * Set the expire After Access Write in case of time based Eviction (in
+         * seconds)
+         */
+        private Integer expireAfterWriteTime = 300;
 
         public Boolean getCreateCacheIfNotExist() {
             return createCacheIfNotExist;
@@ -187,5 +203,29 @@ public class CaffeineLoadCacheComponentConfiguration
         public void setMaximumSize(Integer maximumSize) {
             this.maximumSize = maximumSize;
         }
+
+        public EvictionType getEvictionType() {
+            return evictionType;
+        }
+
+        public void setEvictionType(EvictionType evictionType) {
+            this.evictionType = evictionType;
+        }
+
+        public Integer getExpireAfterAccessTime() {
+            return expireAfterAccessTime;
+        }
+
+        public void setExpireAfterAccessTime(Integer expireAfterAccessTime) {
+            this.expireAfterAccessTime = expireAfterAccessTime;
+        }
+
+        public Integer getExpireAfterWriteTime() {
+            return expireAfterWriteTime;
+        }
+
+        public void setExpireAfterWriteTime(Integer expireAfterWriteTime) {
+            this.expireAfterWriteTime = expireAfterWriteTime;
+        }
     }
 }
\ No newline at end of file