Class: Sprockets::Cache

### Home Classes Methods

### In Files

- lib/sprockets/cache.rb
- lib/sprockets/cache/file_store.rb
- lib/sprockets/cache/memory_store.rb
- lib/sprockets/cache/null_store.rb

### Parent

Object

### Namespace

- CLASS Sprockets::Cache::FileStore
- CLASS Sprockets::Cache::GetWrapper
- CLASS Sprockets::Cache::HashWrapper
- CLASS Sprockets::Cache::MemoryStore
- CLASS Sprockets::Cache::NullStore
- CLASS Sprockets::Cache::ReadWriteWrapper
- CLASS Sprockets::Cache::Wrapper

### Methods

- ::default_logger
- ::new
- #fetch
- #get
- #inspect
- #set

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Cache

Public: Wrapper interface to backend cache stores. Ensures a consistent API even when the backend uses get/set or read/write.

Public cache interface

Always assign the backend store instance to Environment#cache=.

    environment.cache = Sprockets::Cache::MemoryStore.new(1000)

Environment#cache will always return a wrapped Cache interface. See the methods marked public on this class.

Backend cache interface

The Backend cache store must implement two methods.

get(key)

    key - An opaque String with a length less than 250 characters.

    Returns an JSON serializable object.

set(key, value)

    Will only be called once per key. Setting a key "foo" with value "bar",
    then later key "foo" with value "baz" is an undefined behavior.

    key   - An opaque String with a length less than 250 characters.
    value - A JSON serializable object.

    Returns argument value.

### Constants

PEEK_SIZE
VERSION

  Internal: Cache key version for this class. Rarely should have to change unless the cache format radically changes. Will be bump on major version releases though.

### Public Class Methods

default_logger() click to toggle source

    # File lib/sprockets/cache.rb, line 49
    def self.default_logger
      logger = Logger.new($stderr)
      logger.level = Logger::FATAL
      logger
    end

new(cache = nil, logger = self.class.default_logger) click to toggle source

Internal: Wrap a backend cache store.

Always assign a backend cache store instance to Environment#cache= and use Environment#cache to retreive a wrapped interface.

cache - A compatible backend cache store instance.

    # File lib/sprockets/cache.rb, line 61
    def initialize(cache = nil, logger = self.class.default_logger)
      @cache_wrapper = get_cache_wrapper(cache)
      @fetch_cache   = Cache::MemoryStore.new(1024)
      @logger        = logger
    end

### Public Instance Methods

fetch(key) click to toggle source

Public: Prefer API to retrieve and set values in the cache store.

key - JSON serializable key block -

    Must return a consistent JSON serializable object for the given key.

Examples

    cache.fetch("foo") { "bar" }

Returns a JSON serializable object.

    # File lib/sprockets/cache.rb, line 78
    def fetch(key)
      start = Time.now.to_f
      expanded_key = expand_key(key)
      value = @fetch_cache.get(expanded_key)
      if value.nil?
        value = @cache_wrapper.get(expanded_key)
        if value.nil?
          value = yield
          @cache_wrapper.set(expanded_key, value)
          @logger.debug do
            ms = "(#{((Time.now.to_f - start) * 1000).to_i}ms)"
            "Sprockets Cache miss #{peek_key(key)}  #{ms}"
          end
        end
        @fetch_cache.set(expanded_key, value)
      end
      value
    end

get(key, local = false) click to toggle source

Public: Low level API to retrieve item directly from the backend cache store.

This API may be used publicly, but may have undefined behavior depending on the backend store being used. Prefer the Cache#fetch API over using this.

key - JSON serializable key local - Check local cache first (default: false)

Returns a JSON serializable object or nil if there was a cache miss.

    # File lib/sprockets/cache.rb, line 108
    def get(key, local = false)
      expanded_key = expand_key(key)

      if local && value = @fetch_cache.get(expanded_key)
        return value
      end

      value = @cache_wrapper.get(expanded_key)
      @fetch_cache.set(expanded_key, value) if local

      value
    end

inspect() click to toggle source

Public: Pretty inspect

Returns String.

    # File lib/sprockets/cache.rb, line 142
    def inspect
      "#<#{self.class} local=#{@fetch_cache.inspect} store=#{@cache_wrapper.cache.inspect}>"
    end

set(key, value, local = false) click to toggle source

Public: Low level API to set item directly to the backend cache store.

This API may be used publicly, but may have undefined behavior depending on the backend store being used. Prefer the Cache#fetch API over using this.

key - JSON serializable key value - A consistent JSON serializable object for the given key. Setting

    a different value for the given key has undefined behavior.

local - Set on local cache (default: false)

Returns the value argument.

    # File lib/sprockets/cache.rb, line 133
    def set(key, value, local = false)
      expanded_key = expand_key(key)
      @fetch_cache.set(expanded_key, value) if local
      @cache_wrapper.set(expanded_key, value)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.