File: quick_cache.rb

package info (click to toggle)
ruby-compass 1.0.3~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,184 kB
  • ctags: 1,789
  • sloc: ruby: 12,904; makefile: 100; perl: 43; xml: 14; sh: 4
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