Class: Flycal::Cli::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/flycal/cli/file_cache.rb

Overview

File-backed cache under ~/.flycal/cache (stores rendered text/json strings).

Class Method Summary collapse

Class Method Details

.delete(cache_key) ⇒ Object



33
34
35
36
# File 'lib/flycal/cli/file_cache.rb', line 33

def delete(cache_key)
  path = path_for(cache_key)
  File.delete(path) if File.file?(path)
end

.dirObject



10
11
12
# File 'lib/flycal/cli/file_cache.rb', line 10

def dir
  File.join(Config.config_dir, "cache")
end

.path_for(cache_key) ⇒ Object



14
15
16
# File 'lib/flycal/cli/file_cache.rb', line 14

def path_for(cache_key)
  File.join(dir, sanitize(cache_key))
end

.read(cache_key, ttl_minutes:) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/flycal/cli/file_cache.rb', line 18

def read(cache_key, ttl_minutes:)
  path = path_for(cache_key)
  return nil unless File.file?(path)

  age = Time.now - File.mtime(path)
  return nil if age > ttl_minutes.to_f * 60

  File.read(path)
end

.sanitize(cache_key) ⇒ Object



38
39
40
# File 'lib/flycal/cli/file_cache.rb', line 38

def sanitize(cache_key)
  cache_key.to_s.gsub(/[^a-zA-Z0-9._-]/, "_")
end

.write(cache_key, content) ⇒ Object



28
29
30
31
# File 'lib/flycal/cli/file_cache.rb', line 28

def write(cache_key, content)
  FileUtils.mkdir_p(dir)
  File.write(path_for(cache_key), content.to_s)
end