Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb TTL 24hours

Hi I am wonder about how to set the time to live in dynamo db.

I understand that I have to create a field, could be called ttl and set the value to be deleted, but in which format innodejs do I have to save to use for the ttl field for 20 or 24 hours?

Thanks for help.

like image 613
Pablo Alejandro Avatar asked Jul 31 '18 01:07

Pablo Alejandro


1 Answers

From the official DynamoDB documentation:

TTL compares the current time in epoch time format to the time stored in the Time To Live attribute of an item. [...]

Note

The epoch time format is the number of seconds elapsed since 12:00:00 AM January 1st, 1970 UTC.

In Javascript, you can get the number of milliseconds since the epoch by doing Date.now(). Once you have that, you can divide everything by 1000 to get the seconds (also rounding to the nearest integer value) and finally add the number of seconds in the TTL that you want.

This means that if you want to set the expiration time 24 from now, you can easily set the TTL field with the value expirationTime calculated this way:

const SECONDS_IN_AN_HOUR = 60 * 60;
const secondsSinceEpoch = Math.round(Date.now() / 1000);
const expirationTime = secondsSinceEpoch + 24 * SECONDS_IN_AN_HOUR;
like image 65
Stefano Dalpiaz Avatar answered Sep 17 '22 13:09

Stefano Dalpiaz