Ferricstore.KeyspaceNotifications (ferricstore v0.3.2)

Copy Markdown View Source

Emits Redis-compatible keyspace and keyevent notifications via PubSub.

When the notify-keyspace-events configuration value is set (non-empty), key mutation operations fire pub/sub messages on two channel families:

  • __keyspace@0__:<key> -- carries the event name as the message
  • __keyevent@0__:<event> -- carries the key name as the message

Configuration flags

The notify-keyspace-events config value is a string of flag characters:

  • K -- enable __keyspace@0__:<key> channel
  • E -- enable __keyevent@0__:<event> channel
  • g -- generic commands: DEL, EXPIRE, RENAME, PERSIST, COPY
  • $ -- string commands: SET, INCR, APPEND, GETSET
  • x -- expired events
  • A -- alias for all event types (g$x)

At least one of K or E must be present along with at least one event type flag for notifications to fire.

Examples

# Enable keyspace + keyevent for all event types:
Ferricstore.Config.set("notify-keyspace-events", "KEA")

# Enable only keyevent for string commands:
Ferricstore.Config.set("notify-keyspace-events", "E$")

Summary

Functions

Fires keyspace and/or keyevent notifications for a key mutation.

Determines whether a notification should fire for the given event based on the configured flags.

Functions

notify(key, event, flags \\ nil)

@spec notify(binary(), binary(), binary() | nil) :: :ok

Fires keyspace and/or keyevent notifications for a key mutation.

Reads the notify-keyspace-events config to determine which channels to publish to. Does nothing if notifications are disabled (empty config) or if the event type is not covered by the configured flags.

Parameters

  • key - the key that was mutated
  • event - the event name (e.g. "set", "del", "expire")
  • flags - (optional) override config flags; if nil, reads from Ferricstore.Config

Returns

:ok

should_notify?(event, flags)

@spec should_notify?(binary(), binary()) :: boolean()

Determines whether a notification should fire for the given event based on the configured flags.

Parameters

  • event - the event name
  • flags - the flag string from config

Returns

true if the event matches the configured flags, false otherwise.