ank19 commented on a change in pull request #6728:
URL: https://github.com/apache/camel/pull/6728#discussion_r784201790



##########
File path: 
components/camel-caffeine/src/main/java/org/apache/camel/component/caffeine/cache/CaffeineCacheEndpoint.java
##########
@@ -64,27 +64,37 @@ public Producer createProducer() throws Exception {
 
     @Override
     protected void doStart() throws Exception {
-        cache = CamelContextHelper.lookup(getCamelContext(), cacheName, 
Cache.class);
-        if (cache == null) {
-            Caffeine<?, ?> builder = Caffeine.newBuilder();
-            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.expireAfterWrite(configuration.getExpireAfterWriteTime(), 
TimeUnit.SECONDS);
-            }
-            if (configuration.isStatsEnabled()) {
-                if (ObjectHelper.isEmpty(configuration.getStatsCounter())) {
-                    builder.recordStats();
+
+        synchronized (this) {
+            cache = CamelContextHelper.lookup(getCamelContext(), cacheName, 
Cache.class);
+            if (cache == null) {
+                if (configuration.isCreateCacheIfNotExist()) {
+                    Caffeine<?, ?> builder = Caffeine.newBuilder();
+                    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.expireAfterWrite(configuration.getExpireAfterWriteTime(), 
TimeUnit.SECONDS);
+                    }
+                    if (configuration.isStatsEnabled()) {
+                        if 
(ObjectHelper.isEmpty(configuration.getStatsCounter())) {
+                            builder.recordStats();
+                        } else {
+                            
builder.recordStats(configuration::getStatsCounter);
+                        }
+                    }
+                    if 
(ObjectHelper.isNotEmpty(configuration.getRemovalListener())) {
+                        
builder.removalListener(configuration.getRemovalListener());
+                    }
+                    cache = builder.build();
+                    getCamelContext().getRegistry().bind(cacheName, 
Cache.class, cache);

Review comment:
       I have to say thanks for the valuable comments! Took me a while to grasp 
it, but hopefully I got it now, :-) I removed the binding/sync part and I added
   ```
   @BindToRegistry("cache")
   Cache cache = Caffeine.newBuilder().recordStats().build();
   ``` 
   to the documentation to illustrate it, hope that's fine - I guess that was 
my missing link in understanding.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to