Class: Sc4ry::Store
- Inherits:
-
Object
- Object
- Sc4ry::Store
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/sc4ry/store.rb
Overview
must be accessed by Circuits.store
Sc4ry::Store class Store Class Provider/manager singleton Forwarder on Backends::Memory or Backends::Redis
Constant Summary collapse
- @@current =
:memory
- @@backends =
{ memory: { class: Sc4ry::Backends::Memory, config: {} }, redis: { class: Sc4ry::Backends::Redis, config: { host: 'localhost', port: 6379, db: 1 } } }
Instance Attribute Summary collapse
-
#be ⇒ Object
readonly
accessor on current backend (default :memory).
Instance Method Summary collapse
-
#change_backend(name:) ⇒ Symbol
change the current backend.
-
#config_backend(name:, config:) ⇒ Object
change the specified backend config.
-
#current ⇒ Object
return the current backend.
-
#delete_backend(name:) ⇒ Boolean
delete the specified backend reference.
-
#get_config(backend:) ⇒ Hash
return the config of a specific backend.
-
#initialize ⇒ Store
constructor
constructor pointing on :memory backend.
-
#list_backend ⇒ Array
list backend available.
-
#register_backend(name:, backend_class:, config: {}) ⇒ Symbol
register a new backend.
Constructor Details
#initialize ⇒ Store
constructor pointing on :memory backend
24 25 26 |
# File 'lib/sc4ry/store.rb', line 24 def initialize change_backend name: @@current end |
Instance Attribute Details
#be ⇒ Object (readonly)
accessor on current backend (default :memory)
19 20 21 |
# File 'lib/sc4ry/store.rb', line 19 def be @be end |
Instance Method Details
#change_backend(name:) ⇒ Symbol
if changing form :memory to :redis => all values and result are lost and circuits will be lost
if changing to :redis, get all the define circuits with values and status (ideal)
for distributed worker/instance/runner/services
change the current backend
66 67 68 69 70 71 72 |
# File 'lib/sc4ry/store.rb', line 66 def change_backend(name:) raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name @@current = name @be = @@backends[@@current][:class].new(@@backends[@@current][:config]) name end |
#config_backend(name:, config:) ⇒ Object
change the specified backend config
104 105 106 107 108 109 |
# File 'lib/sc4ry/store.rb', line 104 def config_backend(name:, config:) raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name raise Sc4ry::Exceptions::Sc4ryBackendError, 'backend :memory not need config' if name == :memory @@backends[name][:config] = config end |
#current ⇒ Object
return the current backend
33 34 35 |
# File 'lib/sc4ry/store.rb', line 33 def current @@current end |
#delete_backend(name:) ⇒ Boolean
delete the specified backend reference
91 92 93 94 95 96 97 98 |
# File 'lib/sc4ry/store.rb', line 91 def delete_backend(name:) forbidden_mes = 'Delete forbidden for backend in [:redis,:memory]' notfound_mes = "backend #{name} not found" raise Sc4ry::Exceptions::Sc4ryBackendError, forbidden_mes if %i[memory redis].include? name raise Sc4ry::Exceptions::Sc4ryBackendError, notfound_mes unless @@backends.include? name @@backends.delete(name) end |
#get_config(backend:) ⇒ Hash
return the config of a specific backend
44 45 46 47 48 |
# File 'lib/sc4ry/store.rb', line 44 def get_config(backend:) raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{backend} not found" unless @@backends.include? backend @@backends[backend][:config] end |
#list_backend ⇒ Array
list backend available
55 56 57 |
# File 'lib/sc4ry/store.rb', line 55 def list_backend @@backends.keys end |
#register_backend(name:, backend_class:, config: {}) ⇒ Symbol
register a new backend
80 81 82 83 84 85 |
# File 'lib/sc4ry/store.rb', line 80 def register_backend(name:, backend_class:, config: {}) raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} already exist" if @@backends.include? name @@backends[name] = { config: config, class: backend_class } name end |