Class: Atom::FileCache

Inherits:
Object show all
Defined in:
lib/atom/cache.rb

Overview

uses a local directory to store cache files

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ FileCache

Returns a new instance of FileCache.



145
146
147
# File 'lib/atom/cache.rb', line 145

def initialize dir
  @dir = dir
end

Instance Method Details

#[](key) ⇒ Object



162
163
164
165
166
# File 'lib/atom/cache.rb', line 162

def [] key
  File.read(self.to_file(key))
rescue Errno::ENOENT
  nil
end

#[]=(key, value) ⇒ Object



168
169
170
171
172
# File 'lib/atom/cache.rb', line 168

def []= key, value
  File.open(self.to_file(key), 'w') do |f|
    f.write(value)
  end
end

#delete(key) ⇒ Object



174
175
176
# File 'lib/atom/cache.rb', line 174

def delete key
  File.delete(self.to_file(key))
end

#safe(filename) ⇒ Object

turns a URL into a safe filename



154
155
156
157
158
159
160
# File 'lib/atom/cache.rb', line 154

def safe filename
  filemd5 = MD5.hexdigest(filename)
  filename = filename.sub(/^\w+:\/\//, '')
  filename = filename.gsub(/[?\/:|]+/, ',')

  filename + "," + filemd5
end

#to_file(key) ⇒ Object



149
150
151
# File 'lib/atom/cache.rb', line 149

def to_file(key)
  @dir + "/" + self.safe(key)
end