Redis keyspace notification (Keyspace Notification)

Starting from Redis 2.8.0, Redis has added a publish/subscribe mode and keyspace notification function. The key space message reminder provides the ability to allow the client to obtain Redis data changes by subscribing to a specified channel.

It should be noted that the keyspace message reminder is not reliable, and it will not confirm whether the subscriber has received the message. For example, a subscribed client is temporarily disconnected, and events that occurred during the period until the connection is restored will not be available again.

Configuration

By default, Redis does not enable the keyspace message reminder function. In order to enable this function, it needs to notify-keyspace-eventsbe set through configuration, for example:

redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) ""
redis> CONFIG SET notify-keyspace-events KEA
OK
redis> CONFIG GET notify-keyspace-events
1) "notify-keyspace-events"
2) "AKE"

In the above example, it will be notify-keyspace-eventsconfigured to KEArepresent all events except misses. Among them, Kand Erepresents two types of events- Keyspaceand Keyevent. KeyspaceRepresents messages related to event names, such as subscribing to an operation event performed on a specified key; Keyeventrepresents messages related to key names, such as subscribing to key names related to key expiration events.

For more notify-keyspace-eventsconfiguration, please refer to the following description:

  • K: Keyspace event, will be __keyspace@<db>__prefixed with the event
  • E: Keyevent event, will be __keyevent@<db>__prefixed with the event
  • g: Non-specific type of general commands, for example DEL, EXPIRE, RENAMElike
  • $: String commands, for example SET, INCRetc.
  • l: List commands, for example LPUSH, LPOPetc.
  • s: Set commands, for example SADD, SREMetc.
  • h: Hash table commands, for example HSET, HINCRBYetc.
  • z: Ordered collection commands, for example ZSET, ZREMetc.
  • t: Stream commands, for example XADD, XDELetc.
  • x: Expiration event (generated every time a key expires)
  • e: Elimination event (generated when each key is eliminated)
  • m: Missing event (generated when accessing a non-existent key)
  • A: The configured g$lshztxealias, but does not include the miss eventm

Subscribe to specified events

After completing the configuration, you can SUBSCRIBEsubscribe to one or more specified events by ordering to subscribe to the specified channel. For example, subscribing __keyevent@0__:expiredto the key expiration event in database 0 is realized by subscribing. Example 1: Subscription key expiration event .

The format of the subscribed channel is composed of three parts: __<type>@<db>__:<event>event type ( keyspaceor keyevent), database (for example, database 0), and event (for example expired). For the name of the corresponding event, please refer to the command event chapter below .

In addition, you can also PSUBSCRIBEsubscribe to one or more channels matched by compound regular expressions through commands. For example, by subscribing __key*@*__:*to all events in all databases in Redis.

Command event

Redis provides different events for many commands. In this article, we will select some of these commands and their corresponding events to introduce:

  • DEL: An delevent occurs when a key is deleted
  • EXPIRE, PEXPIRE, EXPIREATAnd PEXPIREAT: When setting a positive number or expiration timestamp future time, then generate expireevents, otherwise generate delan event (will be immediately deleted)
  • SETAnd similar SETEX, SETNX, GETSET: generating setevent, the use of SETEXit will produce expireevents
  • MSET: Will generate an setevent for each key
  • LPUSH, LPUSHXAnd RPUSH, RPUSHX: According to generate the direction of insertion lpushor rpushevents
  • RPOP, LPOP: Generate rpopand lpopevent separately, if the last element in the list is removed, the delevent will be generated at the same time
  • LSET: Generate an lsetevent
  • LREM: Generate an lremevent, also if the removed element is the last element in the list, an delevent will be generated at the same time
  • HSET, HSETNXAs well as HMSET: a generating hsetevent
  • HDEL: Generate a hdelevent, and in the removal of Hou Haxi table is empty case generated delevent
  • SADD: Generate an saddevent
  • SREM: Generate a sremevent, and collection case is empty after removing generated delevent
  • SMOVE: The original key generation sremevent and to generate a target key saddevents
  • SINTERSTORE, , SUNIONSTORE: SDIFFSTOREGenerate sinterstore, sunionstoreas well as sdiffstoreevent and result in the case of an empty set and the target key exists, it will generate delan event
  • ZADD: No matter how many elements are added, only one zaddevent is generated
  • ZREM: Whether to remove several elements only generate a zremevent, generated when the removal of an ordered set is empty delevent
  • XADD: Generating xaddevent, the use of MAXLENsub-command may simultaneously generate xtrimevent
  • XDEL: Generate an xdelevent
  • PERSIST: If the expiration event associated with the corresponding key is successfully removed, an persistevent is generated
  • Generate an expiredevent when the key expires
  • maxmemoryAn evictedevent is generated when the key is eliminated after reaching the set memory value
  • ……

For more command related events, please refer to keyspace notification related documents

Example

Subscription key expiration event

redis1> SUBSCRIBE __keyevent@0__:expired
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1
# redis2> SETEX greeting 1 "hello world"
# 等待1秒后:
1) "message"
2) "__keyevent@0__:expired"
3) "greeting"

Subscribe to all events

redis1> PSUBSCRIBE __key*@*__:*
1) "psubscribe"
2) "__key*@*__:*"
3) (integer) 1
# redis2> SET greeting "hello world"
1) "pmessage"
2) "__key*@*__:*"
3) "__keyspace@0__:greeting"
4) "set"
1) "pmessage"
2) "__key*@*__:*"
3) "__keyevent@0__:set"
4) "greeting"

Reference

Guess you like

Origin blog.csdn.net/ghosind/article/details/114242223