Class: AssetRam::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_ram.rb

Overview

Our own asset helper which memoizes Rails’ asset helper calls.

Constant Summary collapse

NO_CACHE =

For testing the gains from caching with this library. Set to true to disable caching.

false
@@_cache =
{}

Class Method Summary collapse

Class Method Details

.cache(key: '', &blk) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asset_ram.rb', line 36

def self.cache(key: '', &blk)
  return yield if NO_CACHE

  cache_key = blk.source_location
  cache_key << key if key.present?

  if !@@_cache.has_key?(cache_key)
    # Using WARN level because it should only be output
    # once during any Rails run. If its output multiple
    # times, then caching isn't working correctly.
    Rails.logger.warn("Caching #{cache_key}")
    @@_cache[cache_key] = yield
  end

  @@_cache.fetch(cache_key)
end