Skip to content

Instantly share code, notes, and snippets.

@bumi
Created November 26, 2014 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumi/2e016635996cce103a0c to your computer and use it in GitHub Desktop.
Save bumi/2e016635996cce103a0c to your computer and use it in GitHub Desktop.
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
@kangguru
Copy link

you'll need to create a different redis object with the #subscribe-block so you can use #set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment