Created
November 26, 2014 14:36
-
-
Save bumi/2e016635996cce103a0c 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
require "redis" | |
redis = Redis.new | |
trap(:INT) { | |
redis.unsubscribe | |
puts 'bye' | |
exit | |
} | |
puts redis.set 'hello', 'test', ex: 5 | |
begin | |
redis.subscribe('__keyevent@0__:expired') do |on| | |
on.subscribe do |channel, subscriptions| | |
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)" | |
end | |
on.message do |channel, key| | |
puts "#{key} has expired" | |
redis.set key, "", ex: 2 * (2 ** 3) # does not work here! :( | |
end | |
on.unsubscribe do |channel, subscriptions| | |
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)" | |
end | |
end | |
rescue Redis::BaseConnectionError => error | |
puts "#{error}, retrying in 1s" | |
sleep 1 | |
retry | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you'll need to create a different redis object with the
#subscribe
-block so you can use#set