Class: Makit::TestCache
- Inherits:
-
Object
- Object
- Makit::TestCache
- Defined in:
- lib/makit/test_cache.rb
Constant Summary collapse
- CACHE_BASE_DIR =
Cache configuration
File.join(Makit::Directories::PROJECT_ARTIFACTS, "test_cache").freeze
- CACHE_METADATA_FILE =
".cache_metadata.json"
- DEFAULT_TTL =
1 hour cache by default
3600
Class Method Summary collapse
-
.cache_directory(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL) ⇒ Object
Directory-based caching - checks if directory exists and is valid.
-
.cache_files(test_instance, cache_key, expected_files, dependencies: [], ttl: DEFAULT_TTL) ⇒ Object
File-based caching - checks if specific files exist and are valid.
-
.cache_stats ⇒ Object
Get cache statistics.
-
.clear_all ⇒ Object
Clear all test caches.
-
.clear_for_class(test_class) ⇒ Object
Clear caches for specific test class.
- .setup ⇒ Object
-
.with_cache(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL, &block) ⇒ Object
Main caching method - wraps expensive test operations.
Class Method Details
.cache_directory(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL) ⇒ Object
Directory-based caching - checks if directory exists and is valid
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/makit/test_cache.rb', line 41 def cache_directory(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL) return nil unless should_cache? cache_dir = cache_directory_for(test_instance, cache_key) = (cache_dir) if cache_valid?(cache_dir, , dependencies, ttl) (test_instance, cache_key, ) return cache_dir end # Prepare fresh cache directory cleanup_cache_directory(cache_dir) FileUtils.mkdir_p(cache_dir) cache_dir end |
.cache_files(test_instance, cache_key, expected_files, dependencies: [], ttl: DEFAULT_TTL) ⇒ Object
File-based caching - checks if specific files exist and are valid
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/makit/test_cache.rb', line 60 def cache_files(test_instance, cache_key, expected_files, dependencies: [], ttl: DEFAULT_TTL) return false unless should_cache? cache_dir = cache_directory_for(test_instance, cache_key) = (cache_dir) # Check if all expected files exist files_exist = expected_files.all? do |file| File.exist?(File.join(cache_dir, file)) end if files_exist && cache_valid?(cache_dir, , dependencies, ttl) (test_instance, cache_key, ) return true end false end |
.cache_stats ⇒ Object
Get cache statistics
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/makit/test_cache.rb', line 93 def cache_stats return { total_size: 0, cache_count: 0, cache_dirs: [] } unless Dir.exist?(CACHE_BASE_DIR) cache_dirs = Dir.glob(File.join(CACHE_BASE_DIR, "**", "*")).select { |d| Dir.exist?(d) } total_size = cache_dirs.sum { |dir| directory_size(dir) } { total_size: total_size, cache_count: cache_dirs.length, cache_dirs: cache_dirs.map { |d| d.sub("#{CACHE_BASE_DIR}/", "") }, } end |
.clear_all ⇒ Object
Clear all test caches
80 81 82 83 |
# File 'lib/makit/test_cache.rb', line 80 def clear_all FileUtils.rm_rf(CACHE_BASE_DIR) puts "🗑️ Cleared all test caches".colorize(:grey) end |
.clear_for_class(test_class) ⇒ Object
Clear caches for specific test class
86 87 88 89 90 |
# File 'lib/makit/test_cache.rb', line 86 def clear_for_class(test_class) class_cache_dir = File.join(CACHE_BASE_DIR, class_cache_key(test_class)) FileUtils.rm_rf(class_cache_dir) puts "🗑️ Cleared test caches for #{test_class}".colorize(:grey) end |
.setup ⇒ Object
11 12 13 |
# File 'lib/makit/test_cache.rb', line 11 def setup FileUtils.mkdir_p(CACHE_BASE_DIR) end |
.with_cache(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL, &block) ⇒ Object
Main caching method - wraps expensive test operations
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/makit/test_cache.rb', line 16 def with_cache(test_instance, cache_key, dependencies: [], ttl: DEFAULT_TTL, &block) return yield unless should_cache? cache_dir = cache_directory_for(test_instance, cache_key) = (cache_dir) if cache_valid?(cache_dir, , dependencies, ttl) (test_instance, cache_key, ) return :cached end # Cache miss or invalid - run the expensive operation cleanup_cache_directory(cache_dir) FileUtils.mkdir_p(cache_dir) # Execute the block in the cache directory context result = Dir.chdir(cache_dir, &block) # Write cache metadata (cache_dir, dependencies) result end |