001/*
002 * Copyright 2012-2017 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.util.List;
020
021import com.couchbase.client.java.Bucket;
022import com.couchbase.client.spring.cache.CacheBuilder;
023import com.couchbase.client.spring.cache.CouchbaseCacheManager;
024
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
028import org.springframework.cache.CacheManager;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Conditional;
031import org.springframework.context.annotation.Configuration;
032
033/**
034 * Couchbase cache configuration.
035 *
036 * @author Stephane Nicoll
037 * @since 1.4.0
038 */
039@Configuration
040@ConditionalOnClass({ Bucket.class, CouchbaseCacheManager.class })
041@ConditionalOnMissingBean(CacheManager.class)
042@ConditionalOnSingleCandidate(Bucket.class)
043@Conditional(CacheCondition.class)
044public class CouchbaseCacheConfiguration {
045
046        private final CacheProperties cacheProperties;
047
048        private final CacheManagerCustomizers customizers;
049
050        private final Bucket bucket;
051
052        public CouchbaseCacheConfiguration(CacheProperties cacheProperties,
053                        CacheManagerCustomizers customizers, Bucket bucket) {
054                this.cacheProperties = cacheProperties;
055                this.customizers = customizers;
056                this.bucket = bucket;
057        }
058
059        @Bean
060        public CouchbaseCacheManager cacheManager() {
061                List<String> cacheNames = this.cacheProperties.getCacheNames();
062                CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(
063                                CacheBuilder.newInstance(this.bucket)
064                                                .withExpiration(this.cacheProperties.getCouchbase()
065                                                                .getExpirationSeconds()),
066                                cacheNames.toArray(new String[cacheNames.size()]));
067                return this.customizers.customize(cacheManager);
068        }
069
070}