Class: Trifle::Stats::Driver::Redis

Inherits:
Object
  • Object
show all
Includes:
Mixins::Packer
Defined in:
lib/trifle/stats/driver/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Packer

included

Constructor Details

#initialize(client = ::Redis.current, prefix: 'trfl') ⇒ Redis

Returns a new instance of Redis.



12
13
14
15
16
# File 'lib/trifle/stats/driver/redis.rb', line 12

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/trifle/stats/driver/redis.rb', line 10

def client
  @client
end

#prefixObject

Returns the value of attribute prefix.



10
11
12
# File 'lib/trifle/stats/driver/redis.rb', line 10

def prefix
  @prefix
end

#separatorObject

Returns the value of attribute separator.



10
11
12
# File 'lib/trifle/stats/driver/redis.rb', line 10

def separator
  @separator
end

Instance Method Details

#get(keys:) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/trifle/stats/driver/redis.rb', line 36

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

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

#inc(keys:, **values) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/trifle/stats/driver/redis.rb', line 18

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

#set(keys:, **values) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/trifle/stats/driver/redis.rb', line 28

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

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