-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jay Adkisson
committed
Jun 20, 2010
1 parent
fc93b67
commit 5a05e03
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Rudis | ||
class Counter < Structure | ||
def incr | ||
redis.incr(key) | ||
end | ||
|
||
def decr | ||
redis.decr(key) | ||
end | ||
|
||
def incrby(i) | ||
redis.incrby(key, i.to_i) | ||
end | ||
|
||
def decrby(i) | ||
redis.decrby(key, i.to_i) | ||
end | ||
|
||
def to_i | ||
redis.get(key).to_i | ||
end | ||
|
||
def zero? | ||
to_i.zero? | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class Rudis | ||
class Lock < Structure | ||
class LockFailed < Exception; end | ||
|
||
def acquire(options={}) | ||
options.rmerge!( | ||
:tries => 1, | ||
:sleep => 1 | ||
) | ||
|
||
return set(options) unless block_given? | ||
|
||
1.upto options[:tries] do | ||
if set(options) | ||
return begin | ||
yield | ||
ensure | ||
clear | ||
end | ||
end | ||
sleep options[:sleep] | ||
end | ||
|
||
# oops, we couldn't get the lock | ||
raise LockFailed, <<-msg.squish | ||
Unable to acquire lock after #{options[:tries]} time(s) | ||
msg | ||
return false | ||
end | ||
|
||
def set(options={}) | ||
options.rmerge!( | ||
:timeout => 30 | ||
) | ||
if redis.setnx(key, timestamp(options[:timeout])) | ||
return true | ||
else | ||
# check the timestamp | ||
old = redis.getset(key, timestamp(options[:timeout])) | ||
if old < timestamp | ||
# expired lock, we're good | ||
return true | ||
else | ||
# lock is not expired, put it back | ||
redis.set(key, old) | ||
return false | ||
end | ||
end | ||
end | ||
|
||
alias clear del | ||
|
||
private | ||
def timestamp(timeout=0) | ||
Time.now.to_i + timeout | ||
end | ||
|
||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
describe Rudis::Counter do | ||
before :each do | ||
Rudis::Counter.redis.flushdb | ||
@c = Rudis::Counter.new('my_counter') | ||
end | ||
|
||
it "counts!" do | ||
@c.to_i.should == 0 | ||
@c.should be_zero | ||
@c.incr.should == 1 | ||
@c.incr.should == 2 | ||
@c.decr.should == 1 | ||
@c.incrby(4).should == 5 | ||
@c.decrby(2).should == 3 | ||
@c.incrby(-1).should == 2 | ||
end | ||
end |