Couchbase Client 2.x with Spring Cache

Any plans to integrate Couchbase 2.x with Spring cache? Any help would be appreciated.

It’s been separated in its own project (so that it is not tied to longer Spring Data release cycles) in our github: couchbaselabs/couchbase-spring-cache.

For now it’s still in development. A version should be released and deployed to Maven Central soon, but you can certainly play around with it by building the master with Maven if you’re so inclined.

Feedback welcome!! :bow:

Do we have release target date on this?

I love to contribute if you need help.

2 Likes

there’s no release target date yet, as we’re working with Stéphane Nicoll (the Spring Boot lead) in order to improve the implementation for a great integration into Spring Boot, right from the start.
when that’s stabilized a little, then a 2.0.0 release will be quick to come.

That said, Spring Cache is pretty straightforward (a couple classes), and works in the current state even if there’s still room for improvement (weird CacheManager constructor, no dynamic creation of caches), so you could start using it, if you’re ok with changing configuration code later on when this hits GA…

I could tag the current state as 2.0.0.M1, mark it as a pre-release in Github (and probably attach the jar to the release), if that helps?

1 Like

Can you please help me how i can configure com.couchbase.client.spring.cache.CouchbaseCacheManager in XML

I am trying to migrate from Couchbase client (using Spring cache) to Java client and i would like to keep Spring cache support with @Cacheable.

I don’t understand your last sentence? What’s the “Couchbase client” and what’s the “Java client”?

In order to use the CouchbaseCacheManager and configure it in xml, you’ll need:

  • the common xml setup of spring cache (see documentation)
  • a Couchbase Bucket as a bean in your context (so you need to open it from a Cluster, it’s a little more involved in xml)
  • declare a CacheBuilder bean
  • declare a CouchbaseCacheManager bean

Here is an example of all those couchbase-related beans:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="couchbaseCluster" class="com.couchbase.client.java.CouchbaseCluster" factory-method="create">
        <constructor-arg>
            <list>
                <!-- list the IP/hostnames to bootstrap from (recommended: 2) -->
                <value>127.0.0.1</value>
            </list>
        </constructor-arg>
    </bean>

    <bean id="couchbaseBucket" class="com.couchbase.client.java.Bucket"
          factory-bean="couchbaseCluster" factory-method="openBucket">
        <!-- specify the bucket and password to use -->
        <constructor-arg index="0" type="java.lang.String" value="default"/>
        <constructor-arg index="1" type="java.lang.String" value=""/>
    </bean>

    <bean id="couchbaseCacheBuilder" class="com.couchbase.client.spring.cache.CacheBuilder"
          factory-method="newInstance">
        <constructor-arg type="com.couchbase.client.java.Bucket" ref="couchbaseBucket"/>
    </bean>

    <bean id="cacheManager" class="com.couchbase.client.spring.cache.CouchbaseCacheManager">
        <constructor-arg type="com.couchbase.client.spring.cache.CacheBuilder" ref="couchbaseCacheBuilder"/>
        <constructor-arg>
            <list>
                <!-- pre-declare caches you intend to use -->
                <value>staticCache1</value>
                <value>staticCache2</value>
                <value>staticCache3</value>
            </list>
        </constructor-arg>
    </bean>
</beans>

I added an integration test to demonstrate that in the master branch

@babu.maganti / @bmaganti I would still consider wiring all that through a @Configuration annotated class (java config). It can get complicated in xml if you want to tune the SDK’s Cluster.

Side-note: You probably saw that there was a first release on Maven Central?

1 Like

Yes, i saw the 2.0.0 release.

I am not sure why you didn’t use couchbase xsd.

Anyway thank you for your inputs.

I will update you once i complete…

In between, i have two caches setup

  1. Couchbase
  2. JVM using Ehcache

I am not sure how this XML config will work with Ehcache config

Thank you so much @simonbasle … Its working like charm.

@simonbasle - Do you happens to know where i can see Spring Data and Spring Cache example with Client 2?

A demo application is on our radar, but I don’t have a roadmap for it yet :frowning:
In the meantime I recommend looking at the tests, even though it’s not ideal in terms of finding a particular information, or look at the documentation topics.

@simonbasle - I am seeing following error… any idea?

Failed to instantiate [com.couchbase.client.java.Bucket]: Factory method ‘openBucket’ threw exception; nested exception is java.lang.RuntimeException: java.util.concurrent.TimeoutException

Maybe the Couchbase cluster is on another machine with network latency? Can you try pinging the couchbase server machine from the machine where the client runs?
We’ve also seen cases where connecting to a node the first time would take a long time (or even timeout) on Windows.

The Cluster can be created with a CouchbaseEnvironment, which in turn allows you to tune the connectTimeout (in milliseconds). But once again, this is getting tricky with XML configuration :frowning:

I am checking if there is issues with our deployment process… because its working some times and its throwing error other times.

And i am tuning the timeout as well.

By the way we didn’t see this issue with Couchbase Client 1

We are migrated from Couchbase Client to JDK 2 client. and we are seeing this issue.

Increased CB timeout to 3mins and i am not seeing the error.