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.



233
234
235
236
237
238
239
# File 'lib/httparty-icebox.rb', line 233

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)


250
251
252
# File 'lib/httparty-icebox.rb', line 250

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

#get(key) ⇒ Object



245
246
247
248
249
# File 'lib/httparty-icebox.rb', line 245

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

#set(key, value) ⇒ Object



240
241
242
243
244
# File 'lib/httparty-icebox.rb', line 240

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

#stale?(key) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
# File 'lib/httparty-icebox.rb', line 253

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