Class: Sprockets::Cache::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/cache/file_store.rb

Overview

A simple file system cache store.

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

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



12
13
14
15
16
17
# File 'lib/sprockets/cache/file_store.rb', line 12

def initialize(root)
  @root = Pathname.new(root)

  # Ensure directory exists
  FileUtils.mkdir_p @root
end

Instance Method Details

#[](key) ⇒ Object

Lookup value in cache



20
21
22
23
# File 'lib/sprockets/cache/file_store.rb', line 20

def [](key)
  pathname = path_for(key)
  pathname.exist? ? pathname.open('rb') { |f| Marshal.load(f) } : nil
end

#[]=(key, value) ⇒ Object

Save value to cache



26
27
28
29
# File 'lib/sprockets/cache/file_store.rb', line 26

def []=(key, value)
  path_for(key).open('w') { |f| Marshal.dump(value, f)}
  value
end