Class: VagrantPlugins::OrbStack::Util::StateCache

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-orbstack/util/state_cache.rb

Overview

TTL-based cache utility for machine state queries.

This class provides a simple time-to-live (TTL) cache to reduce redundant CLI calls when querying machine state. State queries are cached with a configurable TTL (default 5 seconds), and automatically expire when the TTL is exceeded.

The cache is designed for Vagrant's single-threaded environment and does not implement thread-safety mechanisms.

Examples:

Basic usage with default TTL

cache = StateCache.new
cache.set('machine-id', state)
cached_state = cache.get('machine-id')  # Returns state

Custom TTL

cache = StateCache.new(ttl: 10)  # 10-second cache
cache.set('key', 'value')
# 11 seconds later...
cache.get('key')  # Returns nil (expired)

Cache invalidation

cache.invalidate('key')       # Remove specific key
cache.invalidate_all          # Clear entire cache

Constant Summary collapse

DEFAULT_TTL =

Default TTL for cached entries (in seconds)

5

Instance Method Summary collapse

Constructor Details

#initialize(ttl: DEFAULT_TTL) ⇒ StateCache

Initialize a new state cache.

Parameters:

  • ttl (Integer) (defaults to: DEFAULT_TTL)

    Time-to-live for cached entries in seconds (default: 5)



38
39
40
41
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 38

def initialize(ttl: DEFAULT_TTL)
  @ttl = ttl
  @cache = {}
end

Instance Method Details

#get(key) ⇒ Object?

Retrieve a cached value.

Returns the cached value if it exists and has not expired. Returns nil if the key doesn't exist or if the cached entry has exceeded its TTL.

Parameters:

  • key (String)

    The cache key

Returns:

  • (Object, nil)

    The cached value if valid, nil if missing or expired



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 51

def get(key)
  entry = @cache[key]
  return nil unless entry

  # Check if entry has expired
  if Time.now - entry[:timestamp] > @ttl
    # Entry expired, remove it and return nil
    @cache.delete(key)
    return nil
  end

  entry[:value]
end

#invalidate(key) ⇒ void

This method returns an undefined value.

Invalidate a specific cache entry.

Removes the specified key from the cache. This is a no-op if the key doesn't exist.

Parameters:

  • key (String)

    The cache key to remove



89
90
91
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 89

def invalidate(key)
  @cache.delete(key)
end

#invalidate_allvoid

This method returns an undefined value.

Clear all cache entries.

Removes all cached entries. This is useful when you need to ensure fresh data is retrieved on the next query.



100
101
102
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 100

def invalidate_all
  @cache.clear
end

#set(key, value) ⇒ void

This method returns an undefined value.

Store a value in the cache.

Stores the value with the current timestamp. If the key already exists, its value and timestamp are overwritten.

Parameters:

  • key (String)

    The cache key

  • value (Object)

    The value to cache (can be any object, including nil)



74
75
76
77
78
79
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 74

def set(key, value)
  @cache[key] = {
    value: value,
    timestamp: Time.now
  }
end