Visitar URL original
puter.kv.expire() - Puter.js Docs

puter.kv.expire()

Websites Puter Apps Node.js Workers

Sets a key's time-to-live (TTL) in seconds.

Syntax

puter.kv.expire(key, ttlSeconds)
puter.kv.expire(key, ttlSeconds, optConfig)

Parameters

key (String) (required)

The key to expire.

ttlSeconds (Number) (required)

The number of seconds until the key is removed. A value of 0 or less expires the key right away.

optConfig (Object) (optional)

KV sharing is in beta. Method shapes, limits, and behavior may change between releases.

The last argument. It takes these properties:

Return value

A Promise that resolves to true once the TTL is set.

Errors

With handle, the promise can reject with these codes:

Code Meaning
kv_handle_not_found handle doesn't exist, the share stopped, it isn't shared with you, or the call comes from an app other than the one whose data it shares.
kv_handle_access_denied The share's access doesn't allow this method, or the key is private to the owner's app.
kv_handle_option_invalid handle was combined with appUuid.
kv_handle_key_invalid With handle, the key is empty or longer than 768 bytes.
kv_handles_disabled Calls through a share are turned off on this deployment.

Keys that don't exist

A missing or already-expired key becomes an empty entry with the TTL you gave it, rather than staying absent. puter.kv.list() shows it and puter.kv.get() reads it as null, the same as a missing key. The old value, if there was one, doesn't come back. puter.kv.incr() rejects such an entry, the same as any non-number value.

To remove a key's TTL instead of setting one, use puter.kv.set() or puter.kv.update() with a null TTL.

Examples

Retrieve the value of a key after a 1-second expiration

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // (1) Create a new key-value pair
            await puter.kv.set('name', 'Puter Smith');
            puter.print("Key-value pair 'name' created/updated<br>");

            // (2) Set key to expire in 1 second
            await puter.kv.expire('name', 1);
            
            // (3) Wait 2 seconds and get the value
            setTimeout(async () => {
                const name = await puter.kv.get('name');
                puter.print(`Value: ${name}`);
            }, 2000);
        })();
    </script>
</body>
</html>