问题描述
我正在尝试使用Spring Boot插入10个文档。无法使用TTL插入“少量文档”。没有错误或异常。如果我不提供ttl,那么它会按预期工作。
除此之外,如果我将ttl增加到其他值,那么也会创建所有文档。
另一方面,如果我减少了ttl,则无法插入更多文档。
我尝试从另一个POC插入失败的文档(10个文档中的单个文档),该文档具有与创建该文档相同的ttl。
public Flux<JsonDocument> upsertAll(final List<JsonDocument> jsonDocuments) {
return Flux
.from(keys())
.flatMap(key -> Flux
.fromIterable(jsonDocuments)
.parallel()
.runOn(Schedulers.parallel())
.flatMap(jsonDocument -> {
final String arg = String.format("upsertAll-%s",jsonDocument);
return Mono
.just(asyncBucket
.upsert(jsonDocument,1000,TimeUnit.MILLISECONDS)
.doOnError(error -> log.error(jsonDocument.content(),error,"failed to upsert")))
.map(obs -> Tuples.of(obs,jsonDocument.content()))
.map(tuple2 -> log.observableHandler(tuple2))
.map(observable1 -> Tuples.of(observable1,jsonDocument.content()))
.flatMap(tuple2 -> log.monoHandler(tuple2))
;
})
.sequential())
;
}
List<JsonDocument> jsonDocuments = new LinkedList<>();
dbService.upsertAll(jsonDocuments)
.subscribe();
请有人建议如何解决此问题。
解决方法
由于Couchbase服务器API中的古怪之处,小于30天的TTL值与大于30天的TTL值被区别对待。
为了获得与Couchbase Java SDK 2.x一致的行为,您需要在将TTL值传递到SDK之前对其进行调整:
// adjust TTL for Couchbase Java SDK 2.x
public static int adjustTtl(int ttlSeconds) {
return ttlSeconds < TimeUnit.DAYS.toSeconds(30)
? ttlSeconds
: (int) (ttlSeconds + (System.currentTimeMillis() / 1000));
}
在Couchbase Java SDK 3.0.6中,不再需要此功能;只需传递一个持续时间,SDK就会在必要时在后台调整值。