Class: ActiveMatrix::KnowledgeBase
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ActiveMatrix::KnowledgeBase
- Defined in:
- app/models/active_matrix/knowledge_base.rb
Class Method Summary collapse
- .cleanup_expired! ⇒ Object
- .get(key) ⇒ Object
- .set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) ⇒ Object
Instance Method Summary collapse
-
#cache_key ⇒ Object
Cache integration.
- #expired? ⇒ Boolean
- #readable_by?(agent) ⇒ Boolean
- #writable_by?(agent) ⇒ Boolean
- #write_to_cache ⇒ Object
Class Method Details
.cleanup_expired! ⇒ Object
90 91 92 |
# File 'app/models/active_matrix/knowledge_base.rb', line 90 def self.cleanup_expired! expired.destroy_all end |
.get(key) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/active_matrix/knowledge_base.rb', line 65 def self.get(key) # Try cache first cached = Rails.cache.read("global/#{key}") return cached if cached.present? # Fallback to database memory = find_by(key: key) return unless memory&.active? memory.write_to_cache memory.value end |
.set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/active_matrix/knowledge_base.rb', line 78 def self.set(key, value, category: nil, expires_in: nil, public_read: true, public_write: false) memory = find_or_initialize_by(key: key) memory.value = value memory.category = category memory.expires_at = expires_in.present? ? Time.current + expires_in : nil memory.public_read = public_read memory.public_write = public_write memory.save! memory.write_to_cache memory end |
Instance Method Details
#cache_key ⇒ Object
Cache integration
54 55 56 |
# File 'app/models/active_matrix/knowledge_base.rb', line 54 def cache_key "global/#{key}" end |
#expired? ⇒ Boolean
41 42 43 |
# File 'app/models/active_matrix/knowledge_base.rb', line 41 def expired? expires_at.present? && expires_at <= Time.current end |
#readable_by?(agent) ⇒ Boolean
45 46 47 |
# File 'app/models/active_matrix/knowledge_base.rb', line 45 def readable_by?(agent) public_read || (agent.is_a?(ActiveMatrix::Agent) && agent.admin?) end |
#writable_by?(agent) ⇒ Boolean
49 50 51 |
# File 'app/models/active_matrix/knowledge_base.rb', line 49 def writable_by?(agent) public_write || (agent.is_a?(ActiveMatrix::Agent) && agent.admin?) end |
#write_to_cache ⇒ Object
58 59 60 61 62 63 |
# File 'app/models/active_matrix/knowledge_base.rb', line 58 def write_to_cache return unless active? ttl = expires_at.present? ? expires_at - Time.current : nil Rails.cache.write(cache_key, value, expires_in: ttl) end |