Skip to main content

Description

Remove the existing timeout on a key, turning the key from volatile (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).

Method Signature

persist(key: string): Promise<0 | 1>

Parameters

key
string
required
The key to remove expiration from

Return Value

result
0 | 1
  • 1 if the timeout was removed
  • 0 if key does not exist or does not have an associated timeout

Examples

Remove expiration from a key

await redis.set("mykey", "value");
await redis.expire("mykey", 100);

let ttl = await redis.ttl("mykey");
console.log(ttl); // ~100

const result = await redis.persist("mykey");
console.log(result); // 1

ttl = await redis.ttl("mykey");
console.log(ttl); // -1 (no expiration)

Key without expiration

await redis.set("mykey", "value");
const result = await redis.persist("mykey");
console.log(result); // 0 (key has no timeout)

Non-existent key

const result = await redis.persist("nonexistent");
console.log(result); // 0

Make a temporary key permanent

// Set a key with expiration
await redis.set("session:user123", "data", { ex: 3600 });

// Later, decide to keep it permanently
await redis.persist("session:user123");

// Key will now never expire
const ttl = await redis.ttl("session:user123");
console.log(ttl); // -1

See Also

  • expire - Set a key’s time to live
  • ttl - Get the time to live for a key