vgoklani
vgoklani

Reputation: 11746

Redis keyspace notifications - get both key and value change

I'm able to receive key-change pub-sub notifications in redis by launching the server as:

./redis-server --notify-keyspace-events KEA

and then simply running the following script:

import redis
connection = redis.StrictRedis()
pubsub = connection.pubsub()

pubsub.subscribe("__keyspace@0__:my_key")

in a separate listener thread. The message received looks like the following:

{'pattern': None, 'type': 'message', 'channel': '__keyspace@0__:my_key', 'data': 'set'}

What I would like to see in the message is both the updated "key" and the corresponding "value". How do I get the new value via pubsub?

Second question: How do I receive all "key" updates. I tried this:

pubsub.subscribe("__keyspace@0__:*")

but unfortunately it's not returning back anything.

This was a good reference for getting started: Redis keyspace event not-firing

Upvotes: 16

Views: 14793

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49942

Keyspace notifications do not report the value, only the key's name and/or command performed are included in the published message.

The main underlaying reasoning for this is that Redis values can become quite large.

If you really really really need this kind of behavior, well that's pretty easy actually. Because keyspace notifications are using Pub/Sub messages, you can just call PUBLISH yourself after each relevant operation, and with the information that you're interested in.

Upvotes: 19

Related Questions