You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.6.0中Couchbase counter()方法替代方案咨询

Spring Boot 2.6.0 + Spring Data Couchbase 4.3.0 实现Couchbase值自增方案

在Spring Boot 2.6.0对应的Spring Data Couchbase 4.3.0中,原Bucket类的counter()方法已被移除,可通过以下两种方案实现值自增逻辑,替代原有代码:

方案一:直接使用Couchbase Java SDK的MutateIn操作

该方案贴近底层SDK操作,与原counter()方法行为完全一致,适用于仅存储单个数值的文档:

import com.couchbase.client.java.Collection;
import com.couchbase.client.java.kv.MutateInOptions;
import com.couchbase.client.java.kv.MutateInResult;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Component;

@Component
public class CounterService {

    private final Collection collection;

    public CounterService(CouchbaseTemplate couchbaseTemplate) {
        // 获取默认集合,如需指定集合可传入集合名
        this.collection = couchbaseTemplate.getCouchbaseClientFactory().getCollection();
    }

    public String getConId(final Long start) {
        String counterKey = "RetDtcomConId";
        long incrementDelta = 1;

        try {
            // 执行自增操作:文档不存在则创建并设置初始值为start,存在则自增1
            MutateInResult result = collection.mutateIn(
                    counterKey,
                    MutateInOptions.mutateInOptions()
                            .upsertDocument(true) // 文档不存在时自动创建
            ).increment("", incrementDelta, start); // 空路径表示操作文档根值

            // 获取自增后的数值
            Long newId = result.contentAs("", Long.class);
            return newId.toString();
        } catch (Exception e) {
            throw new RuntimeException("自增计数器失败", e);
        }
    }
}

关键说明:

  • upsertDocument(true):确保当目标key不存在时自动创建文档,对应原counter()方法的初始值参数。
  • 空路径"":表示操作文档的根节点,与原counter()方法仅存储单一数值的行为一致。
  • 可根据业务需求调整durability参数(如PersistTo.ONE、ReplicateTo.ONE),保证数据持久化可靠性。

方案二:使用Spring Data Couchbase的MutateInById操作

该方案贴合Spring Data的Repository风格,适合通过实体类映射文档结构的场景:

1. 定义计数器实体类

import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Document;

@Document
public class CounterDocument {
    @Id
    private String id;
    private Long value;

    // Getter & Setter
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public Long getValue() { return value; }
    public void setValue(Long value) { this.value = value; }
}

2. 实现自增逻辑

import com.couchbase.client.java.kv.MutateInResult;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.stereotype.Service;

@Service
public class CounterService {

    private final CouchbaseTemplate couchbaseTemplate;

    public CounterService(CouchbaseTemplate couchbaseTemplate) {
        this.couchbaseTemplate = couchbaseTemplate;
    }

    public String getConId(final Long start) {
        String counterKey = "RetDtcomConId";
        long incrementDelta = 1;

        // 针对CounterDocument实体执行字段自增
        MutateInResult result = couchbaseTemplate.mutateInById(CounterDocument.class)
                .withId(counterKey)
                .upsert(true) // 文档不存在时自动创建
                .increment("value", incrementDelta, start) // 自增指定字段
                .execute();

        Long newId = result.contentAs("value", Long.class);
        return newId.toString();
    }
}

关键说明:

  • 通过mutateInById()方法绑定实体类,操作指定字段value实现自增。
  • upsert(true)同样保证文档不存在时自动创建,初始值为start参数。

内容的提问来源于stack exchange,提问作者Chaitnya athaley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:55:17