com.alilitech.cache.support.CaffeineCacheManager Maven / Gradle / Ivy
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alilitech.cache.support;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* {@link CacheManager} implementation that lazily builds {@link CaffeineCache}
* instances for each {@link #getCache} request. Also supports a 'static' mode
* where the set of cache names is pre-defined through {@link #setCacheNames},
* with no dynamic creation of further cache regions at runtime.
*
* The configuration of the underlying cache can be fine-tuned through a
* {@link Caffeine} builder or {@link CaffeineSpec}, passed into this
* CacheManager through {@link #setCaffeine}/{@link #setCaffeineSpec}.
* A {@link CaffeineSpec}-compliant expression value can also be applied
* via the {@link #setCacheSpecification "cacheSpecification"} bean property.
*
*
Requires Caffeine 2.1 or higher.
*
* @author Ben Manes
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Zhou Xiaoxiang
* @since 4.3
* @see CaffeineCache
*/
public class CaffeineCacheManager implements CacheManager {
private final ConcurrentMap cacheMap = new ConcurrentHashMap<>(16);
private boolean dynamic = true;
private Caffeine