Class: Yard::Lint::CommandCache

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/lint/command_cache.rb

Overview

Cache for YARD command executions to avoid running identical commands multiple times This provides a transparent optimization layer - validators don’t need to know about it

Instance Method Summary collapse

Constructor Details

#initializeCommandCache

Returns a new instance of CommandCache.



8
9
10
11
12
# File 'lib/yard/lint/command_cache.rb', line 8

def initialize
  @cache = {}
  @hits = 0
  @misses = 0
end

Instance Method Details

#execute(command_string) ⇒ Hash

Note:

Returns a deep clone to prevent validators from modifying cached data

Execute a command through the cache If the command has been executed before, return cached result Otherwise execute and cache the result

Parameters:

  • command_string (String)

    the shell command to execute

Returns:

  • (Hash)

    hash with stdout, stderr, exit_code keys



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yard/lint/command_cache.rb', line 20

def execute(command_string)
  cache_key = generate_cache_key(command_string)

  if @cache.key?(cache_key)
    @hits += 1
    deep_clone(@cache[cache_key])
  else
    @misses += 1
    result = execute_command(command_string)
    @cache[cache_key] = deep_clone(result)
    result
  end
end

#statsHash

Get cache statistics

Returns:

  • (Hash)

    hash with hits, misses, and total executions



36
37
38
39
40
41
42
43
# File 'lib/yard/lint/command_cache.rb', line 36

def stats
  {
    hits: @hits,
    misses: @misses,
    total: @hits + @misses,
    saved_executions: @hits
  }
end