Sprockets::Caching

`Caching` is an internal mixin whose public methods are exposed on the `Environment` and `Index` classes.

Public Instance Methods

asset_from_hash(hash) click to toggle source

Return `Asset` instance for serialized `Hash`.

# File lib/sprockets/caching.rb, line 9
def asset_from_hash(hash)
  return unless hash.is_a?(Hash)
  case hash['class']
  when 'BundledAsset'
    BundledAsset.from_hash(self, hash)
  when 'StaticAsset'
    StaticAsset.from_hash(self, hash)
  else
    nil
  end
rescue Exception => e
  logger.debug "Cache for Asset (#{hash['logical_path']}) is stale"
  logger.debug e
  nil
end
cache_hash(key, version) click to toggle source
# File lib/sprockets/caching.rb, line 25
def cache_hash(key, version)
  if cache.nil?
    yield
  elsif hash = cache_get_hash(key, version)
    hash
  elsif hash = yield
    cache_set_hash(key, version, hash)
    hash
  end
end

Protected Instance Methods

cache_asset(path) click to toggle source

Cache helper method. Takes a `path` argument which maybe a logical path or fully expanded path. The `&block` is passed for finding and building the asset if its not in cache.

# File lib/sprockets/caching.rb, line 40
def cache_asset(path)
  # If `cache` is not set, return fast
  if cache.nil?
    yield

  # Check cache for `path`
  elsif (asset = asset_from_hash(cache_get_hash(path.to_s, digest.hexdigest))) && asset.fresh?
    asset

   # Otherwise yield block that slowly finds and builds the asset
  elsif asset = yield
    hash = {}
    asset.encode_with(hash)

    # Save the asset to its path
    cache_set_hash(path.to_s, digest.hexdigest, hash)

    # Since path maybe a logical or full pathname, save the
    # asset its its full path too
    if path.to_s != asset.pathname.to_s
      cache_set_hash(asset.pathname.to_s, digest.hexdigest, hash)
    end

    asset
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.