Class: Tasker::Types::CacheConfig
- Inherits:
-
BaseConfig
- Object
- Dry::Struct
- BaseConfig
- Tasker::Types::CacheConfig
- Defined in:
- lib/tasker/types/cache_config.rb
Overview
Configuration type for intelligent cache strategy settings
This configuration provides strategic control over cache behavior while maintaining consistent infrastructure naming through constants.
Strategic Design:
- CONFIGURABLE: Algorithm parameters that vary by workload characteristics
- CONFIGURABLE: Performance thresholds that vary by deployment environment
- CONSTANTS: Infrastructure naming for consistency across deployments
Instance Attribute Summary collapse
-
#access_frequency_decay_rate ⇒ Float
readonly
Decay rate (default: 0.95).
-
#adaptive_calculation_interval ⇒ Integer
readonly
Calculation interval in seconds (default: 30).
-
#adaptive_ttl_enabled ⇒ Boolean
readonly
Whether adaptive TTL is enabled (default: true).
-
#cache_pressure_threshold ⇒ Float
readonly
Pressure threshold (default: 0.8).
-
#dashboard_cache_ttl ⇒ Integer
readonly
Dashboard TTL in seconds (default: 120).
-
#default_ttl ⇒ Integer
readonly
Default TTL in seconds (default: 300).
-
#hit_rate_smoothing_factor ⇒ Float
readonly
Smoothing factor (default: 0.9).
-
#max_adaptive_ttl ⇒ Integer
readonly
Maximum TTL in seconds (default: 3600).
-
#min_adaptive_ttl ⇒ Integer
readonly
Minimum TTL in seconds (default: 30).
-
#performance_tracking_enabled ⇒ Boolean
readonly
Whether performance tracking is enabled (default: true).
Instance Method Summary collapse
-
#cache_under_pressure?(utilization) ⇒ Boolean
Check if cache is under pressure.
-
#calculate_adaptive_ttl(base_ttl, hit_rate: 0.0, generation_time: 0.0, access_frequency: 0) ⇒ Integer
Calculate adaptive TTL based on performance data.
-
#validate! ⇒ Object
Validate entire configuration.
-
#validate_algorithm_parameters ⇒ Array<String>
Validate algorithm parameters.
-
#validate_ttl_configuration ⇒ Array<String>
Validate TTL configuration.
Methods inherited from BaseConfig
Constructor Details
This class inherits a constructor from Tasker::Types::BaseConfig
Instance Attribute Details
#access_frequency_decay_rate ⇒ Float (readonly)
Returns Decay rate (default: 0.95).
99 |
# File 'lib/tasker/types/cache_config.rb', line 99 attribute :access_frequency_decay_rate, Types::Float.default(0.95) |
#adaptive_calculation_interval ⇒ Integer (readonly)
Returns Calculation interval in seconds (default: 30).
144 |
# File 'lib/tasker/types/cache_config.rb', line 144 attribute :adaptive_calculation_interval, Types::Integer.default(30) |
#adaptive_ttl_enabled ⇒ Boolean (readonly)
Returns Whether adaptive TTL is enabled (default: true).
70 |
# File 'lib/tasker/types/cache_config.rb', line 70 attribute :adaptive_ttl_enabled, Types::Bool.default(true) |
#cache_pressure_threshold ⇒ Float (readonly)
Returns Pressure threshold (default: 0.8).
135 |
# File 'lib/tasker/types/cache_config.rb', line 135 attribute :cache_pressure_threshold, Types::Float.default(0.8) |
#dashboard_cache_ttl ⇒ Integer (readonly)
Returns Dashboard TTL in seconds (default: 120).
126 |
# File 'lib/tasker/types/cache_config.rb', line 126 attribute :dashboard_cache_ttl, Types::Integer.default(120) |
#default_ttl ⇒ Integer (readonly)
Returns Default TTL in seconds (default: 300).
61 |
# File 'lib/tasker/types/cache_config.rb', line 61 attribute :default_ttl, Types::Integer.default(300) |
#hit_rate_smoothing_factor ⇒ Float (readonly)
Returns Smoothing factor (default: 0.9).
89 |
# File 'lib/tasker/types/cache_config.rb', line 89 attribute :hit_rate_smoothing_factor, Types::Float.default(0.9) |
#max_adaptive_ttl ⇒ Integer (readonly)
Returns Maximum TTL in seconds (default: 3600).
117 |
# File 'lib/tasker/types/cache_config.rb', line 117 attribute :max_adaptive_ttl, Types::Integer.default(3600) |
#min_adaptive_ttl ⇒ Integer (readonly)
Returns Minimum TTL in seconds (default: 30).
108 |
# File 'lib/tasker/types/cache_config.rb', line 108 attribute :min_adaptive_ttl, Types::Integer.default(30) |
#performance_tracking_enabled ⇒ Boolean (readonly)
Returns Whether performance tracking is enabled (default: true).
79 |
# File 'lib/tasker/types/cache_config.rb', line 79 attribute :performance_tracking_enabled, Types::Bool.default(true) |
Instance Method Details
#cache_under_pressure?(utilization) ⇒ Boolean
Check if cache is under pressure
252 253 254 |
# File 'lib/tasker/types/cache_config.rb', line 252 def cache_under_pressure?(utilization) utilization >= cache_pressure_threshold end |
#calculate_adaptive_ttl(base_ttl, hit_rate: 0.0, generation_time: 0.0, access_frequency: 0) ⇒ Integer
Calculate adaptive TTL based on performance data
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/tasker/types/cache_config.rb', line 217 def calculate_adaptive_ttl(base_ttl, hit_rate: 0.0, generation_time: 0.0, access_frequency: 0) return base_ttl unless adaptive_ttl_enabled # Start with base TTL as float for more precise calculations adaptive_ttl = base_ttl.to_f # Adjust based on hit rate (only if we have meaningful data) if hit_rate > 0.8 adaptive_ttl *= 1.5 # High hit rate, extend TTL elsif hit_rate > 0.0 && hit_rate < 0.3 adaptive_ttl *= 0.7 # Low hit rate, reduce TTL end # Adjust based on generation time (only if we have meaningful data) if generation_time > 1.0 adaptive_ttl *= 1.3 # Expensive to generate, cache longer elsif generation_time > 0.0 && generation_time < 0.1 adaptive_ttl *= 0.8 # Cheap to generate, cache shorter end # Adjust based on access frequency (only if we have meaningful data) if access_frequency > 100 adaptive_ttl *= 1.2 # Frequently accessed, cache longer elsif access_frequency.positive? && access_frequency < 5 adaptive_ttl *= 0.9 # Rarely accessed, cache shorter end # Convert to integer and apply bounds adaptive_ttl.to_i.clamp(min_adaptive_ttl, max_adaptive_ttl) end |
#validate! ⇒ Object
Validate entire configuration
Runs all validation checks and raises if any errors found
202 203 204 205 206 207 208 |
# File 'lib/tasker/types/cache_config.rb', line 202 def validate! errors = validate_ttl_configuration + validate_algorithm_parameters return if errors.empty? raise ArgumentError, "Invalid cache configuration: #{errors.join(', ')}" end |
#validate_algorithm_parameters ⇒ Array<String>
Validate algorithm parameters
Ensures smoothing factors and thresholds are within valid ranges
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/tasker/types/cache_config.rb', line 176 def validate_algorithm_parameters errors = [] unless (0.0..1.0).cover?(hit_rate_smoothing_factor) errors << "hit_rate_smoothing_factor must be between 0.0 and 1.0 (got: #{hit_rate_smoothing_factor})" end unless (0.0..1.0).cover?(access_frequency_decay_rate) errors << "access_frequency_decay_rate must be between 0.0 and 1.0 (got: #{access_frequency_decay_rate})" end unless (0.0..1.0).cover?(cache_pressure_threshold) errors << "cache_pressure_threshold must be between 0.0 and 1.0 (got: #{cache_pressure_threshold})" end if adaptive_calculation_interval <= 0 errors << "adaptive_calculation_interval must be positive (got: #{adaptive_calculation_interval})" end errors end |
#validate_ttl_configuration ⇒ Array<String>
Validate TTL configuration
Ensures TTL values are positive and logical
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/tasker/types/cache_config.rb', line 154 def validate_ttl_configuration errors = [] errors << "default_ttl must be positive (got: #{default_ttl})" if default_ttl <= 0 errors << "min_adaptive_ttl must be positive (got: #{min_adaptive_ttl})" if min_adaptive_ttl <= 0 errors << "max_adaptive_ttl must be positive (got: #{max_adaptive_ttl})" if max_adaptive_ttl <= 0 if min_adaptive_ttl >= max_adaptive_ttl errors << "min_adaptive_ttl (#{min_adaptive_ttl}) must be less than max_adaptive_ttl (#{max_adaptive_ttl})" end errors << "dashboard_cache_ttl must be positive (got: #{dashboard_cache_ttl})" if dashboard_cache_ttl <= 0 errors end |