Class: SpreeCmCommissioner::VoteCounters::RebuildFromDb

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/vote_counters/rebuild_from_db.rb

Constant Summary

Constants inherited from Base

Base::REDIS_KEY_TTL

Instance Attribute Summary

Attributes inherited from Base

#voting_session_id

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SpreeCmCommissioner::VoteCounters::Base

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/spree_cm_commissioner/vote_counters/rebuild_from_db.rb', line 11

def call
  db_counts = SpreeCmCommissioner::Vote.for_session(@voting_session_id)
                                       .group(:contestant_id)
                                       .sum(:quantity)
  return if db_counts.empty?

  keys   = db_counts.keys.map { |id| vote_key(id) }
  values = db_counts.values

  with_redis do |redis|
    redis.eval(rebuild_script, keys: keys, argv: values + [REDIS_KEY_TTL])
  end
end

#rebuild_scriptObject

Sets all contestant keys atomically in a single Lua call. KEYS: [key1, key2, …] — one per contestant ARGV: [count1, count2, …, ttl] — counts aligned to KEYS, TTL as last arg

Key format: “vote:session:<voting_session_id>:contestant:<contestant_id>” Example: “vote:session:42:contestant:7”



31
32
33
34
35
36
37
38
# File 'app/services/spree_cm_commissioner/vote_counters/rebuild_from_db.rb', line 31

def rebuild_script
  <<~LUA
    local ttl = tonumber(ARGV[#ARGV])
    for i, key in ipairs(KEYS) do
      redis.call('SET', key, ARGV[i], 'EX', ttl)
    end
  LUA
end