mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
Revert status cache to use class variables with RuboCop exceptions
Class variables are needed here because: - Cache must be shared across all Sinatra app instances - Class instance variables don't work in Sinatra's dynamic instantiation model - This is a valid use case for class variables despite RuboCop warning
This commit is contained in:
parent
a4abe2652a
commit
325a5c413c
1 changed files with 10 additions and 15 deletions
|
|
@ -10,21 +10,16 @@ module Vmpooler
|
||||||
api_prefix = "/api/v#{api_version}"
|
api_prefix = "/api/v#{api_version}"
|
||||||
|
|
||||||
# Simple in-memory cache for status endpoint
|
# Simple in-memory cache for status endpoint
|
||||||
@status_cache = {}
|
# rubocop:disable Style/ClassVars
|
||||||
@status_cache_mutex = Mutex.new
|
@@status_cache = {}
|
||||||
|
@@status_cache_mutex = Mutex.new
|
||||||
|
# rubocop:enable Style/ClassVars
|
||||||
STATUS_CACHE_TTL = 30 # seconds
|
STATUS_CACHE_TTL = 30 # seconds
|
||||||
|
|
||||||
class << self
|
|
||||||
attr_accessor :status_cache, :status_cache_mutex
|
|
||||||
end
|
|
||||||
|
|
||||||
@status_cache ||= {}
|
|
||||||
@status_cache_mutex ||= Mutex.new
|
|
||||||
|
|
||||||
# Clear cache (useful for testing)
|
# Clear cache (useful for testing)
|
||||||
def self.clear_status_cache
|
def self.clear_status_cache
|
||||||
@status_cache_mutex.synchronize do
|
@@status_cache_mutex.synchronize do
|
||||||
@status_cache.clear
|
@@status_cache.clear
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -485,8 +480,8 @@ module Vmpooler
|
||||||
|
|
||||||
# Cache helper methods for status endpoint
|
# Cache helper methods for status endpoint
|
||||||
def get_cached_status(cache_key)
|
def get_cached_status(cache_key)
|
||||||
self.class.status_cache_mutex.synchronize do
|
@@status_cache_mutex.synchronize do
|
||||||
cached = self.class.status_cache[cache_key]
|
cached = @@status_cache[cache_key]
|
||||||
if cached && (Time.now - cached[:timestamp]) < STATUS_CACHE_TTL
|
if cached && (Time.now - cached[:timestamp]) < STATUS_CACHE_TTL
|
||||||
return cached[:data]
|
return cached[:data]
|
||||||
end
|
end
|
||||||
|
|
@ -496,8 +491,8 @@ module Vmpooler
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_cached_status(cache_key, data)
|
def set_cached_status(cache_key, data)
|
||||||
self.class.status_cache_mutex.synchronize do
|
@@status_cache_mutex.synchronize do
|
||||||
self.class.status_cache[cache_key] = {
|
@@status_cache[cache_key] = {
|
||||||
data: data,
|
data: data,
|
||||||
timestamp: Time.now
|
timestamp: Time.now
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue