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.5"
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.
91 92 93 94 95 96 97 |
# File 'lib/disk_cache.rb', line 91 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
71 72 73 |
# File 'lib/disk_cache.rb', line 71 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
85 86 87 88 |
# File 'lib/disk_cache.rb', line 85 def filepath(url) hsh = hash url path_h(hsh) + filename_h(hsh) end |
#get(url) ⇒ Object
Public: get a file from the cache
url - The URL to the file
Examples:
DiskCache.get('http://example.com/test123.jpg')
# => #<File:9ae274d94c34542ddd1b64667c1d4e392211ff67>
DiskCache.get('http://example.com/test123.jpg')
# => nil
Returns the File or nil
40 41 42 43 44 |
# File 'lib/disk_cache.rb', line 40 def get(url) File.open(filepath(url), "rb") rescue Errno::ENOENT nil end |
#pget(url) ⇒ Object
Public: get a file and save it to the cache if it isn’t there already
url - The URL to the file
Examples:
DiskCache.get('http://example.com/test123.jpg')
# => #<File:9ae274d94c34542ddd1b64667c1d4e392211ff67>
Returns a File
56 57 58 59 |
# File 'lib/disk_cache.rb', line 56 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
18 19 20 21 22 23 24 25 |
# File 'lib/disk_cache.rb', line 18 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 |