Class: Profiler::MCP::FileCache

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

Constant Summary collapse

BASE_DIR =
"/tmp/rails-profiler"

Class Method Summary collapse

Class Method Details

.cleanup(max_age: 3600) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/profiler/mcp/file_cache.rb', line 22

def self.cleanup(max_age: 3600)
  return unless Dir.exist?(BASE_DIR)

  Dir.glob(File.join(BASE_DIR, "*")).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



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

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