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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_matrix/memory/global_memory.rb', line 97 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
53 54 55 56 |
# File 'lib/active_matrix/memory/global_memory.rb', line 53 def by_category(category) scope = ActiveMatrix::KnowledgeBase.active.by_category(category) AsyncQuery.async_pluck(scope, :key, :value).to_h end |
#delete(key) ⇒ Object
Delete a key
39 40 41 42 43 |
# File 'lib/active_matrix/memory/global_memory.rb', line 39 def delete(key) delete_through(key) do ActiveMatrix::KnowledgeBase.where(key: key).destroy_all.any? end end |
#exists?(key) ⇒ Boolean
Check if a key exists
30 31 32 33 34 35 36 |
# File 'lib/active_matrix/memory/global_memory.rb', line 30 def exists?(key) if @cache_enabled && Rails.cache.exist?(cache_key(key)) true else ActiveMatrix::KnowledgeBase.active.exists?(key: key) end end |
#get(key) ⇒ Object
Get a value from global memory
12 13 14 15 16 |
# File 'lib/active_matrix/memory/global_memory.rb', line 12 def get(key) fetch_with_cache(key) do ActiveMatrix::KnowledgeBase.get(key) end end |
#get_for_agent(key, agent) ⇒ Object
Get with permission check
71 72 73 74 75 |
# File 'lib/active_matrix/memory/global_memory.rb', line 71 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
121 122 123 124 125 126 127 128 129 |
# File 'lib/active_matrix/memory/global_memory.rb', line 121 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
46 47 48 49 50 |
# File 'lib/active_matrix/memory/global_memory.rb', line 46 def keys(category: nil) scope = ActiveMatrix::KnowledgeBase.active scope = scope.by_category(category) if category AsyncQuery.async_pluck(scope, :key) end |
#readable?(key, agent = nil) ⇒ Boolean
Check if readable by agent
59 60 61 62 |
# File 'lib/active_matrix/memory/global_memory.rb', line 59 def readable?(key, agent = nil) memory = ActiveMatrix::KnowledgeBase.find_by(key: key) memory&.readable_by?(agent) end |
#remember(key) ⇒ Object
Remember something globally
87 88 89 90 91 92 93 94 |
# File 'lib/active_matrix/memory/global_memory.rb', line 87 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
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_matrix/memory/global_memory.rb', line 19 def set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) write_through(key, value, expires_in: expires_in) do ActiveMatrix::KnowledgeBase.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
78 79 80 81 82 83 84 |
# File 'lib/active_matrix/memory/global_memory.rb', line 78 def set_for_agent(key, value, agent, **) # Allow creating new keys or updating writable ones memory = ActiveMatrix::KnowledgeBase.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
113 114 115 116 117 118 |
# File 'lib/active_matrix/memory/global_memory.rb', line 113 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
65 66 67 68 |
# File 'lib/active_matrix/memory/global_memory.rb', line 65 def writable?(key, agent = nil) memory = ActiveMatrix::KnowledgeBase.find_by(key: key) memory&.writable_by?(agent) end |