Class: Legion::Extensions::MindGrowth::Helpers::ProposalPersistence

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb

Overview

Cache-backed persistence for proposals and governance votes. Best-effort: degrades to in-memory-only when cache is unavailable.

Constant Summary collapse

PROPOSAL_TTL =

7 days

604_800
VOTES_KEY_SUFFIX =
':votes'
INDEX_KEY_SUFFIX =
':index'

Instance Method Summary collapse

Constructor Details

#initialize(namespace: 'legion_mind_growth') ⇒ ProposalPersistence

Returns a new instance of ProposalPersistence.



14
15
16
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 14

def initialize(namespace: 'legion_mind_growth')
  @namespace = namespace
end

Instance Method Details

#delete_all_proposalsObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 68

def delete_all_proposals
  return false unless cache_available?

  ids = load_index
  ids.each { |id| Legion::Cache.delete_sync(proposal_key(id)) }
  Legion::Cache.delete_sync(index_key)
  true
rescue StandardError => e
  log.error "[proposal_persistence] delete_all_proposals failed: #{e.message}"
  false
end

#delete_proposal(id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 42

def delete_proposal(id)
  return false unless cache_available?

  Legion::Cache.delete_sync(proposal_key(id))
  update_index(id, :remove)
  true
rescue StandardError => e
  log.error "[proposal_persistence] delete_proposal failed for #{id}: #{e.message}"
  false
end

#load_all_proposalsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 53

def load_all_proposals
  return {} unless cache_available?

  ids = load_index
  return {} if ids.empty?

  ids.each_with_object({}) do |id, result|
    p = load_proposal(id)
    result[id] = p if p
  end
rescue StandardError => e
  log.error "[proposal_persistence] load_all_proposals failed: #{e.message}"
  {}
end

#load_proposal(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 30

def load_proposal(id)
  return nil unless cache_available?

  raw = Legion::Cache.get(proposal_key(id)) # rubocop:disable Legion/HelperMigration/DirectCache
  return nil unless raw

  deserialize(raw)
rescue StandardError => e
  log.error "[proposal_persistence] load_proposal failed for #{id}: #{e.message}"
  nil
end

#load_votesObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 90

def load_votes
  return {} unless cache_available?

  raw = Legion::Cache.get(votes_key) # rubocop:disable Legion/HelperMigration/DirectCache
  return {} unless raw

  result = deserialize(raw)
  result.is_a?(Hash) ? result : {}
rescue StandardError => e
  log.error "[proposal_persistence] load_votes failed: #{e.message}"
  {}
end

#save_proposal(proposal_hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 18

def save_proposal(proposal_hash)
  return false unless cache_available?

  key = proposal_key(proposal_hash[:id])
  Legion::Cache.set_sync(key, serialize(proposal_hash), ttl: PROPOSAL_TTL)
  update_index(proposal_hash[:id], :add)
  true
rescue StandardError => e
  log.error "[proposal_persistence] save_proposal failed for #{proposal_hash[:id]}: #{e.message}"
  false
end

#save_votes(votes_hash) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/mind_growth/helpers/proposal_persistence.rb', line 80

def save_votes(votes_hash)
  return false unless cache_available?

  Legion::Cache.set_sync(votes_key, serialize(votes_hash), ttl: PROPOSAL_TTL)
  true
rescue StandardError => e
  log.error "[proposal_persistence] save_votes failed: #{e.message}"
  false
end