Class: Fontist::Utils::Cache

Inherits:
Object
  • Object
show all
Includes:
Locking
Defined in:
lib/fontist/utils/cache.rb

Constant Summary collapse

MAX_FILENAME_SIZE =
255

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Locking

#lock

Class Method Details

.lock_path(path) ⇒ Object



58
59
60
# File 'lib/fontist/utils/cache.rb', line 58

def self.lock_path(path)
  "#{path}.lock"
end

Instance Method Details

#already_fetched?(keys) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/fontist/utils/cache.rb', line 76

def already_fetched?(keys)
  map = load_cache
  keys.find { |k| cache_exist?(map[k]) }
end

#delete(key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/fontist/utils/cache.rb', line 81

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fontist/utils/cache.rb', line 62

def fetch(key)
  map = load_cache
  if Fontist.use_cache? && cache_exist?(map[key])
    print(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



92
93
94
95
96
97
98
# File 'lib/fontist/utils/cache.rb', line 92

def set(key, value)
  lock(lock_path) do
    map = load_cache
    map[key] = value
    map.to_file(cache_map_path)
  end
end