Redis detect when a key's value is changed - redis

Without using keyspace notifications, pub/sub and streams, is there a way to detect/listen to a key's value being changed (or a key being added to a hashmap) in Redis?
Thanks

Related

Redis keyspace notifications detect only newly created keys and not replaced

I am trying to be notified in redis when new key is created and not being notified when the key is updated/replaced. Is there a way to set notifications this way or is there any other way to be notified when only new key was created and not existing one got replaced? I am using hset to set the keys.

Can you subscribe to a flushall notification in REDIS [duplicate]

The keyspace notifications have been essential for a recent web api I've been
developing.
We have redis setup in azure. The api mostly works, we use notifications to figure out if data on memory cache needs to be updated.
Right now, we want to handle notifying the flush event to clear local memory cache if redis database is flushed. But we can not get the flushdb event by Keyspace notification.
And the keyspace events is enable as "AKE". "AKE" string means all the events.
PS: we can get notification with 'set' event like '__keyevent#2__:set'
The subscription code is like below.
subscriber.Subscribe(
"*",
(channel, value) =>
{
// Some codes here
});
Just as the other answer mentioned, there's no such notification.
After all, Keyspace Notification is a notification for events on a single key. Each notification is associated with a key. For keyspace event, the key name is part of the channel name. For keyevent event, the key name is the message.
PUBLISH __keyspace#0__:key_name comamnd
PUBLISH __keyevent#0__:command key_name
Each command that sending a notification must have a key as argument. e.g. del key, set key val. However, the flushdb command has no key as argument. The command doesn't affect a single key. Instead, it removes all keys in the database. So there's no such notification for it. Otherwise, what do you expect from the channel? All keys that have been removed? It's not a good idea.
However, you can simulate an event for flushdb
set a special key, e.g. flushdb-event: set flushdb-event 0
subscribe on the corresponding channel: subscribe __keyspace#0__:flushdb-event
set the special key before you call flushdb: set flushdb-event 1
In this way, you can get the simulated flushdb notification.
According to Redis Documentation, there is no notification for Flushdb.
I think you have a couple of options.
You could call the INFO command periodically and check for a change in the number of flushdb or flushall calls. Here is the output from INFO that I am referring to...
Commandstats
cmdstat_flushall:calls=2,usec=112,usec_per_call=56.00
cmdstat_flushdb:calls=1,usec=110,usec_per_call=52.00
You could run the MONITOR command and parse the output. Note that this is not really a good option - it has heavy performance impact on the server side and would require a lot of processing on the client side.

redis pub sub only for a certain set of keys?

I have a use case in which I want to enable notification only for a certain set of keys, so that when those keys expire I can get a notification from redis.
I have followed this answer to implement this.
I have set parameter notify-keyspace-events to "Ex"
To accomplish this I am adding keys that I want notification for in DB-0 and the other keys in DB-1. But I am recieveing notification for both the DBs. Is there any way to just get notification from a particular DB?
According to redis documentation :
"Redis can notify Pub/Sub clients about events happening in the key space.
This feature is documented at http://redis.io/topics/notifications
For instance if keyspace events notification is enabled, and a client
performs a DEL operation on key "foo" stored in the Database 0, two
messages will be published via Pub/Sub:
PUBLISH keyspace#0:foo del
PUBLISH keyevent#0:del foo
"
But I am receiving notification from both DB-0 and DB-1.
PS : I know I can filter keys in my application, but I store too many expiring keys in redis and sending notification for all the expiring will increase load on my redis server.
I think you subscribed to a pattern that matches all DBs' notification message, e.g. PSUBSCRIBE __key*__:*.
In fact, you can specify the db index in the subscribed pattern: PSUBSCRIBE __keyspace#0__:* and PSUBSCRIBE __keyevent#0__:*. This way, you'll only received notification of DB0.

Update value after Expiration of the key in Redis

I am using Redis for cache some key value pairs.And I put TTL for that keys So it should be deleted after that timeout.But instead of deletion I want to reset the value when the key expires.Is that possible? If so can anyone send me how to do this? Or if not possible is there any other solution for this usecase.
This isn't possible using regular Redis TTL.
Alternatively, you'll need to implement that using some application-layer scheduled job to both implement a custom TTL and the custom action to reset the value.
Do you know Quartz?

list in redis all active/subscribed channels

I am new to redis
I am trying to list all the subscribed channels
is there a command to do that?
the other possibility is to store in a key the channels but I am sure there must be some other way
Although you didn't state it explicitly, I assume that you're interested in the channels that a specific client is subscribed to. Unfortunately, there isn't a Redis command for that and you'll need to keep track of it yourself (e.g. with a key as you had suggested).