Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aupajo/6e53fb73a847c3258fd2 to your computer and use it in GitHub Desktop.
Save Aupajo/6e53fb73a847c3258fd2 to your computer and use it in GitHub Desktop.
# Usage
# sequence = AtomicSequence["cats"]
# sequence.current # => 0
# sequence.next # => 1
# sequence.next # => 2
# sequence.current # => 2
# sequence.reset!
# sequence.current # => 0
# sequence.next # => 1
class AtomicSequence < Struct.new(:key)
Store = REDIS
def next
store :incr
end
def current
store(:get).to_i
end
def reset!
store :del
end
def self.[](name)
self.new(name)
end
private
def store(method)
Store.send(method, key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment