Class: Stoplight::Infrastructure::Redis::Storage::KeySpace

Inherits:
Data
  • Object
show all
Defined in:
lib/stoplight/infrastructure/redis/storage/key_space.rb,
lib/stoplight/infrastructure/redis/storage/key_space.rb

Overview

Immutable key namespace for a light within a system.

Produces keys following entity-first structure:

stoplight:v6:{system_id}:{light_id}:locks:recovery
stoplight:v6:{system_id}:{light_id}:metrics:successes
stoplight:v6:{system_id}:{light_id}:state

Identifiers are derived from SHA-256 and truncated to 12 characters. Collisions are extremely unlikely at expected system scale.

Examples:

key_space = KeySpace.build(system_name: "payments", light_name: "stripe-api")
key_space.key(:locks, :recovery)  #=> "stoplight:v6:df384ae97c77:cfe6861fa39e:locks:recovery"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#light_idObject (readonly)

Returns the value of attribute light_id

Returns:

  • (Object)

    the current value of light_id



23
24
25
# File 'lib/stoplight/infrastructure/redis/storage/key_space.rb', line 23

def light_id
  @light_id
end

#system_idObject (readonly)

Returns the value of attribute system_id

Returns:

  • (Object)

    the current value of system_id



23
24
25
# File 'lib/stoplight/infrastructure/redis/storage/key_space.rb', line 23

def system_id
  @system_id
end

Class Method Details

.build(system_name:, light_name:) ⇒ Object



33
34
35
36
# File 'lib/stoplight/infrastructure/redis/storage/key_space.rb', line 33

def build(system_name:, light_name:) = new(
  system_id: hash_name(system_name),
  light_id: hash_name(light_name)
)

.hash_name(name) ⇒ Object

Generates a truncated SHA256 hash for use in Redis keys.



39
# File 'lib/stoplight/infrastructure/redis/storage/key_space.rb', line 39

def hash_name(name) = Digest::SHA256.hexdigest(name.to_s)[0, 12] #: String

Instance Method Details

#key(*pieces) ⇒ Object

Builds a Redis key within this namespace.

Parameters:

  • pieces

    Key segments to append

Returns:

  • Full Redis key



46
# File 'lib/stoplight/infrastructure/redis/storage/key_space.rb', line 46

def key(*pieces) = [:stoplight, :v6, system_id, "{#{light_id}}", *pieces].join(":")