31.1.6 Couchbase

如果Couchbase可用,并配置好了,CouchbaseCacheManager将会自动配置,使用spring.cache.cache-names属性可以在启动时创建其他缓存。对Bucket的操作也是自动配置的,你可以使用customizer在另一个Bucket上创建其他缓存:假设你需要在“main” Bucket上存放两个缓存(foobar),在另一个Bucket上存放一个存活时间为2秒的biz缓存。首先,你通过配置创建两个缓存:

spring.cache.cache-names=foo,bar

然后定义其他@Configuration来配置另一个Bucketbiz缓存:

@Configuration
public class CouchbaseCacheConfiguration {

    private final Cluster cluster;

    public CouchbaseCacheConfiguration(Cluster cluster) {
        this.cluster = cluster;
    }

    @Bean
    public Bucket anotherBucket() {
        return this.cluster.openBucket("another", "secret");
    }

    @Bean
    public CacheManagerCustomizer<CouchbaseCacheManager> cacheManagerCustomizer() {
        return c -> {
            c.prepareCache("biz", CacheBuilder.newInstance(anotherBucket())
                    .withExpirationInMillis(2000));
        };
    }

}

这个示例配置重用了通过自动配置的Cluster

Last updated