Distributed counter abstraction modelled after Redis INCRBY/DECRBY commands. Provides a local (in-memory) and Redis-backed implementation, selected automatically via Micronaut conditional beans.
Define named count stores in application.yml. Each entry under seqera.count creates a DefaultCountStore bean:
seqera:
count:
tasks:
prefix: task-counter
builds:
prefix: build-counterIf prefix is omitted, the configuration name is used as prefix.
Inject the store by qualifier:
@Inject
@Named("tasks")
CountStore taskCounter;
// the prefix is prepended to the key with a ':' separator
// e.g. prefix "task-counter" + key "pending" = "task-counter:pending"
long count = taskCounter.increment("pending"); // +1, returns new value
long count = taskCounter.increment("pending", 5); // +5, returns new value
long count = taskCounter.decrement("pending"); // -1, returns new value
long count = taskCounter.decrement("pending", 3); // -3, returns new value
long current = taskCounter.get("pending");For more control, extend AbstractCountStore directly:
@Singleton
public class TaskCounter extends AbstractCountStore {
protected TaskCounter(CountProvider provider) {
super(provider);
}
@Override
protected String getPrefix() {
return "tasks";
}
}The implementation is selected automatically based on the presence of a RedisActivator bean:
- Redis — when a
RedisActivatorbean is available - Local — when no
RedisActivatorbean is present (usesConcurrentHashMap+AtomicLong)
You can also inject CountProvider directly if you need to manage keys dynamically:
@Inject
CountProvider countProvider;
countProvider.increment("my-key", 1);
countProvider.decrement("my-key", 1);
long value = countProvider.get("my-key");