Class: Sprockets::Cache::FileStore

### Home Classes Methods

### In Files

- lib/sprockets/cache/file_store.rb

### Parent

Object

### Methods

- ::default_logger
- ::new
- #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::FileStore

Public: A file system cache store that automatically cleans up old keys.

Assign the instance to the Environment#cache.

    environment.cache = Sprockets::Cache::FileStore.new("/tmp")

See Also

    ActiveSupport::Cache::FileStore

### Constants

DEFAULT_MAX_SIZE

  Internal: Default key limit for store.

### Public Class Methods

default_logger() click to toggle source

Internal: Default standard error fatal logger.

Returns a Logger.

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

new(root, max_size = DEFAULT_MAX_SIZE, logger = self.class.default_logger) click to toggle source

Public: Initialize the cache store.

root - A String path to a directory to persist cached values to. max_size - A Integer of the maximum number of keys the store will hold.

    (default: 1000).

    # File lib/sprockets/cache/file_store.rb, line 37
    def initialize(root, max_size = DEFAULT_MAX_SIZE, logger = self.class.default_logger)
      @root     = root
      @size     = find_caches.inject(0) { |n, (_, stat)| n + stat.size }
      @max_size = max_size
      @gc_size  = max_size * 0.75
      @logger   = logger
    end

### Public Instance Methods

get(key) click to toggle source

Public: Retrieve value from cache.

This API should not be used directly, but via the Cache wrapper API.

key - String cache key.

Returns Object or nil or the value is not set.

    # File lib/sprockets/cache/file_store.rb, line 52
    def get(key)
      path = File.join(@root, "#{key}.cache")

      value = safe_open(path) do |f|
        begin
          EncodingUtils.unmarshaled_deflated(f.read, Zlib::MAX_WBITS)
        rescue Exception => e
          @logger.error do
            "#{self.class}[#{path}] could not be unmarshaled: " +
              "#{e.class}: #{e.message}"
          end
          nil
        end
      end

      if value
        FileUtils.touch(path)
        value
      end
    end

inspect() click to toggle source

Public: Pretty inspect

Returns String.

    # File lib/sprockets/cache/file_store.rb, line 122
    def inspect
      "#<#{self.class} size=#{@size}/#{@max_size}>"
    end

set(key, value) click to toggle source

Public: Set a key and value in the cache.

This API should not be used directly, but via the Cache wrapper API.

key - String cache key. value - Object value.

Returns Object value.

    # File lib/sprockets/cache/file_store.rb, line 81
    def set(key, value)
      path = File.join(@root, "#{key}.cache")

      # Ensure directory exists
      FileUtils.mkdir_p File.dirname(path)

      # Check if cache exists before writing
      exists = File.exist?(path)

      # Serialize value
      marshaled = Marshal.dump(value)

      # Compress if larger than 4KB
      if marshaled.bytesize > 4 * 1024
        deflater = Zlib::Deflate.new(
          Zlib::BEST_COMPRESSION,
          Zlib::MAX_WBITS,
          Zlib::MAX_MEM_LEVEL,
          Zlib::DEFAULT_STRATEGY
        )
        deflater << marshaled
        raw = deflater.finish
      else
        raw = marshaled
      end

      # Write data
      PathUtils.atomic_write(path) do |f|
        f.write(raw)
        @size += f.size unless exists
      end

      # GC if necessary
      gc! if @size > @max_size

      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.