001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl.cloud;
018
019import java.util.List;
020import java.util.concurrent.TimeUnit;
021
022import com.github.benmanes.caffeine.cache.Caffeine;
023import com.github.benmanes.caffeine.cache.LoadingCache;
024import org.apache.camel.cloud.ServiceDefinition;
025import org.apache.camel.cloud.ServiceDiscovery;
026import org.apache.camel.util.ObjectHelper;
027
028public final class CachingServiceDiscovery implements ServiceDiscovery {
029    private final ServiceDiscovery delegate;
030    private LoadingCache<String, List<ServiceDefinition>> cache;
031    private long timeout;
032
033    public CachingServiceDiscovery(ServiceDiscovery delegate) {
034        this(delegate, 60 * 1000);
035    }
036
037    public CachingServiceDiscovery(ServiceDiscovery delegate, long timeout, TimeUnit unit) {
038        this(delegate, unit.toMillis(timeout));
039    }
040
041    public CachingServiceDiscovery(ServiceDiscovery delegate, long timeout) {
042        this.delegate = ObjectHelper.notNull(delegate, "delegate");
043        setTimeout(timeout);
044    }
045
046    public ServiceDiscovery getDelegate() {
047        return this.delegate;
048    }
049
050    public void setTimeout(long timeout) {
051        this.timeout = timeout;
052        this.cache = Caffeine.newBuilder()
053            .expireAfterAccess(timeout, TimeUnit.MILLISECONDS)
054            .build(delegate::getServices);
055    }
056
057    public void setTimeout(long timeout, TimeUnit unit) {
058        setTimeout(unit.toMillis(timeout));
059    }
060
061    public long getTimeout() {
062        return timeout;
063    }
064
065    public CachingServiceDiscovery timeout(long timeout) {
066        setTimeout(timeout);
067        return this;
068    }
069
070    public CachingServiceDiscovery timeout(long timeout, TimeUnit unit) {
071        setTimeout(timeout, unit);
072        return this;
073    }
074
075    @Override
076    public List<ServiceDefinition> getServices(String name) {
077        return cache.get(name);
078    }
079
080    // **********************
081    // Helpers
082    // **********************
083
084    public static CachingServiceDiscovery wrap(ServiceDiscovery delegate) {
085        return new CachingServiceDiscovery(delegate);
086    }
087
088    public static CachingServiceDiscovery wrap(ServiceDiscovery delegate, long timeout) {
089        return new CachingServiceDiscovery(delegate).timeout(timeout);
090    }
091
092    public static CachingServiceDiscovery wrap(ServiceDiscovery delegate, long timeout, TimeUnit unit) {
093        return new CachingServiceDiscovery(delegate).timeout(timeout, unit);
094    }
095}