Class: Rack::AI::Features::Caching

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ai/features/caching.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, config) ⇒ Caching

Returns a new instance of Caching.



12
13
14
15
16
17
# File 'lib/rack/ai/features/caching.rb', line 12

def initialize(provider, config)
  @name = :caching
  @provider = provider
  @config = config
  @redis = build_redis_client if @config.config.caching.predictive_enabled
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/rack/ai/features/caching.rb', line 10

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/rack/ai/features/caching.rb', line 10

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



10
11
12
# File 'lib/rack/ai/features/caching.rb', line 10

def provider
  @provider
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rack/ai/features/caching.rb', line 19

def enabled?
  @config.feature_enabled?(:caching) && @config.config.cache_enabled
end

#process_request(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/ai/features/caching.rb', line 27

def process_request(env)
  return { processed: false, reason: "disabled" } unless enabled?

  cache_key = generate_cache_key(env)
  
  # Check if we have cached AI analysis for this request pattern
  cached_result = get_cached_analysis(cache_key)
  if cached_result
    return {
      cache_hit: true,
      cached_result: cached_result,
      feature: @name,
      timestamp: Time.now.iso8601
    }
  end

  # Analyze request patterns for predictive caching
  request_pattern = extract_request_pattern(env)
  pattern_analysis = analyze_access_patterns(request_pattern)
  
  result = {
    cache_hit: false,
    pattern_analysis: pattern_analysis,
    should_prefetch: should_prefetch?(pattern_analysis),
    feature: @name,
    timestamp: Time.now.iso8601
  }

  # Store analysis for future use
  cache_analysis(cache_key, result)
  
  # Trigger prefetching if recommended
  if result[:should_prefetch]
    schedule_prefetch(request_pattern)
  end

  result
end

#process_response(env, status, headers, body) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rack/ai/features/caching.rb', line 66

def process_response(env, status, headers, body)
  return { processed: false, reason: "disabled" } unless enabled?

  # Analyze response for caching recommendations
  response_analysis = analyze_response_cachability(env, status, headers, body)
  
  # Update access patterns
  update_access_patterns(env, status)

  {
    response_analysis: response_analysis,
    feature: "#{@name}_response",
    timestamp: Time.now.iso8601
  }
end

#process_response?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rack/ai/features/caching.rb', line 23

def process_response?
  true
end