com.taotao.cloud.cache.jetcache.enhance.JetCacheSpringCacheManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-cloud-starter-cache-jetcache Show documentation
Show all versions of taotao-cloud-starter-cache-jetcache Show documentation
taotao-cloud-starter-cache-jetcache
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* 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.taotao.cloud.cache.jetcache.enhance;
import com.taotao.cloud.common.constant.SymbolConstant;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.lang.Nullable;
/** 基于 JetCache 的 Spring Cache Manager 扩展 */
public class JetCacheSpringCacheManager implements CacheManager {
private static final Logger log = LoggerFactory.getLogger(JetCacheSpringCacheManager.class);
private boolean dynamic = true;
private boolean allowNullValues = true;
private final Map cacheMap = new ConcurrentHashMap<>(16);
private final JetCacheCreateCacheFactory jetCacheCreateCacheFactory;
public JetCacheSpringCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory) {
this.jetCacheCreateCacheFactory = jetCacheCreateCacheFactory;
}
public JetCacheSpringCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, String... cacheNames) {
this.jetCacheCreateCacheFactory = jetCacheCreateCacheFactory;
setCacheNames(Arrays.asList(cacheNames));
}
public void setAllowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
}
public boolean isAllowNullValues() {
return allowNullValues;
}
private void setCacheNames(@Nullable Collection cacheNames) {
if (cacheNames != null) {
for (String name : cacheNames) {
this.cacheMap.put(name, createJetCache(name));
}
this.dynamic = false;
} else {
this.dynamic = true;
}
}
protected Cache createJetCache(String name) {
com.alicp.jetcache.Cache