Module: Sprockets::Caching

### Home Classes Methods

### In Files

- lib/sprockets/caching.rb

### Methods

- #cache_asset
- #cache_get
- #cache_set

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::AssetAttributes
- Sprockets::Base
- Sprockets::BundledAsset
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Caching
- Sprockets::CharsetNormalizer
- Sprockets::CircularDependencyError
- Sprockets::ClosureCompressor
- Sprockets::Compressing
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::DirectiveProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsTemplate
- Sprockets::EncodingError
- Sprockets::EngineError
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::Manifest
- Sprockets::Mime
- Sprockets::NotImplementedError
- Sprockets::Paths
- Sprockets::ProcessedAsset
- Sprockets::ProcessedAsset::DependencyFile
- Sprockets::Processing
- Sprockets::Processor
- Sprockets::SafetyColons
- Sprockets::SassCacheStore
- Sprockets::SassCompressor
- Sprockets::SassFunctions
- Sprockets::SassImporter
- Sprockets::SassTemplate
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::StaticAsset
- Sprockets::UglifierCompressor
- Sprockets::UnserializeError
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Caching

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

### Public Instance Methods

cache_get(key) click to toggle source

Low level cache getter for `key`. Checks a number of supported cache interfaces.

    # File lib/sprockets/caching.rb, line 7
    def cache_get(key)
      # `Cache#get(key)` for Memcache
      if cache.respond_to?(:get)
        cache.get(key)

      # `Cache#[key]` so `Hash` can be used
      elsif cache.respond_to?(:[])
        cache[key]

      # `Cache#read(key)` for `ActiveSupport::Cache` support
      elsif cache.respond_to?(:read)
        cache.read(key)

      else
        nil
      end
    end

cache_set(key, value) click to toggle source

Low level cache setter for `key`. Checks a number of supported cache interfaces.

    # File lib/sprockets/caching.rb, line 27
    def cache_set(key, value)
      # `Cache#set(key, value)` for Memcache
      if cache.respond_to?(:set)
        cache.set(key, value)

      # `Cache#[key]=value` so `Hash` can be used
      elsif cache.respond_to?(:[]=)
        cache[key] = value

      # `Cache#write(key, value)` for `ActiveSupport::Cache` support
      elsif cache.respond_to?(:write)
        cache.write(key, value)
      end

      value
    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 48
    def cache_asset(path)
      # If `cache` is not set, return fast
      if cache.nil?
        yield

      # Check cache for `path`
      elsif (asset = Asset.from_hash(self, cache_get_hash(path.to_s))) && asset.fresh?(self)
        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, 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, hash)
        end

        asset
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.