class Puppet::Environments::Cached

Public Class Methods

cache_expiration_service() click to toggle source
    # File lib/puppet/environments.rb
343 def self.cache_expiration_service
344   @cache_expiration_service_singleton || DefaultCacheExpirationService.new
345 end
cache_expiration_service=(service) click to toggle source
    # File lib/puppet/environments.rb
339 def self.cache_expiration_service=(service)
340   @cache_expiration_service_singleton = service
341 end
new(loader) click to toggle source
    # File lib/puppet/environments.rb
347 def initialize(loader)
348   @loader = loader
349   @cache_expiration_service = Puppet::Environments::Cached.cache_expiration_service
350   @cache = {}
351 end

Public Instance Methods

clear(name) click to toggle source

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
432 def clear(name)
433   name = name.to_sym
434   entry = @cache[name]
435   clear_entry(name, entry) if entry
436 end
clear_all() click to toggle source

Clears all cached environments. (The intention is that this could be used from a MANUAL cache eviction command (TBD)

    # File lib/puppet/environments.rb
440 def clear_all
441   super
442 
443   @cache.each_pair do |name, entry|
444     clear_entry(name, entry)
445   end
446 
447   @cache = {}
448   Puppet::GettextConfig.delete_environment_text_domains
449 end
entry(env) click to toggle source

Creates a suitable cache entry given the time to live for one environment

    # File lib/puppet/environments.rb
506 def entry(env)
507   ttl = if (conf = get_conf(env.name))
508           conf.environment_timeout
509         else
510           Puppet[:environment_timeout]
511         end
512 
513   case ttl
514   when 0
515     NotCachedEntry.new(env)     # Entry that is always expired (avoids syscall to get time)
516   when Float::INFINITY
517     Entry.new(env)              # Entry that never expires (avoids syscall to get time)
518   else
519     MRUEntry.new(env, ttl)      # Entry that expires in ttl from when it was last touched
520   end
521 end
get(name) click to toggle source

@!macro loader_get

    # File lib/puppet/environments.rb
386 def get(name)
387   entry = get_entry(name)
388   entry ? entry.value : nil
389 end
get_conf(name) click to toggle source

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
484 def get_conf(name)
485   name = name.to_sym
486   clear_if_expired(name, @cache[name])
487   @loader.get_conf(name)
488 end
guard(name) click to toggle source

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
493 def guard(name)
494   entry = get_entry(name, false)
495   entry.guard if entry
496 end
list() click to toggle source

@!macro loader_list

    # File lib/puppet/environments.rb
354 def list
355   # Evict all that have expired, in the same way as `get`
356   clear_all_expired
357 
358   # Evict all that was removed from disk
359   cached_envs = @cache.keys.map!(&:to_sym)
360   loader_envs = @loader.list.map!(&:name)
361   removed_envs = cached_envs - loader_envs
362 
363   removed_envs.each do |env_name|
364     Puppet.debug { "Environment no longer exists '#{env_name}'"}
365     clear(env_name)
366   end
367 
368   @loader.list.map do |env|
369     name = env.name
370     old_entry = @cache[name]
371     if old_entry
372       old_entry.value
373     else
374       add_entry(name, entry(env))
375       env
376     end
377   end
378 end
search_paths() click to toggle source

@!macro loader_search_paths

    # File lib/puppet/environments.rb
381 def search_paths
382   @loader.search_paths
383 end
unguard(name) click to toggle source

Unguard an environment.

    # File lib/puppet/environments.rb
499 def unguard(name)
500   entry = get_entry(name, false)
501   entry.unguard if entry
502 end

Private Instance Methods

add_entry(name, cache_entry) click to toggle source

Adds a cache entry to the cache

    # File lib/puppet/environments.rb
414 def add_entry(name, cache_entry)
415   Puppet.debug {"Caching environment #{name.inspect} #{cache_entry.label}"}
416   @cache[name] = cache_entry
417   @cache_expiration_service.created(cache_entry.value)
418 end
clear_all_expired() click to toggle source

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
454 def clear_all_expired
455   t = Time.now
456 
457   @cache.each_pair do |name, entry|
458     clear_if_expired(name, entry, t)
459   end
460 end
clear_entry(name, entry) click to toggle source
    # File lib/puppet/environments.rb
421 def clear_entry(name, entry)
422   @cache.delete(name)
423   Puppet.debug {"Evicting cache entry for environment #{name.inspect}"}
424   @cache_expiration_service.evicted(name.to_sym)
425   Puppet::GettextConfig.delete_text_domain(name)
426   Puppet.settings.clear_environment_settings(name)
427 end
clear_if_expired(name, entry, t = Time.now) click to toggle source

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
466 def clear_if_expired(name, entry, t = Time.now)
467   return unless entry
468   return if entry.guarded?
469 
470   if entry.expired?(t) || @cache_expiration_service.expired?(name.to_sym)
471     clear_entry(name, entry)
472   end
473 end
get_entry(name, check_expired = true) click to toggle source

Get a cache entry for an envionment. It returns nil if the environment doesn't exist.

    # File lib/puppet/environments.rb
393 def get_entry(name, check_expired = true)
394   # Aggressively evict all that has expired
395   # This strategy favors smaller memory footprint over environment
396   # retrieval time.
397   clear_all_expired if check_expired
398   name = name.to_sym
399   entry = @cache[name]
400   if entry
401     Puppet.debug {"Found in cache #{name.inspect} #{entry.label}"}
402     # found in cache
403     entry.touch
404   elsif (env = @loader.get(name))
405     # environment loaded, cache it
406     entry = entry(env)
407     add_entry(name, entry)
408   end
409   entry
410 end