Class: GlobalMemory
- Defined in:
- lib/generators/active_matrix/install/templates/global_memory.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
61 62 63 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 61 def self.cleanup_expired! expired.destroy_all end |
.get(key) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 36 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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 49 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
25 26 27 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 25 def cache_key "global/#{key}" end |
#expired? ⇒ Boolean
12 13 14 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 12 def expired? expires_at.present? && expires_at <= Time.current end |
#readable_by?(agent) ⇒ Boolean
16 17 18 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 16 def readable_by?(agent) public_read || (agent.is_a?(MatrixAgent) && agent.admin?) end |
#writable_by?(agent) ⇒ Boolean
20 21 22 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 20 def writable_by?(agent) public_write || (agent.is_a?(MatrixAgent) && agent.admin?) end |
#write_to_cache ⇒ Object
29 30 31 32 33 34 |
# File 'lib/generators/active_matrix/install/templates/global_memory.rb', line 29 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 |