Class: Tasker::Telemetry::IntelligentCacheManager

Inherits:
Object
  • Object
show all
Includes:
Concerns::StructuredLogging
Defined in:
lib/tasker/telemetry/intelligent_cache_manager.rb

Overview

Intelligent cache management with adaptive TTL and distributed coordination

This class implements a strategic constants vs configuration approach:

  • CONSTANTS: Infrastructure naming for consistency across deployments
  • CONFIGURABLE: Algorithm parameters for workload-specific tuning

Phase 2.1 Enhancement: Distributed Coordination Leverages proven MetricsBackend patterns for multi-container coordination:

  • Instance ID generation (hostname-pid pattern)
  • Cache capability detection with adaptive strategies
  • Multi-strategy coordination (distributed_atomic, distributed_basic, local_only)
  • Atomic operations with fallback strategies

Features:

  • Kubernetes-ready distributed cache coordination
  • Rails.cache abstraction (works with Redis, Memcached, File, Memory)
  • Configurable smoothing factors for different workload patterns
  • Comprehensive performance tracking with structured logging

Examples:

Basic usage

manager = IntelligentCacheManager.new
result = manager.intelligent_fetch('expensive_key') { expensive_computation }

With custom configuration

config = Tasker::Types::CacheConfig.new(hit_rate_smoothing_factor: 0.95)
manager = IntelligentCacheManager.new(config)
result = manager.intelligent_fetch('key', base_ttl: 600) { data }

Constant Summary collapse

CACHE_PERFORMANCE_KEY_PREFIX =

✅ CONSTANTS: Infrastructure naming (consistency across deployments)

'tasker:cache_perf'
DASHBOARD_CACHE_KEY_PREFIX =
'tasker:dashboard'
WORKFLOW_CACHE_KEY_PREFIX =
'tasker:workflow'
PERFORMANCE_RETENTION_PERIOD =
1.hour
COORDINATION_RETRY_ATTEMPTS =
3
ATOMIC_OPERATION_TIMEOUT =

✅ CONSTANTS: Coordination strategy thresholds (algorithmic, not configurable)

5.seconds
BASIC_COORDINATION_TIMEOUT =
10.seconds
LOCAL_COORDINATION_TIMEOUT =
1.second

Constants included from Concerns::StructuredLogging

Concerns::StructuredLogging::CORRELATION_ID_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::StructuredLogging

#correlation_id, #correlation_id=, #log_exception, #log_orchestration_event, #log_performance_event, #log_step_event, #log_structured, #log_task_event, #with_correlation_id

Constructor Details

#initialize(config = nil) ⇒ IntelligentCacheManager

Initialize intelligent cache manager with distributed coordination

Parameters:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 53

def initialize(config = nil)
  @config = config || Tasker::Configuration.configuration.cache

  # Use unified cache strategy for capability detection
  @cache_strategy = Tasker::CacheStrategy.detect
  @instance_id = @cache_strategy.instance_id
  @coordination_strategy = @cache_strategy.coordination_mode

  # Extract capabilities for backward compatibility
  @cache_capabilities = @cache_strategy.export_capabilities
  @coordination_config = configure_coordination_parameters

  log_structured(:info, 'IntelligentCacheManager initialized',
                 coordination_strategy: @coordination_strategy,
                 cache_store: @cache_strategy.store_class_name,
                 instance_id: @instance_id,
                 adaptive_ttl_enabled: @config.adaptive_ttl_enabled,
                 performance_tracking_enabled: @config.performance_tracking_enabled)
end

Instance Attribute Details

#cache_capabilitiesObject (readonly)

Returns the value of attribute cache_capabilities.



48
49
50
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 48

def cache_capabilities
  @cache_capabilities
end

#configObject (readonly)

Returns the value of attribute config.



48
49
50
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 48

def config
  @config
end

#coordination_strategyObject (readonly)

Returns the value of attribute coordination_strategy.



48
49
50
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 48

def coordination_strategy
  @coordination_strategy
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



48
49
50
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 48

def instance_id
  @instance_id
end

Instance Method Details

#clear_performance_data(cache_key) ⇒ Boolean

Clear performance data for a specific cache key

Parameters:

  • cache_key (String)

    The cache key to clear performance data for

Returns:

  • (Boolean)

    Success status



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 97

def clear_performance_data(cache_key)
  performance_key = build_performance_key(cache_key)
  Rails.cache.delete(performance_key)

  log_structured(:debug, 'Cache performance data cleared',
                 cache_key: cache_key,
                 performance_key: performance_key,
                 coordination_strategy: @coordination_strategy)

  true
rescue StandardError => e
  log_structured(:error, 'Failed to clear cache performance data',
                 cache_key: cache_key,
                 error: e.message,
                 coordination_strategy: @coordination_strategy)
  false
end

#export_performance_metricsHash

Export cache performance metrics for observability

Returns:

  • (Hash)

    Performance metrics summary



118
119
120
121
122
123
124
125
126
127
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 118

def export_performance_metrics
  case @coordination_strategy
  when :distributed_atomic, :distributed_basic
    export_distributed_performance_metrics
  when :local_only
    export_local_performance_metrics
  else
    { strategy: @coordination_strategy, metrics: {} }
  end
end

#intelligent_fetch(cache_key, base_ttl: @config.default_ttl) { ... } ⇒ Object

Main intelligent cache method with distributed coordination

Parameters:

  • cache_key (String)

    The cache key

  • base_ttl (Integer) (defaults to: @config.default_ttl)

    Base TTL in seconds

Yields:

  • Block that generates the value if cache miss

Returns:

  • (Object)

    The cached or generated value



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tasker/telemetry/intelligent_cache_manager.rb', line 79

def intelligent_fetch(cache_key, base_ttl: @config.default_ttl, &)
  case @coordination_strategy
  when :distributed_atomic
    intelligent_fetch_with_atomic_coordination(cache_key, base_ttl, &)
  when :distributed_basic
    intelligent_fetch_with_basic_coordination(cache_key, base_ttl, &)
  when :local_only
    intelligent_fetch_with_local_tracking(cache_key, base_ttl, &)
  else
    # Fallback to basic Rails.cache behavior
    Rails.cache.fetch(cache_key, expires_in: base_ttl, &)
  end
end