Class: ActiveMatrix::Memory::GlobalMemory
- Includes:
- Singleton
- Defined in:
- lib/active_matrix/memory/global_memory.rb
Overview
Global memory storage accessible to all agents
Instance Method Summary collapse
-
#broadcast(key, value, expires_in: 5.minutes) ⇒ Object
Broadcast a value to all agents.
-
#by_category(category) ⇒ Object
Get all values in a category.
-
#delete(key) ⇒ Object
Delete a key.
-
#exists?(key) ⇒ Boolean
Check if a key exists.
-
#get(key) ⇒ Object
Get a value from global memory.
-
#get_for_agent(key, agent) ⇒ Object
Get with permission check.
-
#get_shared(key, agent) ⇒ Object
Get shared data if allowed.
-
#keys(category: nil) ⇒ Object
Get all keys in a category.
-
#readable?(key, agent = nil) ⇒ Boolean
Check if readable by agent.
-
#remember(key) ⇒ Object
Remember something globally.
-
#set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) ⇒ Object
Set a value in global memory.
-
#set_for_agent(key, value, agent) ⇒ Object
Set with permission check.
-
#share(key, value, agent_names, expires_in: nil) ⇒ Object
Share data between specific agents.
-
#writable?(key, agent = nil) ⇒ Boolean
Check if writable by agent.
Methods inherited from Base
#clear!, #get_multi, #initialize, #set_multi
Methods included from Logging
Constructor Details
This class inherits a constructor from ActiveMatrix::Memory::Base
Instance Method Details
#broadcast(key, value, expires_in: 5.minutes) ⇒ Object
Broadcast a value to all agents
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/active_matrix/memory/global_memory.rb', line 112 def broadcast(key, value, expires_in: 5.minutes) set(key, value, category: 'broadcast', expires_in: expires_in, public_read: true) # Notify all agents if event router is available if defined?(EventRouter) EventRouter.instance.broadcast_event({ type: 'global_memory.broadcast', key: key, value: value }) end true end |
#by_category(category) ⇒ Object
Get all values in a category
63 64 65 66 67 |
# File 'lib/active_matrix/memory/global_memory.rb', line 63 def by_category(category) return {} unless defined?(::GlobalMemory) ::GlobalMemory.active.by_category(category).pluck(:key, :value).to_h end |
#delete(key) ⇒ Object
Delete a key
45 46 47 48 49 50 51 |
# File 'lib/active_matrix/memory/global_memory.rb', line 45 def delete(key) return false unless defined?(::GlobalMemory) delete_through(key) do ::GlobalMemory.where(key: key).destroy_all.any? end end |
#exists?(key) ⇒ Boolean
Check if a key exists
34 35 36 37 38 39 40 41 42 |
# File 'lib/active_matrix/memory/global_memory.rb', line 34 def exists?(key) return false unless defined?(::GlobalMemory) if @cache_enabled && Rails.cache.exist?(cache_key(key)) true else ::GlobalMemory.active.exists?(key: key) end end |
#get(key) ⇒ Object
Get a value from global memory
12 13 14 15 16 17 18 |
# File 'lib/active_matrix/memory/global_memory.rb', line 12 def get(key) fetch_with_cache(key) do return nil unless defined?(::GlobalMemory) ::GlobalMemory.get(key) end end |
#get_for_agent(key, agent) ⇒ Object
Get with permission check
86 87 88 89 90 |
# File 'lib/active_matrix/memory/global_memory.rb', line 86 def get_for_agent(key, agent) return nil unless readable?(key, agent) get(key) end |
#get_shared(key, agent) ⇒ Object
Get shared data if allowed
136 137 138 139 140 141 142 143 144 |
# File 'lib/active_matrix/memory/global_memory.rb', line 136 def get_shared(key, agent) data = get(key) return nil unless data.is_a?(Hash) && data['allowed_agents'] allowed = data['allowed_agents'] return unless allowed.include?(agent.name) || allowed.include?('*') data['value'] end |
#keys(category: nil) ⇒ Object
Get all keys in a category
54 55 56 57 58 59 60 |
# File 'lib/active_matrix/memory/global_memory.rb', line 54 def keys(category: nil) return [] unless defined?(::GlobalMemory) scope = ::GlobalMemory.active scope = scope.by_category(category) if category scope.pluck(:key) end |
#readable?(key, agent = nil) ⇒ Boolean
Check if readable by agent
70 71 72 73 74 75 |
# File 'lib/active_matrix/memory/global_memory.rb', line 70 def readable?(key, agent = nil) return false unless defined?(::GlobalMemory) memory = ::GlobalMemory.find_by(key: key) memory&.readable_by?(agent) end |
#remember(key) ⇒ Object
Remember something globally
102 103 104 105 106 107 108 109 |
# File 'lib/active_matrix/memory/global_memory.rb', line 102 def remember(key, **) value = get(key) return value if value.present? value = yield set(key, value, **) if value.present? value end |
#set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) ⇒ Object
Set a value in global memory
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_matrix/memory/global_memory.rb', line 21 def set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) return false unless defined?(::GlobalMemory) write_through(key, value, expires_in: expires_in) do ::GlobalMemory.set(key, value, category: category, expires_in: expires_in, public_read: public_read, public_write: public_write) end end |
#set_for_agent(key, value, agent) ⇒ Object
Set with permission check
93 94 95 96 97 98 99 |
# File 'lib/active_matrix/memory/global_memory.rb', line 93 def set_for_agent(key, value, agent, **) # Allow creating new keys or updating writable ones memory = ::GlobalMemory.find_by(key: key) return false if memory && !memory.writable_by?(agent) set(key, value, **) end |
#share(key, value, agent_names, expires_in: nil) ⇒ Object
Share data between specific agents
128 129 130 131 132 133 |
# File 'lib/active_matrix/memory/global_memory.rb', line 128 def share(key, value, agent_names, expires_in: nil) set(key, { value: value, allowed_agents: agent_names }, category: 'shared', expires_in: expires_in, public_read: false) end |
#writable?(key, agent = nil) ⇒ Boolean
Check if writable by agent
78 79 80 81 82 83 |
# File 'lib/active_matrix/memory/global_memory.rb', line 78 def writable?(key, agent = nil) return false unless defined?(::GlobalMemory) memory = ::GlobalMemory.find_by(key: key) memory&.writable_by?(agent) end |