Couchbase – How to create Spring Cache Couchbase application with SpringBoot
Tags: couchbase, springcache, springboot
Published: 2021-04-25 13:07:18

https://grokonez.com/spring-framework/spring-data/couchbase-create-spring-cache-couchbase-application-springboot

Couchbase – How to create Spring Cache Couchbase application with SpringBoot

Performance is a big problem in software development. And Caching is one solution to speed up system. In the past <a href="https://grokonez.com/spring-framework/spring-data/couchbase-create-spring-jpa-couchbase-application-springboot">post</a>, We are familiar with Couchbase database by learning how to build Spring JPA Couchbase application. So in the tutorial, <a href="https://grokonez.com">JavaSampleApproach</a> will continue to show you how to create <strong>Spring Cache Couchbase</strong> application with <a href="https://grokonez.com/tag/spring-boot">SpringBoot</a>.

Related posts:
- <a href="https://grokonez.com/spring-framework/spring-boot/cache-data-spring-cache-using-spring-boot">How to work with Spring Cache | Spring Boot</a>
- <a href="https://grokonez.com/spring-framework/spring-data/couchbase-create-spring-jpa-couchbase-application-springboot">Couchbase – How to create Spring JPA Couchbase application with SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-caffeine-cache-postgresql-backend">SpringBoot Caffeine cache with PostgreSQL backend</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-hazelcast-cache-postgresql-backend">SpringBoot Hazelcast cache with PostgreSQL backend</a>

<!--more-->
<h2>I. Technologies</h2>
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– Couchbase 4.6.2
<h2>II. Spring Cache Couchbase</h2>
We can use <strong>Couchbase</strong> as backing cache:

<img src="https://grokonez.com/wp-content/uploads/2017/08/spring-cache-couchbase-architecture-resize.png" alt="spring cache couchbase - architecture" width="700" height="366" class="alignnone size-full wp-image-7509" />

For work with <strong>SpringCache Couchbase</strong>, we need a dependency:

<pre><code class="language-html">&lt;dependency&gt;
	&lt;groupId&gt;com.couchbase.client&lt;/groupId&gt;
	&lt;artifactId&gt;couchbase-spring-cache&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>

How to configure Couchbase server as Cache?
-> We need to build a <code>CacheManager</code> with Couchbase cluster:

<pre><code class="language-java">
@EnableCaching
@Configuration
public class CacheConfig {

    ...
	
    @Bean(destroyMethod = "disconnect")
    public Cluster cluster() {
        return CouchbaseCluster.create(couchbaseServer);
    }

    @Bean(destroyMethod = "close")
    public Bucket bucket() {
        return cluster().openBucket(couchbaseBucket, couchbasePassword);
    }

    @Bean
    public CacheManager cacheManager() {
        CacheBuilder cacheBuilder = CacheBuilder.newInstance(bucket()).withExpiration(0);
        return new CouchbaseCacheManager(cacheBuilder, CACHE_NAME);
    }

}</code></pre>

<code>@EnableCaching</code> is used to enable the caching.

Document for Couchbase cache is built with <code>Serializable</code>:

More at:

https://grokonez.com/spring-framework/spring-data/couchbase-create-spring-cache-couchbase-application-springboot

Couchbase – How to create Spring Cache Couchbase application with SpringBoot