Class: DerivedImages::Cache
- Inherits:
-
Object
- Object
- DerivedImages::Cache
- Includes:
- Enumerable
- Defined in:
- lib/derived_images/cache.rb
Overview
Build cache for derived image files.
Instance Method Summary collapse
-
#copy(key, path) ⇒ Object
Copy a cached file out to another path.
-
#digest(key) ⇒ String?
Get the SHA256 hash of a cached file.
-
#each {|key| ... } ⇒ DerivedImages::Cache, Enumerator
Iterates over cache keys.
-
#exist?(key) ⇒ Boolean
Check for the presence of a cached file.
-
#initialize(path = DerivedImages.config.cache_path) ⇒ Cache
constructor
A new instance of Cache.
-
#key_path(key) ⇒ Pathname
Convert a cache key into a filesystem path of where it would be stored.
-
#remove(key) ⇒ Object
Remove a cached file, if it exists.
-
#store(key, path) ⇒ Object
Copy a file into the cache.
-
#take_and_store(key, path) ⇒ Object
Move (not copy) a file into the cache.
Constructor Details
#initialize(path = DerivedImages.config.cache_path) ⇒ Cache
Returns a new instance of Cache.
9 10 11 |
# File 'lib/derived_images/cache.rb', line 9 def initialize(path = DerivedImages.config.cache_path) @path = path && Pathname.new(path) end |
Instance Method Details
#copy(key, path) ⇒ Object
Copy a cached file out to another path.
17 18 19 |
# File 'lib/derived_images/cache.rb', line 17 def copy(key, path) enabled? && FileUtils.copy(key_path(key), path) end |
#digest(key) ⇒ String?
Get the SHA256 hash of a cached file.
84 85 86 |
# File 'lib/derived_images/cache.rb', line 84 def digest(key) Digest::SHA256.file(key_path(key)).hexdigest if exist?(key) end |
#each {|key| ... } ⇒ DerivedImages::Cache, Enumerator
Iterates over cache keys.
73 74 75 76 77 78 |
# File 'lib/derived_images/cache.rb', line 73 def each return enum_for(:each) unless block_given? path.glob('*/*') { |path| yield path.to_s.last(65).delete('/') } if enabled? self end |
#exist?(key) ⇒ Boolean
Check for the presence of a cached file.
25 26 27 |
# File 'lib/derived_images/cache.rb', line 25 def exist?(key) enabled? && key_path(key).file? end |
#key_path(key) ⇒ Pathname
Convert a cache key into a filesystem path of where it would be stored.
65 66 67 |
# File 'lib/derived_images/cache.rb', line 65 def key_path(key) path.join(key[0...2], key[2..]) end |
#remove(key) ⇒ Object
Remove a cached file, if it exists.
32 33 34 35 36 37 |
# File 'lib/derived_images/cache.rb', line 32 def remove(key) return unless enabled? key_path(key).delete if key_path(key).exist? maybe_clean_dir_for(key) end |
#store(key, path) ⇒ Object
Copy a file into the cache.
43 44 45 46 47 48 |
# File 'lib/derived_images/cache.rb', line 43 def store(key, path) return unless enabled? mkdir_for(key) FileUtils.copy(path, key_path(key)) end |
#take_and_store(key, path) ⇒ Object
Move (not copy) a file into the cache.
54 55 56 57 58 59 |
# File 'lib/derived_images/cache.rb', line 54 def take_and_store(key, path) return unless enabled? mkdir_for(key) FileUtils.mv(path, key_path(key)) end |