I want to be able to run a callback when any change is made in my redis collection. The callback would take the key and value as inputs. Is something like this possible?
Thanks?
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.
For a clustered cache, you would see shards instead. From there, you can expand a cache resource to view all the keys inside it. By clicking on a Redis key name, all its contents will open in a new editor tab. With a collection type Redis key, clicking on it will reveal the individual elements under the key name.
There are multiple ways to get metrics data from a Redis cluster. One is to run the info command in a Redis shell (which you can open by running redis-cli in your terminal). Graphical management frontends for Redis, such as Redsmin and Redis Commander, also provide some monitoring functionality.
UPDATE: Whomever is reading this - these are ancient answers, disregard them all. What you need is Redis Keyspace Notifications which have been around for ages. See https://redis.io/topics/notifications
Two options:
Use MONITOR
command - it traces every command that gets to Redis and you can analyze and see when your collection is being touched.
If you "own" the code that writes to the collection, signal your other code (your callback). You can use Redis Pub/Sub channel for this.
EDIT Redis is actually going to implement this feature in version 2.8. See Antirez's blog post on this: Redis keyspace changes notification system.
You can also connect to the Redis server like a follower using the sync command. See How Redis Replication Works? for a quick introduction.
The output of sync command has two phases. In the first phase, the server returns the database dump.rdb file. Once the file is sent, it starts sending commands in the Redis protocol, which is also the AOF format.
Here is the high level picture of what you can do :
SYNC
commandIt seems a lot of work, but you should be able to hack this pretty easily. And it would make a good open source library too!
EDIT : Sync v/s Monitor
Monitor
is a debugging command. The response format can (and has) change(d) over time. Sync
is used for Master -> Slave replication, and so will be better supportedMonitor
will emit all commands, including read-only commands. Sync
will only get you commands that modify data.Monitor
will log individual commands that are executed within a lua script. Sync
will only transfer the entire lua script, so you will have to parse the script yourself. This is indeed a deal breaker for sync
.Monitor
will log commands that did not succeed, Sync
will only log commands that modify data. For example, the command del non-existing-key
will be logged by monitor but won't show up when you run sync.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With