Module: RSpeed::Redis

Defined in:
lib/rspeed/redis.rb

Class Method Summary collapse

Class Method Details

.cleanObject



9
10
11
12
13
14
# File 'lib/rspeed/redis.rb', line 9

def clean
  RSpeed::Logger.log(self, __method__, 'Cleaning pipes and profiles.')

  destroy(pattern: RSpeed::Variable::PIPES_PATTERN)
  destroy(pattern: RSpeed::Variable::PROFILE_PATTERN)
end

.clientObject



16
17
18
# File 'lib/rspeed/redis.rb', line 16

def client
  @client ||= ::Redis.new(db: RSpeed::Env.db, host: RSpeed::Env.host, port: RSpeed::Env.port)
end

.destroy(pattern:) ⇒ Object



20
21
22
23
24
# File 'lib/rspeed/redis.rb', line 20

def destroy(pattern:)
  RSpeed::Logger.log(self, __method__, %(Destroying pattern "#{pattern}".))

  keys(pattern: pattern).each { |key| client.del(key) }
end

.get(key) ⇒ Object



26
27
28
# File 'lib/rspeed/redis.rb', line 26

def get(key)
  client.get(key)
end

.keys(pattern:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspeed/redis.rb', line 30

def keys(pattern:)
  cursor = 0
  result = []

  loop do
    cursor, results = client.scan(cursor, match: pattern)
    result += results

    break if cursor.to_i.zero?
  end

  result
end

.list(key) ⇒ Object



44
45
46
# File 'lib/rspeed/redis.rb', line 44

def list(key)
  client.lrange(key, 0, -1)
end

.profiles_content(pattern: 'rspeed:profile_*') ⇒ Object



48
49
50
# File 'lib/rspeed/redis.rb', line 48

def profiles_content(pattern: 'rspeed:profile_*')
  client.keys(pattern).map { |key| list(key) }.flatten
end

.result?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rspeed/redis.rb', line 52

def result?
  keys(pattern: RSpeed::Variable.result).any?
end

.set(key, value) ⇒ Object



56
57
58
# File 'lib/rspeed/redis.rb', line 56

def set(key, value)
  client.set(key, value)
end

.specs_finished?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/rspeed/redis.rb', line 60

def specs_finished?
  (RSpeed::Redis.keys(pattern: RSpeed::Variable::PIPES_PATTERN).size == RSpeed::Env.pipes).tap do |boo|
    RSpeed::Logger.log(self, __method__, "Specs #{boo ? 'finished.' : 'not fineshed yet.'}")
  end
end

.specs_initiated?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/rspeed/redis.rb', line 66

def specs_initiated?
  RSpeed::Redis.keys(pattern: RSpeed::Variable::PIPES_PATTERN).any?.tap do |boo|
    RSpeed::Logger.log(self, __method__, "Specs #{boo ? 'initialized.' : 'not initialized yet.'}")
  end
end

.version_the_resultObject



72
73
74
75
76
# File 'lib/rspeed/redis.rb', line 72

def version_the_result
  RSpeed::Logger.log(self, __method__, 'Versioning the result.')

  client.rename(RSpeed::Variable.result, RSpeed::Variable.previous_result)
end