Class: Tasker::Types::CacheConfig

Inherits:
BaseConfig
  • Object
show all
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

Examples:

Basic usage

config = CacheConfig.new(
  default_ttl: 600,                    # 10 minutes for stable data
  hit_rate_smoothing_factor: 0.8       # Less aggressive smoothing
)

Environment-specific tuning

# Development environment - shorter TTLs for rapid iteration
config = CacheConfig.new(
  default_ttl: 120,                    # 2 minutes
  adaptive_ttl_enabled: false,         # Disable complexity
  performance_tracking_enabled: false  # Reduce overhead
)

# Production environment - optimized for performance
config = CacheConfig.new(
  default_ttl: 300,                    # 5 minutes
  hit_rate_smoothing_factor: 0.95,     # Aggressive smoothing
  access_frequency_decay_rate: 0.98,   # Slow decay
  adaptive_ttl_enabled: true,          # Enable intelligence
  performance_tracking_enabled: true   # Full observability
)

# High-performance environment - maximum efficiency
config = CacheConfig.new(
  default_ttl: 600,                    # 10 minutes
  min_adaptive_ttl: 60,                # 1 minute minimum
  max_adaptive_ttl: 7200,              # 2 hours maximum
  cache_pressure_threshold: 0.9,       # High threshold
  adaptive_calculation_interval: 15    # Frequent recalculation
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseConfig

#initialize

Constructor Details

This class inherits a constructor from Tasker::Types::BaseConfig

Instance Attribute Details

#access_frequency_decay_rateFloat (readonly)

Returns Decay rate (default: 0.95).

Returns:

  • (Float)

    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_intervalInteger (readonly)

Returns Calculation interval in seconds (default: 30).

Returns:

  • (Integer)

    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_enabledBoolean (readonly)

Returns Whether adaptive TTL is enabled (default: true).

Returns:

  • (Boolean)

    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_thresholdFloat (readonly)

Returns Pressure threshold (default: 0.8).

Returns:

  • (Float)

    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_ttlInteger (readonly)

Returns Dashboard TTL in seconds (default: 120).

Returns:

  • (Integer)

    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_ttlInteger (readonly)

Returns Default TTL in seconds (default: 300).

Returns:

  • (Integer)

    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_factorFloat (readonly)

Returns Smoothing factor (default: 0.9).

Returns:

  • (Float)

    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_ttlInteger (readonly)

Returns Maximum TTL in seconds (default: 3600).

Returns:

  • (Integer)

    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_ttlInteger (readonly)

Returns Minimum TTL in seconds (default: 30).

Returns:

  • (Integer)

    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_enabledBoolean (readonly)

Returns Whether performance tracking is enabled (default: true).

Returns:

  • (Boolean)

    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

Parameters:

  • utilization (Float)

    Current cache utilization (0.0-1.0)

Returns:

  • (Boolean)

    Whether 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

Parameters:

  • base_ttl (Integer)

    Base TTL to adjust

  • hit_rate (Float) (defaults to: 0.0)

    Current cache hit rate (0.0-1.0)

  • generation_time (Float) (defaults to: 0.0)

    Average generation time in seconds

  • access_frequency (Integer) (defaults to: 0)

    Access frequency count

Returns:

  • (Integer)

    Calculated adaptive TTL



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

Raises:

  • (ArgumentError)

    If configuration is invalid



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_parametersArray<String>

Validate algorithm parameters

Ensures smoothing factors and thresholds are within valid ranges

Returns:

  • (Array<String>)

    Validation errors (empty if valid)



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_configurationArray<String>

Validate TTL configuration

Ensures TTL values are positive and logical

Returns:

  • (Array<String>)

    Validation errors (empty if valid)



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