2020-02-07

Spring Couchbase Cache

Couchbase

If the Couchbase Java client and the couchbase-spring-cache implementation are available and
Couchbase is configured, a CouchbaseCacheManager is auto-configured. It is also possible to create
additional caches on startup by setting the configprop:spring.cache.cache-names[] property. These
caches operate on the Bucket that was auto-configured. You can also create additional caches on
another Bucket by using the customizer. Assume you need two caches (cache1 and cache2) on the
"main" Bucket and one (cache3) cache with a custom time to live of 2 seconds on the “another”
Bucket. You can create the first two caches through configuration, as follows:
spring.cache.cache-names=cache1,cache2

Then you can define a @Configuration class to configure the extra Bucket and the cache3 cache, as

@Configuration(proxyBeanMethods = false)
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("cache3", CacheBuilder.newInstance(anotherBucket())
.withExpiration(2));
};
}
}


No comments:

Post a Comment