Created
December 8, 2010 14:28
-
-
Save chsh/733341 to your computer and use it in GitHub Desktop.
BubbleValues keeps volatile values cleared in few seconds.
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
class BubbleValue | |
def initialize(hash, key, timeout = 3) | |
@hash = hash | |
@key = key | |
Thread.new { | |
sleep timeout | |
@hash[@key] = nil | |
} | |
end | |
end | |
class BubbleValues | |
def initialize(params = {}) | |
params = { :timeout => 3 }.merge params | |
@timeout = params[:timeout] | |
@hash = {} | |
end | |
def exist?(key) | |
@hash[key] || (@hash[key] = true; BubbleValue.new(@hash, key, @timeout); false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment