Module: DiskCache
- Extended by:
- DiskCache
- Included in:
- DiskCache
- Defined in:
- lib/disk_cache.rb,
lib/disk_cache/version.rb
Constant Summary collapse
- VERSION =
"0.0.6"
Instance Method Summary collapse
-
#clear! ⇒ Object
Public: this removes all contents of the cache.
-
#del(url) ⇒ Object
Public: delete a file from the cache.
-
#filepath(url) ⇒ Object
Public: calculate the path/filename of a file’s URL.
-
#get(url) ⇒ Object
Public: get a file from the cache.
-
#pget(url) ⇒ Object
Public: get a file and save it to the cache if it isn’t there already.
-
#put(url) ⇒ Object
Public: save a file to the cache.
Instance Method Details
#clear! ⇒ Object
Public: this removes all contents of the cache.
92 93 94 95 96 97 98 |
# File 'lib/disk_cache.rb', line 92 def clear! if File.exists? cache_dir Dir.glob(cache_dir + '*').each do |f| FileUtils.rm_r f end end end |
#del(url) ⇒ Object
Public: delete a file from the cache
url - The URL to the file
Example:
DiskCache.del('http://example.com/test123.jpg')
# => nil
Returns nil
72 73 74 |
# File 'lib/disk_cache.rb', line 72 def del(url) FileUtils.rm filepath(url), force: true end |
#filepath(url) ⇒ Object
Public: calculate the path/filename of a file’s URL
url - the URL of the file
Example:
DiskCache.filepath('http://example.com/test123.jpg')
# => "cache/9a/e2/74d94c34542ddd1b64667c1d4e392211ff67"
Returns a Sting with the full path and filename
86 87 88 89 |
# File 'lib/disk_cache.rb', line 86 def filepath(url) hsh = hash url path_h(hsh) + filename_h(hsh) end |
#get(url) ⇒ Object
41 42 43 44 45 |
# File 'lib/disk_cache.rb', line 41 def get(url) File.open(filepath(url), "rb") rescue Errno::ENOENT nil end |
#pget(url) ⇒ Object
57 58 59 60 |
# File 'lib/disk_cache.rb', line 57 def pget(url) put(url) get(url) end |
#put(url) ⇒ Object
Public: save a file to the cache
url - The URL to the file
Example:
DiskCache.put('http://example.com/test123.jpg')
#=> nil
Returns: nil
19 20 21 22 23 24 25 26 |
# File 'lib/disk_cache.rb', line 19 def put(url) file = filepath(url) return nil if File.exist?(file) FileUtils.mkdir_p path(url) File.open(file, "wb") { |f| f << open(escape(url)).read } nil end |