class Puppet::Environments::Cached
Public Class Methods
# File lib/puppet/environments.rb 353 def self.cache_expiration_service 354 @cache_expiration_service_singleton || DefaultCacheExpirationService.new 355 end
# File lib/puppet/environments.rb 349 def self.cache_expiration_service=(service) 350 @cache_expiration_service_singleton = service 351 end
# File lib/puppet/environments.rb 357 def initialize(loader) 358 @loader = loader 359 @cache_expiration_service = Puppet::Environments::Cached.cache_expiration_service 360 @cache = {} 361 end
Public Instance Methods
Clears the cache of the environment with the given name. (The intention is that this could be used from a MANUAL cache eviction command (TBD)
# File lib/puppet/environments.rb 442 def clear(name) 443 name = name.to_sym 444 entry = @cache[name] 445 clear_entry(name, entry) if entry 446 end
Clears all cached environments. (The intention is that this could be used from a MANUAL cache eviction command (TBD)
Puppet::Environments::EnvironmentLoader#clear_all
# File lib/puppet/environments.rb 450 def clear_all 451 super 452 453 @cache.each_pair do |name, entry| 454 clear_entry(name, entry) 455 end 456 457 @cache = {} 458 Puppet::GettextConfig.delete_environment_text_domains 459 end
Creates a suitable cache entry given the time to live for one environment
# File lib/puppet/environments.rb 516 def entry(env) 517 ttl = if (conf = get_conf(env.name)) 518 conf.environment_timeout 519 else 520 Puppet[:environment_timeout] 521 end 522 523 case ttl 524 when 0 525 NotCachedEntry.new(env) # Entry that is always expired (avoids syscall to get time) 526 when Float::INFINITY 527 Entry.new(env) # Entry that never expires (avoids syscall to get time) 528 else 529 MRUEntry.new(env, ttl) # Entry that expires in ttl from when it was last touched 530 end 531 end
@!macro loader_get
# File lib/puppet/environments.rb 396 def get(name) 397 entry = get_entry(name) 398 entry ? entry.value : nil 399 end
This implementation evicts the cache, and always gets the current configuration of the environment
TODO: While this is wasteful since it needs to go on a search for the conf, it is too disruptive to optimize this.
@!macro loader_get_conf
# File lib/puppet/environments.rb 494 def get_conf(name) 495 name = name.to_sym 496 clear_if_expired(name, @cache[name]) 497 @loader.get_conf(name) 498 end
Guard an environment so it can't be evicted while it's in use. The method may be called multiple times, provided it is unguarded the same number of times. If you call this method, you must call `unguard` in an ensure block.
# File lib/puppet/environments.rb 503 def guard(name) 504 entry = get_entry(name, false) 505 entry.guard if entry 506 end
@!macro loader_list
# File lib/puppet/environments.rb 364 def list 365 # Evict all that have expired, in the same way as `get` 366 clear_all_expired 367 368 # Evict all that was removed from disk 369 cached_envs = @cache.keys.map!(&:to_sym) 370 loader_envs = @loader.list.map!(&:name) 371 removed_envs = cached_envs - loader_envs 372 373 removed_envs.each do |env_name| 374 Puppet.debug { "Environment no longer exists '#{env_name}'"} 375 clear(env_name) 376 end 377 378 @loader.list.map do |env| 379 name = env.name 380 old_entry = @cache[name] 381 if old_entry 382 old_entry.value 383 else 384 add_entry(name, entry(env)) 385 env 386 end 387 end 388 end
@!macro loader_search_paths
# File lib/puppet/environments.rb 391 def search_paths 392 @loader.search_paths 393 end
Unguard an environment.
# File lib/puppet/environments.rb 509 def unguard(name) 510 entry = get_entry(name, false) 511 entry.unguard if entry 512 end
Private Instance Methods
Adds a cache entry to the cache
# File lib/puppet/environments.rb 424 def add_entry(name, cache_entry) 425 Puppet.debug {"Caching environment #{name.inspect} #{cache_entry.label}"} 426 @cache[name] = cache_entry 427 @cache_expiration_service.created(cache_entry.value) 428 end
Clears all environments that have expired, either by exceeding their time to live, or through an explicit eviction determined by the cache expiration service.
# File lib/puppet/environments.rb 464 def clear_all_expired 465 t = Time.now 466 467 @cache.each_pair do |name, entry| 468 clear_if_expired(name, entry, t) 469 end 470 end
# File lib/puppet/environments.rb 431 def clear_entry(name, entry) 432 @cache.delete(name) 433 Puppet.debug {"Evicting cache entry for environment #{name.inspect}"} 434 @cache_expiration_service.evicted(name.to_sym) 435 Puppet::GettextConfig.delete_text_domain(name) 436 Puppet.settings.clear_environment_settings(name) 437 end
Clear an environment if it is expired, either by exceeding its time to live, or through an explicit eviction determined by the cache expiration service.
# File lib/puppet/environments.rb 476 def clear_if_expired(name, entry, t = Time.now) 477 return unless entry 478 return if entry.guarded? 479 480 if entry.expired?(t) || @cache_expiration_service.expired?(name.to_sym) 481 clear_entry(name, entry) 482 end 483 end
Get a cache entry for an envionment. It returns nil if the environment doesn't exist.
# File lib/puppet/environments.rb 403 def get_entry(name, check_expired = true) 404 # Aggressively evict all that has expired 405 # This strategy favors smaller memory footprint over environment 406 # retrieval time. 407 clear_all_expired if check_expired 408 name = name.to_sym 409 entry = @cache[name] 410 if entry 411 Puppet.debug {"Found in cache #{name.inspect} #{entry.label}"} 412 # found in cache 413 entry.touch 414 elsif (env = @loader.get(name)) 415 # environment loaded, cache it 416 entry = entry(env) 417 add_entry(name, entry) 418 end 419 entry 420 end