I want to use a record as a counter that expires after 20 seconds after the first increment.
Currently this is my approach:
Increment the counter once en retrieve the new value
If the new value is 1, I set the expiration to 20 seconds
This works correctly until i modify the record, for example by incrementing the counter.
After more increments, the expiration that was set in step 2. is cleared, and the ttl from the namespace’s default expiration is used.
Is it possible to update a record while keeping the original expiration?
Currently you cannot do a write without updating the TTL. You should calculate the new TTL from what the server returned but it wouldn’t be very accurate with processing and network delays.
As of server release 3.10.1 you can set a special TTL value of -2 on your updates, which means ‘do not modify TTL’. The initial write (insert) will have whatever TTL you give it, or inherit from the namespace default TTL. Subsequent updates with TTL -2 will not modify it.
That’s great thanks @rbotzer - another quick question… use case : is it possible to create if it doesn’t exist with a TTL as specified, or update if it does exist with TTL -2 without making 2 calls… one to see “does this exist” and a second to either update or create?
If you’re using it across the board for upsert, the initial insert will use the default TTL for the namespace, and the subsequent updates will not touch it, meaning the record will get closer and closer to its expiration. There’s no way to give it two values from the client, but you can think about setting the right default TTL for the namespace.
That’s great, thanks both - I guess the only way to achieve this then is to create a specific namespace for just this data in order to specify a default TTL, and then this will be unchanged by applications writing to the namespace.