Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis keyspace notifications - get both key and value change

Tags:

python

redis

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

like image 563
vgoklani Avatar asked Mar 02 '17 12:03

vgoklani


1 Answers

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.

like image 95
Itamar Haber Avatar answered Nov 07 '22 05:11

Itamar Haber