Skip to content

Commit

Permalink
Use the same keys between data stores
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak committed Aug 13, 2014
1 parent 93aa318 commit aa31896
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
20 changes: 12 additions & 8 deletions lib/stoplight/data_store/base.rb
Expand Up @@ -76,20 +76,24 @@ def validate_state!(state)
fail ArgumentError, 'Invalid state'
end

def key(name, slug)
[DataStore::KEY_PREFIX, name, slug].join(':')
def attempts_key
key('attempts')
end

def attempt_key(name)
key(name, 'attempts')
def failures_key(name)
key('failures', name)
end

def failure_key(name)
key(name, 'failures')
def states_key
key('states')
end

def settings_key(name)
key(name, 'settings')
def thresholds_key
key('thresholds')
end

def key(slug, name = nil)
[KEY_PREFIX, name, slug].compact.join(':')
end
end
end
Expand Down
16 changes: 6 additions & 10 deletions lib/stoplight/data_store/memory.rb
Expand Up @@ -29,15 +29,15 @@ def clear_attempts(name)
# @group Failures

def failures(name)
@data[failure_key(name)] || []
@data[failures_key(name)] || []
end

def record_failure(name, error)
(@data[failure_key(name)] ||= []).push(Failure.new(error))
(@data[failures_key(name)] ||= []).push(Failure.new(error))
end

def clear_failures(name)
@data.delete(failure_key(name))
@data.delete(failures_key(name))
end

# @group State
Expand All @@ -64,19 +64,15 @@ def set_threshold(name, threshold)
private

def all_attempts
@data['attempts'] ||= {}
end

def all_failures
@data['failures'] ||= {}
@data[attempts_key] ||= {}
end

def all_states
@data['states'] ||= {}
@data[states_key] ||= {}
end

def all_thresholds
@data['thresholds'] ||= {}
@data[thresholds_key] ||= {}
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions lib/stoplight/data_store/redis.rb
Expand Up @@ -17,58 +17,58 @@ def initialize(*args)
end

def names
@redis.hkeys("#{KEY_PREFIX}:states")
@redis.hkeys(states_key)
end

# @group Attempts

def attempts(name)
@redis.hget("#{KEY_PREFIX}:attempts", name).to_i
@redis.hget(attempts_key, name).to_i
end

def record_attempt(name)
@redis.hincrby("#{KEY_PREFIX}:attempts", name, 1)
@redis.hincrby(attempts_key, name, 1)
end

def clear_attempts(name)
@redis.hdel("#{KEY_PREFIX}:attempts", name)
@redis.hdel(attempts_key, name)
end

# @group Failures

def failures(name)
@redis.lrange(failure_key(name), 0, -1)
@redis.lrange(failures_key(name), 0, -1)
end

def record_failure(name, error)
@redis.rpush(failure_key(name), Failure.new(error).to_json)
@redis.rpush(failures_key(name), Failure.new(error).to_json)
end

def clear_failures(name)
@redis.del(failure_key(name))
@redis.del(failures_key(name))
end

# @group State

def state(name)
@redis.hget("#{KEY_PREFIX}:states", name) || STATE_UNLOCKED
@redis.hget(states_key, name) || STATE_UNLOCKED
end

def set_state(name, state)
validate_state!(state)
@redis.hset("#{KEY_PREFIX}:states", name, state)
@redis.hset(states_key, name, state)
state
end

# @group Threshold

def threshold(name)
value = @redis.hget("#{KEY_PREFIX}:thresholds", name)
value = @redis.hget(thresholds_key, name)
Integer(value) if value
end

def set_threshold(name, threshold)
@redis.hset("#{KEY_PREFIX}:thresholds", name, threshold)
@redis.hset(thresholds_key, name, threshold)
threshold
end
end
Expand Down

0 comments on commit aa31896

Please sign in to comment.