Skip to content

Instantly share code, notes, and snippets.

@voidlock voidlock/gist:289104
Created Jan 28, 2010

Embed
What would you like to do?
require "rubygems"
require "redis"
redis = Redis.new
redis.delete "foo" # cleanup
puts "SETNX COMMAND:"
puts "Should be true:"
puts redis.set_unless_exists("foo", 1)
puts
puts "Should be false"
puts redis.set_unless_exists("foo", 1)
puts redis.set_unless_exists("foo", 1)
puts
puts "Setting expire"
puts redis.expire("foo", 200)
puts "ttl #{redis.ttl("foo")}"
puts
puts "Should be false:"
puts redis.set_unless_exists("foo", 1)
puts
puts "GETSET COMMAND:"
redis.delete "foo" # cleanup
puts "Should be nil:"
puts redis.getset("foo", 1)
puts
puts "Should be 1, 2"
puts redis.getset("foo", 2)
puts redis.getset("foo", 3)
puts
puts "Setting expire"
puts redis.expire("foo", 200)
puts "ttl #{redis.ttl("foo")}"
puts
puts "Should be 3:"
puts redis.getset("foo", 4)
# Output from the above
# SETNX COMMAND:
# Should be true:
# true
#
# Should be false
# false
# false
#
# Setting expire
# true
# ttl 200
#
# Should be false:
# true
#
# GETSET COMMAND:
# Should be nil:
# nil
#
# Should be 1, 2
# 1
# 2
#
# Setting expire
# true
# ttl 200
#
# Should be 3:
# 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.