Last active
August 29, 2015 14:20
-
-
Save Aupajo/6e53fb73a847c3258fd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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