Class: Redis::KeyspaceNotifications::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/keyspace_notifications/notification.rb

Overview

A parsed keyspace, keyevent or subkey notification.

Immutable value object produced by Parser.parse. key and the subkeys elements are BINARY (ASCII-8BIT) encoded strings, because Redis keys and subkeys may contain arbitrary bytes; call force_encoding on them if your application guarantees UTF-8 keys.

Constant Summary collapse

FAMILIES =
%i[keyspace keyevent subkeyspace subkeyevent subkeyspaceitem subkeyspaceevent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil) ⇒ Notification

Returns a new instance of Notification.

Parameters:

  • family (Symbol) —

    one of FAMILIES

  • db (Integer, String) —

    database index or "*"

  • event (String) —

    event name

  • key (String) —

    affected key

  • channel (String) —

    raw channel

  • payload (String) —

    raw payload

  • subkeys (Array<String>) (defaults to: []) —

    affected subkeys

  • pattern (String, nil) (defaults to: nil) —

    matched psubscribe pattern



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redis/keyspace_notifications/notification.rb', line 35

def initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil)
  @family = family
  @db = frozen_copy(db) # Integers pass through; the "*" String form gets copied
  @event = frozen_copy(event)
  @key = frozen_copy(key)
  @subkeys = subkeys.map { |subkey| frozen_copy(subkey) }.freeze
  @channel = frozen_copy(channel)
  @payload = frozen_copy(payload)
  @pattern = frozen_copy(pattern)
  # Deep-frozen: the value object implements #hash / #eql?, so no field may be
  # mutable after construction (a mutated Hash key becomes unreachable).
  freeze
end

Instance Attribute Details

#channel ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def channel
  @channel
end

#db ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def db
  @db
end

#event ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def event
  @event
end

#family ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def family
  @family
end

#key ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def key
  @key
end

#pattern ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def pattern
  @pattern
end

#payload ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def payload
  @payload
end

#subkeys ⇒ Symbol, ... (readonly)

Returns:

  • (Symbol) —

    the channel family, one of FAMILIES

  • (Integer, String) —

    the database the event happened in, or "*" when the channel itself carried a database wildcard

  • (String) —

    the event name (e.g. "set", "expired", "hdel"). Opaque: new server events flow through without a library update

  • (String) —

    the affected key (BINARY encoded)

  • (Array<String>) —

    the affected subkeys, in server order with duplicates preserved; empty for keyspace/keyevent, exactly one for subkeyspaceitem

  • (String) —

    the raw pub/sub channel the notification arrived on

  • (String) —

    the raw pub/sub message payload

  • (String, nil) —

    the psubscribe pattern that matched, when delivered via pmessage



25
26
27
# File 'lib/redis/keyspace_notifications/notification.rb', line 25

def subkeys
  @subkeys
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns whether other is a Notification with equal fields.

Parameters:

  • other (Object) —

    the object to compare against

Returns:

  • (Boolean) —

    whether other is a Notification with equal fields



78
79
80
# File 'lib/redis/keyspace_notifications/notification.rb', line 78

def ==(other)
  other.is_a?(Notification) && to_h == other.to_h
end

#hash ⇒ Integer

Returns a hash derived from all fields.

Returns:

  • (Integer) —

    a hash derived from all fields



84
85
86
# File 'lib/redis/keyspace_notifications/notification.rb', line 84

def hash
  to_h.hash
end

#subkey ⇒ String?

Convenience accessor for single-subkey families (subkeyspaceitem).

Returns:

  • (String, nil) —

    the first subkey, or nil when there are none



52
53
54
# File 'lib/redis/keyspace_notifications/notification.rb', line 52

def subkey
  @subkeys.first
end

#subkey_family? ⇒ Boolean

Returns whether this notification belongs to one of the Redis 8.8 subkey channel families (and therefore may carry subkeys).

Returns:

  • (Boolean) —

    whether this notification belongs to one of the Redis 8.8 subkey channel families (and therefore may carry subkeys)



58
59
60
# File 'lib/redis/keyspace_notifications/notification.rb', line 58

def subkey_family?
  @family != :keyspace && @family != :keyevent
end

#to_h ⇒ Hash{Symbol => Object}

Returns all fields as a hash.

Returns:

  • (Hash{Symbol => Object}) —

    all fields as a hash



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/redis/keyspace_notifications/notification.rb', line 63

def to_h
  {
    family: @family,
    db: @db,
    event: @event,
    key: @key,
    subkeys: @subkeys,
    channel: @channel,
    payload: @payload,
    pattern: @pattern
  }
end