Sha256: 6a41f98d0811c4cbe0bc27b9c65ad46ead4c082ebeeb6f949ba16cc8e826141d

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require 'redis'
require_relative '../mixins/packer'

module Trifle
  module Stats
    module Driver
      class Redis
        include Mixins::Packer
        attr_accessor :client, :prefix, :separator

        def initialize(client = ::Redis.current, prefix: 'trfl')
          @client = client
          @prefix = prefix
          @separator = '::'
        end

        def inc(keys:, **values)
          keys.map do |key|
            pkey = ([prefix] + key).join(separator)

            self.class.pack(hash: values).each do |k, c|
              client.hincrby(pkey, k, c)
            end
          end
        end

        def set(keys:, **values)
          keys.map do |key|
            pkey = ([prefix] + key).join(separator)

            client.hmset(pkey, *self.class.pack(hash: values))
          end
        end

        def get(keys:)
          keys.map do |key|
            pkey = ([prefix] + key).join(separator)

            self.class.unpack(
              hash: client.hgetall(pkey)
            )
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trifle-stats-1.3.0 lib/trifle/stats/driver/redis.rb
trifle-stats-1.2.0 lib/trifle/stats/driver/redis.rb
trifle-stats-1.1.2 lib/trifle/stats/driver/redis.rb
trifle-stats-1.1.1 lib/trifle/stats/driver/redis.rb