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.util; 018 019import java.io.Serializable; 020import java.util.Collection; 021import java.util.Map; 022import java.util.Set; 023import java.util.concurrent.atomic.LongAdder; 024 025import com.github.benmanes.caffeine.cache.Cache; 026import com.github.benmanes.caffeine.cache.Caffeine; 027import com.github.benmanes.caffeine.cache.RemovalCause; 028import com.github.benmanes.caffeine.cache.RemovalListener; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032/** 033 * A cache that uses a near optional LRU Cache. 034 * <p/> 035 * The Cache is implemented by Caffeine which provides an <a href="https://github.com/ben-manes/caffeine/wiki/Efficiency">efficient cache</a>. 036 * <p/> 037 * If this cache stores {@link org.apache.camel.Service} then this implementation will on eviction 038 * invoke the {@link org.apache.camel.Service#stop()} method, to auto-stop the service. 039 * <p/> 040 * Use {@link LRUCacheFactory} to create a new instance (do not use the constructor). 041 * 042 * @see LRUCacheFactory 043 * @see LRUSoftCache 044 * @see LRUWeakCache 045 */ 046public class LRUCache<K, V> implements Map<K, V>, RemovalListener<K, V>, Serializable { 047 private static final Logger LOG = LoggerFactory.getLogger(LRUCache.class); 048 049 protected final LongAdder hits = new LongAdder(); 050 protected final LongAdder misses = new LongAdder(); 051 protected final LongAdder evicted = new LongAdder(); 052 053 private int maxCacheSize = 10000; 054 private boolean stopOnEviction; 055 private final Cache<K, V> cache; 056 private final Map<K, V> map; 057 058 /** 059 * Constructs an empty <tt>LRUCache</tt> instance with the 060 * specified maximumCacheSize, and will stop on eviction. 061 * 062 * @param maximumCacheSize the max capacity. 063 * @throws IllegalArgumentException if the initial capacity is negative 064 */ 065 public LRUCache(int maximumCacheSize) { 066 this(16, maximumCacheSize); // 16 is the default initial capacity in ConcurrentLinkedHashMap 067 } 068 069 /** 070 * Constructs an empty <tt>LRUCache</tt> instance with the 071 * specified initial capacity, maximumCacheSize, and will stop on eviction. 072 * 073 * @param initialCapacity the initial capacity. 074 * @param maximumCacheSize the max capacity. 075 * @throws IllegalArgumentException if the initial capacity is negative 076 */ 077 public LRUCache(int initialCapacity, int maximumCacheSize) { 078 //Do not stop service if ConcurrentLinkedHashMap try to evict entry when its max capacity is zero. 079 this(initialCapacity, maximumCacheSize, maximumCacheSize > 0); 080 } 081 082 /** 083 * Constructs an empty <tt>LRUCache</tt> instance with the 084 * specified initial capacity, maximumCacheSize,load factor and ordering mode. 085 * 086 * @param initialCapacity the initial capacity. 087 * @param maximumCacheSize the max capacity. 088 * @param stopOnEviction whether to stop service on eviction. 089 * @throws IllegalArgumentException if the initial capacity is negative 090 */ 091 public LRUCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) { 092 this(initialCapacity, maximumCacheSize, stopOnEviction, false, false, false); 093 } 094 095 /** 096 * Constructs an empty <tt>LRUCache</tt> instance with the 097 * specified initial capacity, maximumCacheSize,load factor and ordering mode. 098 * 099 * @param initialCapacity the initial capacity. 100 * @param maximumCacheSize the max capacity. 101 * @param stopOnEviction whether to stop service on eviction. 102 * @param soft whether to use soft values a soft cache (default is false) 103 * @param weak whether to use weak keys/values as a weak cache (default is false) 104 * @param syncListener whether to use synchronous call for the eviction listener (default is false) 105 * @throws IllegalArgumentException if the initial capacity is negative 106 */ 107 public LRUCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction, 108 boolean soft, boolean weak, boolean syncListener) { 109 Caffeine<K, V> caffeine = Caffeine.newBuilder() 110 .initialCapacity(initialCapacity) 111 .maximumSize(maximumCacheSize) 112 .removalListener(this); 113 if (soft) { 114 caffeine.softValues(); 115 } 116 if (weak) { 117 caffeine.weakKeys(); 118 caffeine.weakValues(); 119 } 120 if (syncListener) { 121 caffeine.executor(Runnable::run); 122 } 123 124 this.cache = caffeine.build(); 125 this.map = cache.asMap(); 126 this.maxCacheSize = maximumCacheSize; 127 this.stopOnEviction = stopOnEviction; 128 } 129 130 @Override 131 public V get(Object o) { 132 V answer = map.get(o); 133 if (answer != null) { 134 hits.increment(); 135 } else { 136 misses.increment(); 137 } 138 return answer; 139 } 140 141 @Override 142 public int size() { 143 return map.size(); 144 } 145 146 @Override 147 public boolean isEmpty() { 148 return map.isEmpty(); 149 } 150 151 @Override 152 public boolean containsKey(Object o) { 153 return map.containsKey(o); 154 } 155 156 @Override 157 public boolean containsValue(Object o) { 158 return map.containsValue(0); 159 } 160 161 @Override 162 public V put(K k, V v) { 163 return map.put(k, v); 164 } 165 166 @Override 167 public V remove(Object o) { 168 return map.remove(o); 169 } 170 171 public void putAll(Map<? extends K, ? extends V> map) { 172 this.cache.putAll(map); 173 } 174 175 @Override 176 public void clear() { 177 map.clear(); 178 resetStatistics(); 179 } 180 181 @Override 182 public Set<K> keySet() { 183 return map.keySet(); 184 } 185 186 @Override 187 public Collection<V> values() { 188 return map.values(); 189 } 190 191 @Override 192 public Set<Entry<K, V>> entrySet() { 193 return map.entrySet(); 194 } 195 196 @Override 197 public void onRemoval(K key, V value, RemovalCause cause) { 198 if (cause.wasEvicted()) { 199 evicted.increment(); 200 LOG.trace("onRemoval {} -> {}", key, value); 201 if (stopOnEviction) { 202 try { 203 // stop service as its evicted from cache 204 ServiceHelper.stopService(value); 205 } catch (Exception e) { 206 LOG.warn("Error stopping service: " + value + ". This exception will be ignored.", e); 207 } 208 } 209 } 210 } 211 212 /** 213 * Gets the number of cache hits 214 */ 215 public long getHits() { 216 return hits.longValue(); 217 } 218 219 /** 220 * Gets the number of cache misses. 221 */ 222 public long getMisses() { 223 return misses.longValue(); 224 } 225 226 /** 227 * Gets the number of evicted entries. 228 */ 229 public long getEvicted() { 230 return evicted.longValue(); 231 } 232 233 /** 234 * Returns the maxCacheSize. 235 */ 236 public int getMaxCacheSize() { 237 return maxCacheSize; 238 } 239 240 /** 241 * Rest the cache statistics such as hits and misses. 242 */ 243 public void resetStatistics() { 244 hits.reset(); 245 misses.reset(); 246 evicted.reset(); 247 } 248 249 public void cleanUp() { 250 cache.cleanUp(); 251 } 252 253 @Override 254 public String toString() { 255 return "LRUCache@" + ObjectHelper.getIdentityHashCode(this); 256 } 257}