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

Inherits:
AbstractStore show all
Defined in:
lib/tmdb_party/httparty_icebox.rb

Overview

Store objects on the filesystem

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileStore

Returns a new instance of FileStore.



201
202
203
204
205
206
207
# File 'lib/tmdb_party/httparty_icebox.rb', line 201

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)


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

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

#get(key) ⇒ Object



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

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



208
209
210
211
212
# File 'lib/tmdb_party/httparty_icebox.rb', line 208

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)


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

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