Class: Waves::Caches::File

Inherits:
Simple show all
Defined in:
lib/waves/caches/file.rb

Overview

A file-based cache, where the keys are the filenames.

Instance Method Summary collapse

Methods inherited from Simple

#[], #[]=, #exists?

Constructor Details

#initialize(args) ⇒ File

Returns a new instance of File.

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/waves/caches/file.rb', line 9

def initialize( args )
  raise ArgumentError, ":directory is nil" if args[ :directory ].nil?
  @directory = args[ :directory ] ; @keys = []
end

Instance Method Details

#clearObject



26
27
28
# File 'lib/waves/caches/file.rb', line 26

def clear
  @keys.each { |key| delete( key ) }
end

#delete(key) ⇒ Object



19
20
21
22
23
24
# File 'lib/waves/caches/file.rb', line 19

def delete( key )
  if @keys.include? key
    ::File.delete( @directory / key )
    @keys.delete( key )
  end
end

#fetch(key) ⇒ Object



30
31
32
33
34
# File 'lib/waves/caches/file.rb', line 30

def fetch( key )
  Marshal.load( ::File.read( @directory / key ) ) if @keys.include?( key )
rescue ArgumentError
  nil
end

#store(key, value) ⇒ Object



14
15
16
17
# File 'lib/waves/caches/file.rb', line 14

def store( key, value )            
  @keys << key
  ::File.open( @directory / key, 'w') { |f| Marshal.dump( value, f ) }
end