Class: Nauvisian::Cache::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/nauvisian/cache/file_system.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, ttl: MINIMUM_TTL) ⇒ FileSystem

Returns a new instance of FileSystem.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/nauvisian/cache/file_system.rb', line 16

def initialize(name:, ttl: MINIMUM_TTL)
  raise ArgumentError, "ttl is too small (must be >= #{MINIMUM_TTL})" if ttl < MINIMUM_TTL

  @cache_directory = self.class.cache_root / name
  @ttl = ttl
end

Class Method Details

.cache_rootObject



12
13
14
# File 'lib/nauvisian/cache/file_system.rb', line 12

def self.cache_root
  Pathname(ENV.fetch("XDG_CACHE_HOME", Nauvisian.platform.home_directory / ".cache")) / "nauvisian"
end

Instance Method Details

#fetch(key) ⇒ Object



25
26
27
28
29
30
# File 'lib/nauvisian/cache/file_system.rb', line 25

def fetch(key)
  path = generate_path(key)
  return path.binread if path.exist? && !stale?(path, Time.now)

  yield.tap {|content| store(path, content) }
end