Class: ActiveMatrix::Memory::AgentMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/active_matrix/memory/agent_memory.rb

Overview

Per-agent private memory storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_multi, #set_multi

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(agent) ⇒ AgentMemory

Returns a new instance of AgentMemory.



9
10
11
12
# File 'lib/active_matrix/memory/agent_memory.rb', line 9

def initialize(agent)
  super()
  @agent = agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



7
8
9
# File 'lib/active_matrix/memory/agent_memory.rb', line 7

def agent
  @agent
end

Instance Method Details

#allObject

Get all memory as hash



64
65
66
67
68
# File 'lib/active_matrix/memory/agent_memory.rb', line 64

def all
  return {} unless defined?(::AgentMemory)

  @agent.agent_memories.active.pluck(:key, :value).to_h
end

#clear!Object

Clear all agent memory



71
72
73
74
75
76
77
78
79
80
# File 'lib/active_matrix/memory/agent_memory.rb', line 71

def clear!
  return false unless defined?(::AgentMemory)

  @agent.agent_memories.destroy_all

  # Clear cache entries
  keys.each { |key| Rails.cache.delete(cache_key(key)) } if @cache_enabled

  true
end

#decrement(key, amount = 1) ⇒ Object

Decrement a counter



101
102
103
# File 'lib/active_matrix/memory/agent_memory.rb', line 101

def decrement(key, amount = 1)
  increment(key, -amount)
end

#delete(key) ⇒ Object

Delete a key



48
49
50
51
52
53
54
# File 'lib/active_matrix/memory/agent_memory.rb', line 48

def delete(key)
  return false unless defined?(::AgentMemory)

  delete_through(key) do
    @agent.agent_memories.where(key: key).destroy_all.any?
  end
end

#exists?(key) ⇒ Boolean

Check if a key exists

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/active_matrix/memory/agent_memory.rb', line 37

def exists?(key)
  return false unless defined?(::AgentMemory)

  if @cache_enabled && Rails.cache.exist?(cache_key(key))
    true
  else
    @agent.agent_memories.active.exists?(key: key)
  end
end

#get(key) ⇒ Object

Get a value from agent memory



15
16
17
18
19
20
21
22
# File 'lib/active_matrix/memory/agent_memory.rb', line 15

def get(key)
  fetch_with_cache(key) do
    return nil unless defined?(::AgentMemory)

    memory = @agent.agent_memories.active.find_by(key: key)
    memory&.value
  end
end

#increment(key, amount = 1) ⇒ Object

Increment a counter



93
94
95
96
97
98
# File 'lib/active_matrix/memory/agent_memory.rb', line 93

def increment(key, amount = 1)
  current = get(key) || 0
  new_value = current + amount
  set(key, new_value)
  new_value
end

#keysObject

Get all keys



57
58
59
60
61
# File 'lib/active_matrix/memory/agent_memory.rb', line 57

def keys
  return [] unless defined?(::AgentMemory)

  @agent.agent_memories.active.pluck(:key)
end

#pull(key, value) ⇒ Object

Remove from a list



114
115
116
117
118
119
# File 'lib/active_matrix/memory/agent_memory.rb', line 114

def pull(key, value)
  list = get(key) || []
  list.delete(value)
  set(key, list)
  list
end

#push(key, value) ⇒ Object

Add to a list



106
107
108
109
110
111
# File 'lib/active_matrix/memory/agent_memory.rb', line 106

def push(key, value)
  list = get(key) || []
  list << value
  set(key, list)
  list
end

#remember(key, expires_in: nil) ⇒ Object

Remember something with optional TTL



83
84
85
86
87
88
89
90
# File 'lib/active_matrix/memory/agent_memory.rb', line 83

def remember(key, expires_in: nil)
  value = get(key)
  return value if value.present?

  value = yield
  set(key, value, expires_in: expires_in) if value.present?
  value
end

#set(key, value, expires_in: nil) ⇒ Object

Set a value in agent memory



25
26
27
28
29
30
31
32
33
34
# File 'lib/active_matrix/memory/agent_memory.rb', line 25

def set(key, value, expires_in: nil)
  return false unless defined?(::AgentMemory)

  write_through(key, value, expires_in: expires_in) do
    memory = @agent.agent_memories.find_or_initialize_by(key: key)
    memory.value = value
    memory.expires_at = expires_in.present? ? Time.current + expires_in : nil
    memory.save!
  end
end