Class: Fontist::Utils::Cache

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

Instance Method Summary collapse

Methods included from Locking

#lock

Instance Method Details

#delete(key) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/fontist/utils/cache.rb', line 20

def delete(key)
  lock(lock_path) do
    map = load_cache
    return unless map[key]

    value = map.delete(key)
    File.write(cache_map_path, YAML.dump(map))
    value
  end
end

#fetch(key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fontist/utils/cache.rb', line 6

def fetch(key)
  map = load_cache
  if 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



31
32
33
34
35
36
37
# File 'lib/fontist/utils/cache.rb', line 31

def set(key, value)
  lock(lock_path) do
    map = load_cache
    map[key] = value
    File.write(cache_map_path, YAML.dump(map))
  end
end