Class: Waves::Caches::File
Overview
A file-based cache, where the keys are the filenames.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(args) ⇒ File
constructor
A new instance of File.
- #store(key, value) ⇒ Object
Methods inherited from Simple
Constructor Details
#initialize(args) ⇒ File
Returns a new instance of File.
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
#clear ⇒ Object
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 |