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")

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



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

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

Instance Method Details

#[](key) ⇒ Object

Lookup value in cache



17
18
19
20
# File 'lib/sprockets/cache/file_store.rb', line 17

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

#[]=(key, value) ⇒ Object

Save value to cache



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

def []=(key, value)
  # Ensure directory exists
  FileUtils.mkdir_p @root.join(key).dirname

  @root.join(key).open('w') { |f| Marshal.dump(value, f)}
  value
end