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, prefix: 'trfl') ⇒ Redis



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

def initialize(client, 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

#descriptionObject



18
19
20
# File 'lib/trifle/stats/driver/redis.rb', line 18

def description
  "#{self.class.name}(J)"
end

#get(keys:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/trifle/stats/driver/redis.rb', line 42

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

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

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



22
23
24
25
26
27
28
29
30
31
# File 'lib/trifle/stats/driver/redis.rb', line 22

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

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

#pingObject



53
54
55
# File 'lib/trifle/stats/driver/redis.rb', line 53

def ping(*)
  []
end

#scanObject



57
58
59
# File 'lib/trifle/stats/driver/redis.rb', line 57

def scan(*)
  []
end

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



33
34
35
36
37
38
39
40
# File 'lib/trifle/stats/driver/redis.rb', line 33

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

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