Class: HTTParty::Icebox::Store::FileStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- HTTParty::Icebox::Store::FileStore
- Defined in:
- lib/httparty-icebox.rb
Overview
Store objects on the filesystem
See HTTParty::Icebox::ClassMethods.cache
Instance Method Summary collapse
- #exists?(key) ⇒ Boolean
- #get(key) ⇒ Object
-
#initialize(options = {}) ⇒ FileStore
constructor
A new instance of FileStore.
- #set(key, value) ⇒ Object
- #stale?(key) ⇒ Boolean
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(={}) super [:location] ||= Dir::tmpdir @path = Pathname.new( [:location] ) FileUtils.mkdir_p( @path ) self end |
Instance Method Details
#exists?(key) ⇒ 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
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 |