001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 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 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 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 */ 016 017package org.springframework.boot.autoconfigure.cache; 018 019import java.time.Duration; 020import java.util.List; 021 022import com.couchbase.client.java.Bucket; 023import com.couchbase.client.spring.cache.CacheBuilder; 024import com.couchbase.client.spring.cache.CouchbaseCacheManager; 025 026import org.springframework.boot.autoconfigure.cache.CacheProperties.Couchbase; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 028import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 029import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 030import org.springframework.boot.context.properties.PropertyMapper; 031import org.springframework.cache.CacheManager; 032import org.springframework.context.annotation.Bean; 033import org.springframework.context.annotation.Conditional; 034import org.springframework.context.annotation.Configuration; 035import org.springframework.util.StringUtils; 036 037/** 038 * Couchbase cache configuration. 039 * 040 * @author Stephane Nicoll 041 * @since 1.4.0 042 */ 043@Configuration 044@ConditionalOnClass({ Bucket.class, CouchbaseCacheManager.class }) 045@ConditionalOnMissingBean(CacheManager.class) 046@ConditionalOnSingleCandidate(Bucket.class) 047@Conditional(CacheCondition.class) 048public class CouchbaseCacheConfiguration { 049 050 private final CacheProperties cacheProperties; 051 052 private final CacheManagerCustomizers customizers; 053 054 private final Bucket bucket; 055 056 public CouchbaseCacheConfiguration(CacheProperties cacheProperties, 057 CacheManagerCustomizers customizers, Bucket bucket) { 058 this.cacheProperties = cacheProperties; 059 this.customizers = customizers; 060 this.bucket = bucket; 061 } 062 063 @Bean 064 public CouchbaseCacheManager cacheManager() { 065 List<String> cacheNames = this.cacheProperties.getCacheNames(); 066 CacheBuilder builder = CacheBuilder.newInstance(this.bucket); 067 Couchbase couchbase = this.cacheProperties.getCouchbase(); 068 PropertyMapper.get().from(couchbase::getExpiration).whenNonNull() 069 .asInt(Duration::getSeconds).to(builder::withExpiration); 070 String[] names = StringUtils.toStringArray(cacheNames); 071 CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(builder, names); 072 return this.customizers.customize(cacheManager); 073 } 074 075}