Class: Sxn::Config::ConfigCache

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/config/config_cache.rb

Overview

Caches discovered configurations with TTL and file change invalidation

Features:

  • Time-based cache expiration (TTL)

  • File modification time checking for cache invalidation

  • Atomic cache file operations

  • Cache storage in .sxn/.cache/config.json

Constant Summary collapse

CACHE_DIR =
".sxn/.cache"
CACHE_FILE =
"config.json"
DEFAULT_TTL =

5 minutes in seconds

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: nil, ttl: DEFAULT_TTL) ⇒ ConfigCache

Returns a new instance of ConfigCache.



23
24
25
26
27
28
29
# File 'lib/sxn/config/config_cache.rb', line 23

def initialize(cache_dir: nil, ttl: DEFAULT_TTL)
  @cache_dir = cache_dir || File.join(Dir.pwd, CACHE_DIR)
  @cache_file_path = File.join(@cache_dir, CACHE_FILE)
  @ttl = ttl
  @write_mutex = Mutex.new
  ensure_cache_directory
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



21
22
23
# File 'lib/sxn/config/config_cache.rb', line 21

def cache_dir
  @cache_dir
end

#cache_file_pathObject (readonly)

Returns the value of attribute cache_file_path.



21
22
23
# File 'lib/sxn/config/config_cache.rb', line 21

def cache_file_path
  @cache_file_path
end

#ttlObject (readonly)

Returns the value of attribute ttl.



21
22
23
# File 'lib/sxn/config/config_cache.rb', line 21

def ttl
  @ttl
end

Instance Method Details

#get(config_files) ⇒ Hash?

Get cached configuration or nil if invalid/missing

Parameters:

  • config_files (Array<String>)

    List of config file paths to check

Returns:

  • (Hash, nil)

    Cached configuration or nil



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sxn/config/config_cache.rb', line 34

def get(config_files)
  return nil unless cache_exists?

  cache_data = load_cache
  return nil unless cache_data

  return nil unless cache_valid?(cache_data, config_files)

  cache_data["config"]
rescue StandardError => e
  warn "Warning: Failed to load cache: #{e.message}"
  nil
end

#invalidateBoolean

Invalidate the cache by removing the cache file

Returns:

  • (Boolean)

    Success status



69
70
71
72
73
74
75
76
77
# File 'lib/sxn/config/config_cache.rb', line 69

def invalidate
  return true unless cache_exists?

  File.delete(cache_file_path)
  true
rescue StandardError => e
  warn "Warning: Failed to invalidate cache: #{e.message}"
  false
end

#set(config, config_files) ⇒ Boolean

Store configuration in cache

Parameters:

  • config (Hash)

    Configuration to cache

  • config_files (Array<String>)

    List of config file paths

Returns:

  • (Boolean)

    Success status



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sxn/config/config_cache.rb', line 52

def set(config, config_files)
  cache_data = {
    "config" => config,
    "cached_at" => Time.now.to_f,
    "config_files" => (config_files),
    "cache_version" => 1
  }

  save_cache(cache_data)
  true
rescue StandardError => e
  warn "Warning: Failed to save cache: #{e.message}"
  false
end

#stats(config_files = []) ⇒ Hash

Get cache statistics

Parameters:

  • config_files (Array<String>) (defaults to: [])

    List of config file paths to check validity against

Returns:

  • (Hash)

    Cache statistics



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sxn/config/config_cache.rb', line 96

def stats(config_files = [])
  return { exists: false, valid: false } unless cache_exists?

  cache_data = load_cache
  return { exists: true, valid: false, invalid: true } unless cache_data

  is_valid = cache_valid?(cache_data, config_files || [])

  {
    exists: true,
    valid: is_valid,
    cached_at: Time.at(cache_data["cached_at"]),
    age_seconds: Time.now.to_f - cache_data["cached_at"],
    file_count: cache_data["config_files"]&.length || 0,
    cache_version: cache_data["cache_version"]
  }
rescue StandardError
  { exists: true, valid: false, invalid: true, error: true }
end

#valid?(config_files) ⇒ Boolean

Check if cache is valid without loading the full configuration

Parameters:

  • config_files (Array<String>)

    List of config file paths to check

Returns:

  • (Boolean)

    True if cache is valid



82
83
84
85
86
87
88
89
90
91
# File 'lib/sxn/config/config_cache.rb', line 82

def valid?(config_files)
  return false unless cache_exists?

  cache_data = load_cache
  return false unless cache_data

  cache_valid?(cache_data, config_files)
rescue StandardError
  false
end