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.



205
206
207
208
209
210
211
# File 'lib/httparty_icebox.rb', line 205

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)


222
223
224
# File 'lib/httparty_icebox.rb', line 222

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

#get(key) ⇒ Object



217
218
219
220
221
# File 'lib/httparty_icebox.rb', line 217

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



212
213
214
215
216
# File 'lib/httparty_icebox.rb', line 212

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)


225
226
227
228
# File 'lib/httparty_icebox.rb', line 225

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