my redis keys do not expire

后端 未结 1 560
猫巷女王i
猫巷女王i 2021-02-07 11:45

My redis server does not delete keys when the time-to-live reaches 0.

Here is a sample code:

redis-cli
>SET mykey \"ismykey\"
>EXPIRE mykey 20
#ch         


        
1条回答
  •  -上瘾入骨i
    2021-02-07 11:58

    Since you're doing a '...' it's hard to say for sure, but I'd say you're setting mykey during that part, which will effectively remove the expiration.

    From the EXPIRE manual

    The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands

    Also, regarding the -1 reply from TTL

    Return value

    Integer reply: TTL in seconds or -1 when key does not exist or does not have a timeout.

    EDIT: Note that this behaviour changed in Redis 2.8

    Starting with Redis 2.8 the return value in case of error changed:
    The command returns -2 if the key does not exist.
    The command returns -1 if the key exists but has no associated expire.

    In other words, if your key exists, it would seem to be persistent, ie not have any expiration set.

    EDIT: It seems I can reproduce this if I create the key on a REDIS slave server, the slave will not delete the key without master input, since normally you would not create keys locally on a slave. Is this the case here?

    However while the slaves connected to a master will not expire keys independently (but will wait for the DEL coming from the master), they'll still take the full state of the expires existing in the dataset, so when a slave is elected to a master it will be able to expire the keys independently, fully acting as a master.

    0 讨论(0)
提交回复
热议问题