Sha256: d14566ece778f9ae5d716b9e9a7d51d8f1713659507c924b0e3c338d4dd87f28

Contents?: true

Size: 714 Bytes

Versions: 2

Compression:

Stored size: 714 Bytes

Contents

require 'digest/md5'
require 'fileutils'
require 'pathname'

module Sprockets
  module Cache
    # A simple file system cache store.
    #
    #     environment.cache = Sprockets::Cache::FileStore.new("tmp/sprockets")
    #
    class FileStore
      def initialize(root)
        @root = Pathname.new(root)

        # Ensure directory exists
        FileUtils.mkdir_p @root
      end

      # Lookup value in cache
      def [](key)
        pathname = @root.join(key)
        pathname.exist? ? pathname.open('rb') { |f| Marshal.load(f) } : nil
      end

      # Save value to cache
      def []=(key, value)
        @root.join(key).open('w') { |f| Marshal.dump(value, f)}
        value
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sprockets-2.1.0 lib/sprockets/cache/file_store.rb
sprockets-2.1.0.beta lib/sprockets/cache/file_store.rb