Class: Profiler::MCP::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/file_cache.rb

Class Method Summary collapse

Class Method Details

.base_dirObject



8
9
10
# File 'lib/profiler/mcp/file_cache.rb', line 8

def self.base_dir
  Profiler.configuration.tmp_path.to_s
end

.cleanup(max_age: 3600) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/profiler/mcp/file_cache.rb', line 24

def self.cleanup(max_age: 3600)
  bd = base_dir
  return unless Dir.exist?(bd)

  Dir.glob(File.join(bd, "*")).each do |dir|
    FileUtils.rm_rf(dir) if File.directory?(dir) && (Time.now - File.mtime(dir)) > max_age
  end
end

.save(token, name, content) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/profiler/mcp/file_cache.rb', line 12

def self.save(token, name, content)
  cleanup if rand < 0.05

  dir = File.join(base_dir, token)
  FileUtils.mkdir_p(dir)
  path = File.join(dir, name)
  File.write(path, content)
  path
rescue Errno::EACCES, Errno::EROFS
  nil
end