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.



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

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)


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

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

#get(key) ⇒ Object



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

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

#set(key, value) ⇒ Object



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

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)


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

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