001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.components.cache;
017
018
019import io.jboot.utils.StrUtil;
020
021import java.util.Set;
022import java.util.concurrent.ConcurrentHashMap;
023
024public abstract class JbootCacheBase implements JbootCache {
025
026    protected JbootCacheConfig config;
027
028    public JbootCacheBase(JbootCacheConfig config) {
029        this.config = config;
030    }
031
032    private Set<String> ignoreThreadCacheNames = ConcurrentHashMap.newKeySet();
033    private ThreadLocal<String> CACHE_NAME_PREFIX_TL = new ThreadLocal<>();
034
035    @Override
036    public JbootCacheConfig getConfig() {
037        return config;
038    }
039
040    @Override
041    public JbootCache setThreadCacheNamePrefix(String cacheNamePrefix) {
042        if (StrUtil.isNotBlank(cacheNamePrefix)) {
043            CACHE_NAME_PREFIX_TL.set(cacheNamePrefix);
044        } else {
045            CACHE_NAME_PREFIX_TL.remove();
046        }
047        return this;
048    }
049
050    @Override
051    public void clearThreadCacheNamePrefix() {
052        CACHE_NAME_PREFIX_TL.remove();
053    }
054
055
056    @Override
057    public boolean addThreadCacheNamePrefixIngore(String cacheName) {
058        return ignoreThreadCacheNames.add(cacheName);
059    }
060
061    @Override
062    public boolean removeThreadCacheNamePrefixIngore(String cacheName) {
063        return ignoreThreadCacheNames.remove(cacheName);
064    }
065
066
067    /**
068     * 构建缓存名称
069     *
070     * @param cacheName
071     * @return
072     */
073    protected String buildCacheName(String cacheName) {
074
075        String cacheNamePrefix = null;
076
077        if (!ignoreThreadCacheNames.contains(cacheName)) {
078            cacheNamePrefix = CACHE_NAME_PREFIX_TL.get();
079        }
080
081        if (StrUtil.isBlank(cacheNamePrefix)) {
082            cacheNamePrefix = config.getDefaultCachePrefix();
083        }
084
085        return StrUtil.isNotBlank(cacheNamePrefix) ? (cacheNamePrefix + ":" + cacheName) : cacheName;
086    }
087
088
089    @Override
090    public void refresh(String cacheName, Object key) {
091
092    }
093
094    @Override
095    public void refresh(String cacheName) {
096
097    }
098
099
100    protected void println(String debugInfo) {
101        JbootCacheManager.me().getPrinter().println(debugInfo);
102    }
103}