Skip to content

Commit

Permalink
Add support for new SETEX command (atomic SET+EXPIRE).
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyeow committed Apr 26, 2010
1 parent 37c42b7 commit 41443f9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(reply_type)
BULK_COMMANDS = {
"set" => true,
"setnx" => true,
"setex" => true,
"rpush" => true,
"lpush" => true,
"lset" => true,
Expand Down Expand Up @@ -60,6 +61,7 @@ def initialize(reply_type)
"del" => BOOLEAN_PROCESSOR,
"renamenx" => BOOLEAN_PROCESSOR,
"expire" => BOOLEAN_PROCESSOR,
"setex" => BOOLEAN_PROCESSOR,
"hset" => BOOLEAN_PROCESSOR,
"hexists" => BOOLEAN_PROCESSOR,
"info" => lambda{|r|
Expand Down Expand Up @@ -188,10 +190,7 @@ def set(key, value, ttl = nil)
end

def set_with_expire(key, value, ttl)
multi do
set(key, value)
expire(key, ttl)
end
setex(key, ttl, value)
end

def mset(*args)
Expand Down
2 changes: 1 addition & 1 deletion test/redis_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def redis.call_command(attrs)
end
end

test "SET with EXPIRE" do
test "SET with EXPIRE (i.e. SETEX)" do
@r.set_with_expire("foo", "s1", 1)

assert_equal "s1", @r.get("foo")
Expand Down

4 comments on commit 41443f9

@djanowski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey!
Michel and I were thinking that we may need to keep set_with_expire with the current implementation and add setex separately. Additionally, add a warning in set_with_expire suggesting users to use setex.
Otherwise we'd be breaking backwards compatibility for users who are not on Redis edge.
What do you think?

@chuyeow
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, that is a better way to handle things :)

@djanowski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, so let us know when you have a new patch. Thanks again!

@chuyeow
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm kinda confused now whether the 2.x branch (now master) is meant to be run only with a Redis revision that supports SETEX. If it is, there's no need to support set_with_expire with the current implementation.

Anyway, here's a shot: http://github.com/chuyeow/redis-rb/commits/setex (including an earlier commit that fixes a syntax error in Ruby 1.8.7).

Please sign in to comment.