Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic counters in Spring with Couchbase

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.

like image 753
Robin Ellerkmann Avatar asked Mar 19 '26 22:03

Robin Ellerkmann


1 Answers

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();
    }

}
like image 193
IgorekPotworek Avatar answered Mar 21 '26 11:03

IgorekPotworek