search

Home  >  Q&A  >  body text

nosql - redis expire 疑问

使用 redis-cli 设置 mykey, 并且设置其过期时间为 40s, 然后, 我再重新设置 mykey, 这时 mykey 的值会被重置, 我不明白的是为啥 过期时间也同时重置了?如果我仅仅想重置值, 不更改过期时间有没办法?

源码:

SET mykey "Hello"

EXPIRE mykey 40

EXISTS mykey

SET mykey "Hello"

EXISTS mykey

EXISTS mykey
给我你的怀抱给我你的怀抱2873 days ago748

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-27 09:04:48

    Go to the official website and read the description of expire:
    It’s explained like this:

    The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL, SET, GETSET and all the *STORE commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.

    If you use DEL, SET, GETSET, the value stored in the key will be replaced with a new one, and the command will also clear the timeout; if you add a piece of data to the list structure or change a field of the hset data, the timeout will not be cleared; If you want to overwrite the value through set, you must reset expire.

    Click the link

    reply
    0
  • 怪我咯

    怪我咯2017-04-27 09:04:48

    EXPIREAT and EXPIRE will reset the expiration time when updating value.

    Get the expiration time of the key through ttl before set
    After set, set the value of ttl to the expiration time

    192.168.1.9:6379> set key value
    OK
    192.168.1.9:6379> expire key 100
    (integer) 1
    192.168.1.9:6379> ttl key
    (integer) 98
    192.168.1.9:6379> set key value1
    OK
    192.168.1.9:6379> expire key 98
    (integer) 1
    192.168.1.9:6379> ttl key
    (integer) 96
    192.168.1.9:6379>

    But there will be errors in this way. I don’t know if others have better methods.

    reply
    0
  • Cancelreply