Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a keyspace event in redis for a key entering the database that isn't already present?

Tags:

redis

I have a program that utilizes a redis key with an expire time set. I want to detect when there is a new entry to the data set. I can tell when there is a removal by listening for the "expired" event, but the "set" and "expire" events are fired every time the key is set, even if it's already in the database.

Is there a keyspace event for a NEW key appearing?

like image 875
Jaxkr Avatar asked Aug 01 '16 23:08

Jaxkr


People also ask

How do I enable Keyspace notifications in Redis?

Notifications are enabled using the notify-keyspace-events of redis. conf or via the CONFIG SET. Setting the parameter to the empty string disables notifications.

What is Keyspace in Redis?

Redis keyspace notifications allow you to subscribe to PubSub channels. Through the channel, clients receive published events when a Redis command or data alteration occurs. These notifications are useful when an application must respond to changes that occur to the value stored in a particular key or keys.


1 Answers

There's no keyspace configuration that detects that a key was overwritten vs. newly added.

If you are primarily using the SET command, you may be able to take advantage of the NX option and publish a custom event based on the result. Obviously this isn't an ideal approach, but it's something. https://redis.io/commands/set

Example of a custom event:

PUBLISH __keyevent@0__:new_data_entry new_key

Details on that here: https://redis.io/topics/notifications#type-of-events

Hope that helps.

like image 144
davissp14 Avatar answered Dec 02 '22 19:12

davissp14