Class: HTTParty::Icebox::Store::FileStore

Inherits:
AbstractStore show all
Defined in:
lib/httparty-icebox.rb

Overview

Store objects on the filesystem

See HTTParty::Icebox::ClassMethods.cache

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileStore

Returns a new instance of FileStore.



203
204
205
206
207
208
209
# File 'lib/httparty-icebox.rb', line 203

def initialize(options={})
  super
  options[:location] ||= Dir::tmpdir
  @path = Pathname.new( options[:location] )
  FileUtils.mkdir_p( @path )
  self
end

Instance Method Details

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/httparty-icebox.rb', line 220

def exists?(key)
  File.exists?( @path.join(key) )
end

#get(key) ⇒ Object



215
216
217
218
219
# File 'lib/httparty-icebox.rb', line 215

def get(key)
  data = Marshal.load(File.read( @path.join(key)))
  Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
  data
end

#set(key, value) ⇒ Object



210
211
212
213
214
# File 'lib/httparty-icebox.rb', line 210

def set(key, value)
  Cache.logger.info("Cache: set (#{key})")
  File.open( @path.join(key), 'w' ) { |file| file << Marshal.dump(value)  }
  true
end

#stale?(key) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
# File 'lib/httparty-icebox.rb', line 223

def stale?(key)
  return true unless exists?(key)
  Time.now - created(key) > @timeout
end