Skip to main content

PEXPIRE

Syntax

PEXPIRE key seconds [NX | XX | GT | LT]

Module

generic

Categories

fast write

Description

Expire the key in the specified number of milliseconds. This commands turns a key into a volatile one.

Options

  • NX - Only set the expiry time if the key has no associated expiry.
  • XX - Only set the expiry time if the key already has an expiry time.
  • GT - Only set the expiry time if the new expiry time is greater than the current one.
  • LT - Only set the expiry time if the new expiry time is less than the current one.

Examples

The embedded API utilizes the ExpireOptions interface, which acts as a wrapper for the various expiry options.


ExpireOptions include the following constants:

  • NX - Only set the expiry time if the key has no associated expiry.
  • XX - Only set the expiry time if the key already has an expiry time.
  • GT - Only set the expiry time if the new expiry time is greater than the current one.
  • LT - Only set the expiry time if the new expiry time is less than the current one.

Add an expiration to a key:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
updated, err := db.PExpire("key", 10000, nil)

Add an expiration to a key only if it does not have one already:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
updated, err := db.PExpire("key", 10000, db.NX)

Add an expiration to a key only if it has one already:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
updated, err := db.PExpire("key", 10000, db.XX)

Add an expiration to a key only if it already has one that is less than the current expiry:

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
updated, err := db.PExpire("key", 10000, db.XX, db.LT)