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
64 65 66 |
# File 'app/models/active_matrix/knowledge_base.rb', line 64 def self.cleanup_expired! expired.destroy_all end |
.get(key) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/active_matrix/knowledge_base.rb', line 39 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
52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/active_matrix/knowledge_base.rb', line 52 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
28 29 30 |
# File 'app/models/active_matrix/knowledge_base.rb', line 28 def cache_key "global/#{key}" end |
#expired? ⇒ Boolean
15 16 17 |
# File 'app/models/active_matrix/knowledge_base.rb', line 15 def expired? expires_at.present? && expires_at <= Time.current end |
#readable_by?(agent) ⇒ Boolean
19 20 21 |
# File 'app/models/active_matrix/knowledge_base.rb', line 19 def readable_by?(agent) public_read || (agent.is_a?(ActiveMatrix::Agent) && agent.admin?) end |
#writable_by?(agent) ⇒ Boolean
23 24 25 |
# File 'app/models/active_matrix/knowledge_base.rb', line 23 def writable_by?(agent) public_write || (agent.is_a?(ActiveMatrix::Agent) && agent.admin?) end |
#write_to_cache ⇒ Object
32 33 34 35 36 37 |
# File 'app/models/active_matrix/knowledge_base.rb', line 32 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 |