Class: MonkeysPaw::CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/monkeyspaw/cache_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CacheManager

Returns a new instance of CacheManager.



8
9
10
11
12
# File 'lib/monkeyspaw/cache_manager.rb', line 8

def initialize(app)
  @app = app
  @cache_dir = app.root.join('.monkeyspaw_cache')
  @cache_dir.mkpath unless @cache_dir.exist?
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/monkeyspaw/cache_manager.rb', line 6

def app
  @app
end

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



6
7
8
# File 'lib/monkeyspaw/cache_manager.rb', line 6

def cache_dir
  @cache_dir
end

Instance Method Details

#cache_page(path, prompt_file, html_content) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/monkeyspaw/cache_manager.rb', line 30

def cache_page(path, prompt_file, html_content)
  cache_key = generate_cache_key(path, prompt_file)
  cache_file = cache_dir.join("#{cache_key}.html")

  # Save the HTML content
  File.write(cache_file, html_content)

  # Save dependency information
  save_dependencies(path, prompt_file)

  puts "Cached page #{path}"
end

#clear_cacheObject



43
44
45
46
47
# File 'lib/monkeyspaw/cache_manager.rb', line 43

def clear_cache
  FileUtils.rm_rf(cache_dir)
  cache_dir.mkpath
  puts "Cache cleared"
end

#get_cached_page(path, prompt_file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/monkeyspaw/cache_manager.rb', line 14

def get_cached_page(path, prompt_file)
  cache_key = generate_cache_key(path, prompt_file)
  cache_file = cache_dir.join("#{cache_key}.html")

  return nil unless cache_file.exist?

  # If the cache exists, check if any dependencies have changed
  if dependencies_changed?(path, prompt_file)
    puts "Dependencies changed for #{path}, regenerating..."
    return nil
  end

  puts "Using cached version of #{path}"
  File.read(cache_file)
end