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 018import io.jboot.Jboot; 019import io.jboot.components.cache.caffeine.CaffeineCacheImpl; 020import io.jboot.components.cache.caredis.JbootCaredisCacheImpl; 021import io.jboot.components.cache.ehcache.JbootEhcacheImpl; 022import io.jboot.components.cache.ehredis.JbootEhredisCacheImpl; 023import io.jboot.components.cache.j2cache.J2cacheImpl; 024import io.jboot.components.cache.none.NoneCacheImpl; 025import io.jboot.components.cache.redis.JbootRedisCacheImpl; 026import io.jboot.core.spi.JbootSpiLoader; 027import io.jboot.exception.JbootIllegalConfigException; 028import io.jboot.utils.ConfigUtil; 029import io.jboot.utils.StrUtil; 030 031import java.util.Map; 032import java.util.concurrent.ConcurrentHashMap; 033 034 035public class JbootCacheManager { 036 037 private static JbootCacheManager me = new JbootCacheManager(); 038 039 private JbootCacheManager() { 040 } 041 042 private Map<String, JbootCache> cacheMap = new ConcurrentHashMap<>(); 043 private CachePrinter printer = new CachePrinter() { 044 @Override 045 public void println(String debugInfo) { 046 CachePrinter.super.println(debugInfo); 047 } 048 }; 049 050 public static JbootCacheManager me() { 051 return me; 052 } 053 054 public JbootCache getCache() { 055 return getCache("default"); 056 } 057 058 public JbootCache getCache(String name) { 059 if (StrUtil.isBlank(name)) { 060 throw new IllegalArgumentException("Cache name must not be null or blank."); 061 } 062 063 JbootCache cache = cacheMap.get(name); 064 065 if (cache == null) { 066 Map<String, JbootCacheConfig> configModels = ConfigUtil.getConfigModels(JbootCacheConfig.class); 067 JbootCacheConfig.TYPES.forEach(configModels::remove); 068 069 configModels.putIfAbsent("default", Jboot.config(JbootCacheConfig.class)); 070 071 if (!configModels.containsKey(name)) { 072 throw new JbootIllegalConfigException("Please config \"jboot.cache." + name + ".type\" in your jboot.properties."); 073 } 074 075 JbootCacheConfig cacheConfig = configModels.get(name); 076 JbootCache newCache = buildCache(cacheConfig); 077 if (newCache != null) { 078 cacheMap.putIfAbsent(name, newCache); 079 } 080 081 cache = cacheMap.get(name); 082 } 083 084 return cache; 085 } 086 087 088 private synchronized JbootCache buildCache(JbootCacheConfig config) { 089 090 switch (config.getType()) { 091 case JbootCacheConfig.TYPE_EHCACHE: 092 return new JbootEhcacheImpl(config); 093 case JbootCacheConfig.TYPE_REDIS: 094 return new JbootRedisCacheImpl(config); 095 case JbootCacheConfig.TYPE_EHREDIS: 096 return new JbootEhredisCacheImpl(config); 097 case JbootCacheConfig.TYPE_J2CACHE: 098 return new J2cacheImpl(config); 099 case JbootCacheConfig.TYPE_CAFFEINE: 100 return new CaffeineCacheImpl(config); 101 case JbootCacheConfig.TYPE_CAREDIS: 102 return new JbootCaredisCacheImpl(config); 103 case JbootCacheConfig.TYPE_NONE: 104 return new NoneCacheImpl(config); 105 default: 106 return JbootSpiLoader.load(JbootCache.class, config.getType(), config); 107 } 108 } 109 110 public CachePrinter getPrinter() { 111 return printer; 112 } 113 114 public void setPrinter(CachePrinter printer) { 115 this.printer = printer; 116 } 117}