Class: Makit::Commands::Middleware::Cache
- 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.
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
readonly
Returns the value of attribute cache_dir.
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
Instance Method Summary collapse
-
#applicable?(request) ⇒ Boolean
Check if this middleware applies to the given request.
-
#call(request) {|Request| ... } ⇒ Result
Execute the middleware chain with caching.
-
#config ⇒ Hash
Get middleware configuration.
-
#initialize(cache_dir: nil, enabled: true, **options) ⇒ Cache
constructor
Initialize caching middleware.
Methods inherited from Base
Constructor Details
#initialize(cache_dir: nil, enabled: true, **options) ⇒ Cache
Initialize caching middleware
35 36 37 38 39 40 41 42 |
# File 'lib/makit/commands/middleware/cache.rb', line 35 def initialize(cache_dir: nil, enabled: true, **) @cache_dir = cache_dir || default_cache_dir @enabled = enabled @options = # Ensure cache directory exists FileUtils.mkdir_p(@cache_dir) if @enabled end |
Instance Attribute Details
#cache_dir ⇒ Object (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 |
#enabled ⇒ Object (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
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
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] = 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) && cached_result = load_cached_result(cache_file, ) 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 |
#config ⇒ Hash
Get middleware configuration
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 |