Skip to content

Set Key Expiration - TTL

Description

This operation sets a time-to-live(TTL) timeout on a specific key. When the timeout expires, they key and the corresponding value are deleted. Note that, all keys are created without timeouts by default. This operation enables the timeout functionality.

Method

Backendless.Hive(hiveName).[bucketType](keyName).expireAfter(seconds): Promise<void>;

where:

Argument                Description
hiveName Name of a hive where the operation is performed. String value.
[bucketType] This placeholder must be substituted with one of the following stores types: KeyValueStore, ListStore, MapStore, SetStore, SortedSetStore.
keyName Key name to set the expiration timeout in seconds. String value.
seconds A timeout value in seconds. When the specified number of seconds elapses after the operation is complete, the key-value pair expires and is subsequently removed from the store. Integer value.

Return Value

None.

If the specified key does not exist, returns the following Backendless error:

{  
    "code": 27003,  
    "message": "no such key",  
    "errorData": {}  
}

Example

The example below sets the 60 seconds timeout for the 'fruits' key, once the timeout expires, the key gets deleted from the list store.

await Backendless.Hive('groceryStore').ListStore('fruits').expireAfter(60)

where:

Argument                Description
'groceryStore' Name of a hive where the operation is performed.
'fruits' Key name to set the 60 seconds timeout for.

Codeless Reference
general_api_set_key_expiration_ttl

where:

Argument                Description
hive name Name of a hive where the operation is performed.
type Storage type, can be one of the following: Key / Value, Sorted Set, Set, Map, List.
key name Key name to set a timeout for.
expire Expiration timeout type. Can be set to seconds after or on timestamp/date.

The after seconds identifies the number of seconds until the key expires.

The timestamp identifies a Unix timestamp/date when the key must expire. This option expects the number of seconds in the Unix format representing the exact date and time set for the key expiration.

Consider the following Key Value storage:

sample-key-value

The example below sets an expiration timeout on the "Japan" key for 100 seconds, once the timeout expires, the "Japan" key gets deleted from the store.

general_api_example_set_key_expiration_ttl