Class: Fontist::Utils::Cache
- Inherits:
-
Object
- Object
- Fontist::Utils::Cache
- Includes:
- Locking
- Defined in:
- lib/fontist/utils/cache.rb
Constant Summary collapse
- MAX_FILENAME_SIZE =
255
Instance Attribute Summary collapse
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
Class Method Summary collapse
Instance Method Summary collapse
- #already_fetched?(keys) ⇒ Boolean
- #delete(key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(cache_path: nil) ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
Methods included from Locking
Constructor Details
#initialize(cache_path: nil) ⇒ Cache
Returns a new instance of Cache.
62 63 64 |
# File 'lib/fontist/utils/cache.rb', line 62 def initialize(cache_path: nil) @cache_path = cache_path || Fontist.downloads_path end |
Instance Attribute Details
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
60 61 62 |
# File 'lib/fontist/utils/cache.rb', line 60 def cache_path @cache_path end |
Class Method Details
.lock_path(path) ⇒ Object
66 67 68 |
# File 'lib/fontist/utils/cache.rb', line 66 def self.lock_path(path) "#{path}.lock" end |
Instance Method Details
#already_fetched?(keys) ⇒ Boolean
84 85 86 87 |
# File 'lib/fontist/utils/cache.rb', line 84 def already_fetched?(keys) map = load_cache keys.find { |k| cache_exist?(map[k]) } end |
#delete(key) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fontist/utils/cache.rb', line 89 def delete(key) lock(lock_path) do map = load_cache return unless map[key] value = map.delete(key) map.to_file(cache_map_path) value end end |
#fetch(key) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fontist/utils/cache.rb', line 70 def fetch(key) map = load_cache if Fontist.use_cache? && cache_exist?(map[key]) print_cached(map[key]) return downloaded_file(map[key]) end generated_file = yield path = save_cache(generated_file, key) downloaded_file(path) end |
#set(key, value) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/fontist/utils/cache.rb', line 100 def set(key, value) lock(lock_path) do map = load_cache map[key] = value map.to_file(cache_map_path) end end |