File: quick_cache.rb

package info (click to toggle)
ruby-compass 0.12.2~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,308 kB
  • sloc: ruby: 10,474; makefile: 42; xml: 14
file content (15 lines) | stat: -rw-r--r-- 437 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module QuickCache

  # cache a value in memory for just a few seconds
  # This can speed up reads of values that change relatively infrequently
  # but might be read many times in a short burst of reads.
  def quick_cache(key, ttl = 1)
    @quick_cache ||= {}
    if @quick_cache[key] && @quick_cache[key].first > Time.now - ttl
      @quick_cache[key].last
    else
      (@quick_cache[key] = [Time.now, yield]).last
    end
  end

end