Introduction
The atProtocol has various attributes that can be set on the metadata of a key. Time To Live (TTL) is one such attribute.
Please refer to the AtMetadata class for the complete list of metadata attributes.
By using TTL one can create self-destructive keys. Let's understand how one can create a key with TTL with the atProtocol.
Description:
TTL (Time To Live) is the period beyond which the key will expire. TTL is expressed in milliseconds. Adding TTL to the time on which it is created (in milliseconds epoch) results in the time at which the key expires. As long as the key has not expired, a “lookup” of the key returns the value that was set originally. After the key expiry, ‘null’ is returned when the key is looked up for a value. Subsequently, all the expired keys are deleted through a background task.
Below is the snippet of how TTL is calculated. For more information, refer to AtMetadataBuilder.
DateTime _getExpiresAt(int epochNow, int ttl, {int ttb}) {
if (ttl != null) {
var expiresAt = epochNow + ttl;
if (ttb != null) {
expiresAt = expiresAt + ttb;
}
return DateTime.fromMillisecondsSinceEpoch(expiresAt).toUtc();
}
return null;
}
Setting TTL on a key using the Atsign Dart SDK
In the below example, the value of the OTP (One Time Password) is available for 1 minute (60000 milliseconds) before the key expires.
var metadata = Metadata()..ttl = 60000;
var phoneKey = AtKey()
..key = 'OTP'
..metadata = metadata;
var value = '545879';
Setting TTL to key from the command line:
update:ttl:60000:@alice:otp@bob '545879'
For more samples, refer to update_verb_test.
Coming Soon
We are working to make the Internet better. Come code with us - https://atsign.dev !