Atomic counters in Spring with Couchbase

↘锁芯ラ 提交于 2019-12-07 16:53:50

问题


Is it possible to use Couchbase's Atomic Counters with the Spring Connector? Currently I have a repository for a specific document type and want to have an atomic counter for every document. I know that the Java SDK offers the functionality but I was not able to find an equivalent action for Spring.


回答1:


As I know Spring doesn't have support for counters but it's pretty simple to write own repository:

@Repository
public class CountersRepository {

    private static final long INITIAL_COUNTER_VALUE = 1;

    @Autowired
    private Bucket bucket;

    public void incCounter(final String counter) {
        bucket.counter(counter, 1, INITIAL_COUNTER_VALUE);
    }

    public void decCounter(final String counter) {
        bucket.counter(counter, -1, INITIAL_COUNTER_VALUE);
    }

    public Long getCounterValue(final String counter) {
        return bucket.counter(counter, 0).content();
    }

}


来源:https://stackoverflow.com/questions/42507421/atomic-counters-in-spring-with-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!