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



8
9
10
# File 'lib/fontist/utils/cache.rb', line 8

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

Instance Method Details

#already_fetched?(keys) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/fontist/utils/cache.rb', line 26

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

#delete(key) ⇒ Object



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

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



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fontist/utils/cache.rb', line 12

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



42
43
44
45
46
47
48
# File 'lib/fontist/utils/cache.rb', line 42

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