Redis can automatically clean data from memory using keyword. we can tell Redis to remove data at seconds, milisecond and epoch time.

List Command to Key Expirations

  • TTL key : this comand return integer (seconds) expiration, -1 means key without TTL.
    • exp: TTL totalemployee
    • get info TTL
    • 19835 second= 5.50972222 hour
    • IF we need to show expiry in miliseconds, we can use PTTL Commands. this commands will give us total milisecond to expiry.
  • PERSIST key : This command to remove time-to-live in key
    • exp: PERSIST totalemployee
    • persist keyword
  • EXPIRE key seconds [NX | XX | GT | LT]: Set a Time-to-live on key. this command will set expiration of key in seconds
    • exp: EXPIRE totalemployee 1000
    • EXPIRE
    • Options in EXPIRE
      • NX : Option expiry only when the key has no expiry.
      • XX : Option expiry only when the key has an existing expiry.
      • GT : Option expiry only when the new expiry is greater than current one.
      • LT : Option expiry only when the new expiry is less than current one.
    • Note : PEXPIRE this command to set expiry in miliseconds
  • EXPIREAT key unix-time-seconds [NX | XX | GT | LT ]
    • EXPIREAT will set expiry of key in spesifice date and time.
    • exp : Wells we say to expiry of key at “2024:02:27 22:00:00” convert to unix-time-seconds = 1709042400.
    • command : EXPIREAT totalemployee 1709042400
    • The Options EXPIREAT same with command EXPIRE.
    • Note : PEXIREAT with set expiry in unix-timestamp-miliseconds.

Reference :

Learning by Doing and Create Notes;

Tagged with: