Class: Aidp::Interfaces::CachingBinaryChecker
- Inherits:
-
Object
- Object
- Aidp::Interfaces::CachingBinaryChecker
- Includes:
- BinaryCheckerInterface
- Defined in:
- lib/aidp/interfaces/binary_checker_interface.rb
Overview
CachingBinaryChecker wraps another checker with TTL-based caching. Reduces filesystem checks for frequently-queried binaries.
Constant Summary collapse
- DEFAULT_TTL =
Default cache TTL in seconds (5 minutes)
300
Instance Method Summary collapse
- #available?(binary_name) ⇒ Boolean
-
#clear!(binary_name) ⇒ void
Clear cached result for a specific binary.
-
#clear_cache! ⇒ void
Clear all cached results.
-
#initialize(checker, ttl: DEFAULT_TTL) ⇒ CachingBinaryChecker
constructor
A new instance of CachingBinaryChecker.
- #path_for(binary_name) ⇒ Object
Constructor Details
#initialize(checker, ttl: DEFAULT_TTL) ⇒ CachingBinaryChecker
Returns a new instance of CachingBinaryChecker.
157 158 159 160 161 162 |
# File 'lib/aidp/interfaces/binary_checker_interface.rb', line 157 def initialize(checker, ttl: DEFAULT_TTL) @checker = checker @ttl = ttl @cache = {} @path_cache = {} end |
Instance Method Details
#available?(binary_name) ⇒ Boolean
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/aidp/interfaces/binary_checker_interface.rb', line 164 def available?(binary_name) key = binary_name.to_s cached = @cache[key] if cached && !expired?(cached[:timestamp]) return cached[:value] end result = @checker.available?(binary_name) @cache[key] = {value: result, timestamp: Time.now} result end |
#clear!(binary_name) ⇒ void
This method returns an undefined value.
Clear cached result for a specific binary.
200 201 202 203 204 |
# File 'lib/aidp/interfaces/binary_checker_interface.rb', line 200 def clear!(binary_name) key = binary_name.to_s @cache.delete(key) @path_cache.delete(key) end |
#clear_cache! ⇒ void
This method returns an undefined value.
Clear all cached results.
192 193 194 195 |
# File 'lib/aidp/interfaces/binary_checker_interface.rb', line 192 def clear_cache! @cache.clear @path_cache.clear end |
#path_for(binary_name) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/aidp/interfaces/binary_checker_interface.rb', line 177 def path_for(binary_name) key = binary_name.to_s cached = @path_cache[key] if cached && !expired?(cached[:timestamp]) return cached[:value] end result = @checker.path_for(binary_name) @path_cache[key] = {value: result, timestamp: Time.now} result end |