123456789_123456789_123456789_123456789_123456789_

Class: Redis::KeyspaceNotifications::Notification

Relationships & Source Files
Inherits: Object
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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil) ⇒ 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>)

    affected subkeys

  • pattern (String, nil)

    matched psubscribe pattern

[ GitHub ]

  
# 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

#channelSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#dbSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#eventSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#familySymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#keySymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#patternSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#payloadSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

#subkey_family?Boolean (readonly)

Returns:

  • (Boolean)

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

[ GitHub ]

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

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

#subkeysSymbol, ... (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

[ GitHub ]

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

attr_reader :family, :db, :event, :key, :subkeys, :channel, :payload, :pattern

Instance Method Details

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

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (Boolean)

    whether other is a Notification with equal fields

[ GitHub ]

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

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

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/redis/keyspace_notifications/notification.rb', line 81

alias eql? ==

#frozen_copy(value) (private)

[ GitHub ]

  
# File 'lib/redis/keyspace_notifications/notification.rb', line 90

def frozen_copy(value)
  value.nil? || value.frozen? ? value : value.dup.freeze
end

#hashInteger

Returns:

  • (Integer)

    a hash derived from all fields

[ GitHub ]

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

def hash
  to_h.hash
end

#subkeyString?

Convenience accessor for single-subkey families (subkeyspaceitem).

Returns:

  • (String, nil)

    the first subkey, or nil when there are none

[ GitHub ]

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

def subkey
  @subkeys.first
end

#to_hHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})

    all fields as a hash

[ GitHub ]

  
# 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