Spring Cache Abstraction with Couchbase

Caner Kaya
2 min readSep 6, 2021

Firstly, this artical is not a tutorial about Spring Cache and Couchbase Server. The intention of that is to show how to configure our Spring Boot application for using Couchbase Server as a cache storage.

Now, let’s get started. As you guess, we need run a Couchbase Server. So we will start a single node cluster Couchbase Server by using Docker.

docker run -d --name couchbase-local -p 8091-8096:8091-8096 -p 11210-11211:11210-11211 couchbase

Then check http://localhost:8091. You will see setup screen.

Couchbase Server setup screen

Setup a new Cluster with password Pass123*.

New Cluster screen

Now we need a bucket with name book-bucket.

New Bucket screen

Finally, we are done with Couchbase installation. For more detail about Couchbase installation part, you can visit here.

It is time to build the Spring Boot application from start.spring.io. Here the dependencies

start.spring.io

Next step is to set properties and make configurations. Create application.yml and CacheConfig.java files.

application.yml
CacheConfig.java

So we will implement a Book (must be implement Serializable) model and API to serve resource.

Finally we have one more annotation to make application to enable caching. Let’s add @EnableCaching.

Now we can start testing. Open terminal and run the commands.

curl --request GET http://localhost:8080/book
curl --request GET http://localhost:8080/book
curl --request GET http://localhost:8080/book/1
curl --request GET http://localhost:8080/book/1
curl --request GET http://localhost:8080/book/2
curl --request GET http://localhost:8080/book/2

Here is the application logs.

Thank you for reading. You can access the source code from here.

--

--