|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.cache; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.cache2k.Cache2kBuilder; |
| 23 | +import org.cache2k.extra.spring.SpringCache2kCacheManager; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.ObjectProvider; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 28 | +import org.springframework.cache.CacheManager; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Conditional; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.util.CollectionUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Cache2k cache configuration. |
| 36 | + * |
| 37 | + * @author Jens Wilke |
| 38 | + * @author Stephane Nicoll |
| 39 | + */ |
| 40 | +@Configuration(proxyBeanMethods = false) |
| 41 | +@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCacheManager.class }) |
| 42 | +@ConditionalOnMissingBean(CacheManager.class) |
| 43 | +@Conditional(CacheCondition.class) |
| 44 | +class Cache2kCacheConfiguration { |
| 45 | + |
| 46 | + @Bean |
| 47 | + SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers, |
| 48 | + ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) { |
| 49 | + SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager(); |
| 50 | + cacheManager.defaultSetup(configureDefaults(cache2kBuilderCustomizers)); |
| 51 | + Collection<String> cacheNames = cacheProperties.getCacheNames(); |
| 52 | + if (!CollectionUtils.isEmpty(cacheNames)) { |
| 53 | + cacheManager.setDefaultCacheNames(cacheNames); |
| 54 | + } |
| 55 | + return customizers.customize(cacheManager); |
| 56 | + } |
| 57 | + |
| 58 | + private Function<Cache2kBuilder<?, ?>, Cache2kBuilder<?, ?>> configureDefaults( |
| 59 | + ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) { |
| 60 | + return (builder) -> { |
| 61 | + cache2kBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); |
| 62 | + return builder; |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments