I’m using java-client 2.7.14 to connect CB server CE 6.0.0. To create a document with expiry, I’m using JsonDocument.create(String id, int expiry, JsonObject content) with expiry = 86400 for 1 day (afaik, for expiry < 30 days we set seconds, otherwise epoch as expiry value) . I can debug and track this value set correctly com.couchbase.client.java.CouchbaseBucket#insert(D document) ->
document=JsonDocument{id = ‘test-id’, cas=0, expiry=86400, content={ … }}
After bucket.insert(…) i don’t do any mutation operation more, yet when I query for the document I just inserted, I always see it’s “expiration” is zero:
SELECT META().* FROM test-bucket WHERE META().id = ‘test-id’
@unhuman thanks for the reply, but our target TTL will be ~90 days
i would try “bucket ttl”, but our server 6.0.0 and I don’t have a ttl option via create/edit bucket although mentioned in the dev. docs (maybe it’s only available on enterprise, idk).
my last resort would be a daily scheduled job to manually delete expired docs using a creationDate field… ugly but couldn’t find another way…
omg it worked! thanks @unhuman, its weird i didn’t encounter this on the docs. do you have any idea why i don’t have a bucket ttl option for create/update buckets settings although its on the docs?
and final question: did I get it correct that, if expiry < 30 days, it must be seconds from now, else exact epoch seconds;
int expiry = (days <= 30) ? days * 24 * 60 * 60 : LocalDateTime.now().plusDays(days).atZone(systemDefault()).toEpochSecond();
Yes. You have it correct. <= 30 days = relative time, > 30 days is absolute time. You could consider just using absolute time wherever you can. Or, you can upgrade your Java client to 3.0.x and use Duration, which provides a much more consistent interface for what you’re trying to do.
As mentioned by @vsr1 it looks like you’ll be able to get implicit TTL support in N1QL with 6.5.1.
TTL is a document setting, not a feature by bucket by cluster. So, if you wanted to change your TTLs for all your documents, you would need to perform a migration activity (touching every document).
Great, i ll try to upgrade server version then. Until then, i may try if i can use 3.x client.
TTL is a document meta as you pointed, but docs say it may be bucket wise too (but i didn’t have that opinion for my setup as i said in my previous message.
Yes, I’m sure. But, you shouldn’t do it because I tell you I’m sure - you have no idea who I am.
But… Let’s say you want a TTL in 10 seconds. You either pass the API 10 or you pass it System.currentTimeMillis()/ 1000 + 10. Assuming your clocks are synced (and share timezone - hopefully UTC), you should be able to see your document TTL in 10 seconds either way.