marcv
marcv

Reputation: 1976

Redis subscriber is not notified by EXPIRE key 0

I've got a Redis client subscribed to __keyevent@0__:expired notifications. It works perfectly, either when the key expires by itself (ttl reached) or when I expire them manually with a number of seconds greater than 0, like so:

EXPIRE myKey 1

The subscriber sees the expired event and can therefore take some actions.

However, if I want to manually delete the key and have the subscriber notified, I use EXPIRE with 0 as the number of seconds:

EXPIRE myKey 0

The key gets deleted, but the subscriber doesn't receive anything.

I can't see anything related to this in the doc. Can anyone explain this behavior?

Upvotes: 1

Views: 158

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49942

From reviewing the source code (expire.c, ~252), setting an expiry value of <=0 (or using EXPIREAT with a time in the past) results in a deletion of the key rather than an expiry (and accordingly a DEL notification rather than an EXPIRED event).

This behavior is indeed undocumented and it would be good if you could submit a PR that fixes that to the documentation repo (https://github.com/antirez/redis-doc).

Upvotes: 1

Related Questions