Class: Makit::Commands::Middleware::Cache

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/commands/middleware/cache.rb

Overview

Caching middleware for command execution results

This middleware provides caching functionality for command execution, allowing commands to be cached based on their content and timestamp.

Examples:

Basic usage

runner = Commands::Runner.new(middleware: [Cache.new])
result = runner.execute("git --version")

With custom cache directory

cache = Cache.new(cache_dir: "custom/cache")
runner = Commands::Runner.new(middleware: [cache])

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#name

Constructor Details

#initialize(cache_dir: nil, enabled: true, **options) ⇒ Cache

Initialize caching middleware

Parameters:

  • cache_dir (String) (defaults to: nil)

    directory to store cache files

  • enabled (Boolean) (defaults to: true)

    whether caching is enabled

  • options (Hash)

    additional options



35
36
37
38
39
40
41
42
# File 'lib/makit/commands/middleware/cache.rb', line 35

def initialize(cache_dir: nil, enabled: true, **options)
  @cache_dir = cache_dir || default_cache_dir
  @enabled = enabled
  @options = options

  # Ensure cache directory exists
  FileUtils.mkdir_p(@cache_dir) if @enabled
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



24
25
26
# File 'lib/makit/commands/middleware/cache.rb', line 24

def cache_dir
  @cache_dir
end

#enabledObject (readonly)

Returns the value of attribute enabled.



28
29
30
# File 'lib/makit/commands/middleware/cache.rb', line 28

def enabled
  @enabled
end

Instance Method Details

#applicable?(request) ⇒ Boolean

Check if this middleware applies to the given request

Parameters:

  • request (Request)

    command request

Returns:

  • (Boolean)

    true if caching should be applied



48
49
50
# File 'lib/makit/commands/middleware/cache.rb', line 48

def applicable?(request)
  @enabled && request.[:cache_key].present?
end

#call(request) {|Request| ... } ⇒ Result

Execute the middleware chain with caching

Parameters:

  • request (Request)

    command request

Yields:

  • (Request)

    yields to next middleware or execution

Returns:

  • (Result)

    execution result



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/makit/commands/middleware/cache.rb', line 57

def call(request, &block)
  return block.call(request) unless applicable?(request)

  cache_key = request.[:cache_key]
  cache_timestamp = request.[:cache_timestamp]
  cache_file = get_cache_file(cache_key)

  # Check if we have a valid cached result
  if cache_file && File.exist?(cache_file) && cache_timestamp
    cached_result = load_cached_result(cache_file, cache_timestamp)
    return cached_result if cached_result
  end

  # Execute the command
  result = block.call(request)

  # Cache the result if successful
  cache_result(cache_file, result) if result.success?

  result
end

#configHash

Get middleware configuration

Returns:

  • (Hash)

    configuration hash



82
83
84
85
86
87
88
# File 'lib/makit/commands/middleware/cache.rb', line 82

def config
  {
    cache_dir: @cache_dir,
    enabled: @enabled,
    options: @options,
  }
end