Class: VagrantPlugins::OrbStack::Util::StateCache
- Inherits:
-
Object
- Object
- VagrantPlugins::OrbStack::Util::StateCache
- 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.
Constant Summary collapse
- DEFAULT_TTL =
Default TTL for cached entries (in seconds)
5
Instance Method Summary collapse
-
#get(key) ⇒ Object?
Retrieve a cached value.
-
#initialize(ttl: DEFAULT_TTL) ⇒ StateCache
constructor
Initialize a new state cache.
-
#invalidate(key) ⇒ void
Invalidate a specific cache entry.
-
#invalidate_all ⇒ void
Clear all cache entries.
-
#set(key, value) ⇒ void
Store a value in the cache.
Constructor Details
#initialize(ttl: DEFAULT_TTL) ⇒ StateCache
Initialize a new state cache.
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.
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.
89 90 91 |
# File 'lib/vagrant-orbstack/util/state_cache.rb', line 89 def invalidate(key) @cache.delete(key) end |
#invalidate_all ⇒ void
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.
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 |