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 019/** 020 * A cache that uses a near optional LRU Cache using {@link java.lang.ref.SoftReference}. 021 * <p/> 022 * The Cache is implemented by Caffeine which provides an <a href="https://github.com/ben-manes/caffeine/wiki/Efficiency">efficient cache</a>. 023 * <p/> 024 * This implementation uses {@link java.lang.ref.SoftReference} for stored values in the cache, to support the JVM 025 * when it wants to reclaim objects when it's running out of memory. Therefore this implementation does 026 * not support <b>all</b> the {@link java.util.Map} methods. 027 * <p/> 028 * <b>Only</b> methods below should be used: 029 * <ul> 030 * <li>containsKey - To determine if the key is in the cache and refers to a value</li> 031 * <li>entrySet - To return a set of all the entries (as key/value pairs)</li> 032 * <li>get - To get a value from the cache</li> 033 * <li>isEmpty - To determine if the cache contains any values</li> 034 * <li>keySet - To return a set of the current keys which refers to a value</li> 035 * <li>put - To add a value to the cache</li> 036 * <li>putAll - To add values to the cache</li> 037 * <li>remove - To remove a value from the cache by its key</li> 038 * <li>size - To get the current size</li> 039 * <li>values - To return a copy of all the value in a list</li> 040 * </ul> 041 * <p/> 042 * The {@link #containsValue(Object)} method should <b>not</b> be used as it's not adjusted to check 043 * for the existence of a value without catering for the soft references. 044 * <p/> 045 * Notice that if the JVM reclaims memory, the content of this cache may be garbage collected without any 046 * eviction notifications. 047 * <p/> 048 * Use {@link LRUCacheFactory} to create a new instance (do not use the constructor). 049 * 050 * @see LRUCacheFactory 051 * @see LRUCache 052 * @see LRUWeakCache 053 */ 054public class LRUSoftCache<K, V> extends LRUCache<K, V> { 055 056 public LRUSoftCache(int maximumCacheSize) { 057 this(16, maximumCacheSize); 058 } 059 060 public LRUSoftCache(int initialCapacity, int maximumCacheSize) { 061 this(initialCapacity, maximumCacheSize, false); 062 } 063 064 public LRUSoftCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) { 065 super(initialCapacity, maximumCacheSize, stopOnEviction, true, false, false); 066 } 067 068 @Override 069 public String toString() { 070 return "LRUSoftCache@" + ObjectHelper.getIdentityHashCode(this); 071 } 072}