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.util.concurrent.atomic.AtomicBoolean; 020 021import org.apache.camel.util.concurrent.ThreadHelper; 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024 025/** 026 * Factory to create {@link LRUCache} instances. 027 */ 028public final class LRUCacheFactory { 029 030 private static final Logger LOG = LoggerFactory.getLogger(LRUCacheFactory.class); 031 032 private static final AtomicBoolean INIT = new AtomicBoolean(); 033 034 private LRUCacheFactory() { 035 } 036 037 /** 038 * Warm-up the LRUCache to startup Apache Camel faster. 039 */ 040 public static void warmUp() { 041 // create a dummy map in a separate thread to warm-up the Caffeine cache concurrently 042 // while Camel is starting up. This allows us to overall startup Camel a bit faster 043 // as Caffeine takes 150+ millis to initialize. 044 if (INIT.compareAndSet(false, true)) { 045 // only need to init Caffeine once in the JVM/classloader 046 Runnable task = () -> { 047 StopWatch watch = new StopWatch(); 048 LOG.debug("Warming up LRUCache ..."); 049 newLRUCache(16); 050 LOG.debug("Warming up LRUCache complete in {} millis", watch.taken()); 051 }; 052 053 String threadName = ThreadHelper.resolveThreadName(null, "LRUCacheFactory"); 054 055 Thread thread = new Thread(task, threadName); 056 thread.start(); 057 } 058 } 059 060 /** 061 * Constructs an empty <tt>LRUCache</tt> instance with the 062 * specified maximumCacheSize, and will stop on eviction. 063 * 064 * @param maximumCacheSize the max capacity. 065 * @throws IllegalArgumentException if the initial capacity is negative 066 */ 067 public static LRUCache newLRUCache(int maximumCacheSize) { 068 LOG.trace("Creating LRUCache with maximumCacheSize: {}", maximumCacheSize); 069 return new LRUCache(maximumCacheSize); 070 } 071 072 /** 073 * Constructs an empty <tt>LRUCache</tt> instance with the 074 * specified initial capacity, maximumCacheSize, and will stop on eviction. 075 * 076 * @param initialCapacity the initial capacity. 077 * @param maximumCacheSize the max capacity. 078 * @throws IllegalArgumentException if the initial capacity is negative 079 */ 080 public static LRUCache newLRUCache(int initialCapacity, int maximumCacheSize) { 081 LOG.trace("Creating LRUCache with initialCapacity: {}, maximumCacheSize: {}", initialCapacity, maximumCacheSize); 082 return new LRUCache(initialCapacity, maximumCacheSize); 083 } 084 085 /** 086 * Constructs an empty <tt>LRUCache</tt> instance with the 087 * specified initial capacity, maximumCacheSize,load factor and ordering mode. 088 * 089 * @param initialCapacity the initial capacity. 090 * @param maximumCacheSize the max capacity. 091 * @param stopOnEviction whether to stop service on eviction. 092 * @throws IllegalArgumentException if the initial capacity is negative 093 */ 094 public static LRUCache newLRUCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) { 095 LOG.trace("Creating LRUCache with initialCapacity: {}, maximumCacheSize: {}, stopOnEviction: {}", initialCapacity, maximumCacheSize, stopOnEviction); 096 return new LRUCache(initialCapacity, maximumCacheSize, stopOnEviction); 097 } 098 099 /** 100 * Constructs an empty <tt>LRUSoftCache</tt> instance with the 101 * specified maximumCacheSize, and will stop on eviction. 102 * 103 * @param maximumCacheSize the max capacity. 104 * @throws IllegalArgumentException if the initial capacity is negative 105 */ 106 public static LRUSoftCache newLRUSoftCache(int maximumCacheSize) { 107 LOG.trace("Creating LRUSoftCache with maximumCacheSize: {}", maximumCacheSize); 108 return new LRUSoftCache(maximumCacheSize); 109 } 110 111 /** 112 * Constructs an empty <tt>LRUWeakCache</tt> instance with the 113 * specified maximumCacheSize, and will stop on eviction. 114 * 115 * @param maximumCacheSize the max capacity. 116 * @throws IllegalArgumentException if the initial capacity is negative 117 */ 118 public static LRUWeakCache newLRUWeakCache(int maximumCacheSize) { 119 LOG.trace("Creating LRUWeakCache with maximumCacheSize: {}", maximumCacheSize); 120 return new LRUWeakCache(maximumCacheSize); 121 } 122}