Skip to content

Instantly share code, notes, and snippets.

@chsh
Created December 8, 2010 14:28
Show Gist options
  • Save chsh/733341 to your computer and use it in GitHub Desktop.
Save chsh/733341 to your computer and use it in GitHub Desktop.
BubbleValues keeps volatile values cleared in few seconds.
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