Redis hset and key space notification

I am using Redis 2.8 Pub / Sub key space, I would like to know if it is possible to be notified of which field has changed after the command HSET

?

I am currently getting a notification for the key as a result HSET

, but I would be better aware of which field was set - I understand that I can read the set again to look at the differences after being notified, but I don't find it very efficient ...

+3


source to share


3 answers


The standard Redis key notifications do not include data about the data that was changed, and in particular do not contain information about the affected Hash field.



+2


source


While this is not exactly what you want, it can still be used as a workaround. Try to have a unique hash key name like:

redis.hmset('task:{}'.format(unique_id), status='running')

      

And when you receive the message, it will look like this:



(b'__keyspace@0__:task:c81b8373-b5ea-4be0-b8f1-b490e7280898', 'hset')

      

Now, knowing the unique ID of the task, you can:

redis.hget('task:{}'.format(unique_id), 'status')
> running

      

0


source


I don't know if you found your solution, but I want to share how I work with Redis and NodeJS.

I have a redis client created:

var client =  redis.createClient(redis_options);

      

So this client will receive messages when redis notification arrives. (redis server was configured for this redis-cli config set notify-keyspace-events KEA

-> - https://redis.io/topics/notifications )

client.on('pmessage', function(pattern, channel, message) {

      

Here the "pmessage" event offers us three parameters ( https://www.npmjs.com/package/redis ):

pattern: pattern for the locked key channel: the channel you are listening to ( '__ keyspace @ 0 __:' + hash) message: operation performed in redis (e: hset )

So, if you have some kind of object, like a dictionary, that binds the object (which you want to update when a notification arrives) to the redis hash. Then, when you receive a notification, you can take action on that specific object.

I hope you understand my answer! Sorry for my poor English!

If you need anything else, just let me know!

-1


source







All Articles