BigW Consortium Gitlab

user_activities.rb 688 Bytes
Newer Older
1 2 3 4 5 6 7 8
module Gitlab
  class UserActivities
    include Enumerable

    KEY = 'users:activities'.freeze
    BATCH_SIZE = 500

    def self.record(key, time = Time.now)
9
      Gitlab::Redis::SharedState.with do |redis|
10 11 12 13 14
        redis.hset(KEY, key, time.to_i)
      end
    end

    def delete(*keys)
15
      Gitlab::Redis::SharedState.with do |redis|
16 17 18 19 20 21 22 23
        redis.hdel(KEY, keys)
      end
    end

    def each
      cursor = 0
      loop do
        cursor, pairs =
24
          Gitlab::Redis::SharedState.with do |redis|
25 26 27 28 29 30 31 32 33 34
            redis.hscan(KEY, cursor, count: BATCH_SIZE)
          end

        Hash[pairs].each { |pair| yield pair }

        break if cursor == '0'
      end
    end
  end
end