class Puppet::Environments::Cached

Constants

END_OF_TIME
INFINITY
START_OF_TIME

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, line 418
def clear(name)
  @cache.delete(name)
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, line 424
def clear_all()
  super
  @cache = {}
  @expirations.clear
  @next_expiration = END_OF_TIME
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, line 434
def clear_all_expired()
  t = Time.now
  return if t < @next_expiration && ! @cache.any? {|name, _| @cache_expiration_service.expired?(name.to_sym) }
  to_expire = @cache.select { |name, entry| entry.expires < t || @cache_expiration_service.expired?(name.to_sym) }
  to_expire.each do |name, entry|
    Puppet.debug("Evicting cache entry for environment '#{name}'")
    @cache_expiration_service.evicted(name)
    clear(name)
    @expirations.delete(entry.expires)
    Puppet.settings.clear_environment_settings(name)
  end
  @next_expiration = @expirations.first || END_OF_TIME
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, line 463
def entry(env)
  ttl = (conf = get_conf(env.name)) ? conf.environment_timeout : Puppet.settings.value(:environment_timeout)
  case ttl
  when 0
    NotCachedEntry.new(env)     # Entry that is always expired (avoids syscall to get time)
  when INFINITY
    Entry.new(env)              # Entry that never expires (avoids syscall to get time)
  else
    TTLEntry.new(env, ttl)
  end
end
evict_if_expired(name) click to toggle source

Evicts the entry if it has expired Also clears caches in Settings that may prevent the entry from being updated

# File lib/puppet/environments.rb, line 477
def evict_if_expired(name)
  if (result = @cache[name]) && (result.expired? || @cache_expiration_service.expired?(name))
    @cache.delete(name)
    @cache_expiration_service.evicted(name)

    Puppet.settings.clear_environment_settings(name)
  end
end
get(name) click to toggle source

@!macro loader_get

# File lib/puppet/environments.rb, line 387
def get(name)
  # Aggressively evict all that has expired
  # This strategy favors smaller memory footprint over environment
  # retrieval time.
  clear_all_expired
  if result = @cache[name]
    # found in cache
    return result.value
  elsif (result = @loader.get(name))
    # environment loaded, cache it
    cache_entry = entry(result)
    @cache_expiration_service.created(result)
    add_entry(name, cache_entry)
    result
  end
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, line 456
def get_conf(name)
  evict_if_expired(name)
  @loader.get_conf(name)
end
list() click to toggle source

@!macro loader_list

# File lib/puppet/environments.rb, line 377
def list
  @loader.list
end
search_paths() click to toggle source

@!macro loader_search_paths

# File lib/puppet/environments.rb, line 382
def search_paths
  @loader.search_paths
end

Public Class Methods

cache_expiration_service() click to toggle source
# File lib/puppet/environments.rb, line 347
def self.cache_expiration_service
  @cache_expiration_service || DefaultCacheExpirationService.new
end
cache_expiration_service=(service) click to toggle source
# File lib/puppet/environments.rb, line 343
def self.cache_expiration_service=(service)
  @cache_expiration_service = service
end
end_of_time() click to toggle source

Returns the end of time (the next Mesoamerican Long Count cycle-end after 2012 (5125+2012) = 7137, of for a 32 bit machine using Ruby < 1.9.3, the year 2038.

# File lib/puppet/environments.rb, line 353
def self.end_of_time
  begin
    Time.gm(7137)
  rescue ArgumentError
    Time.gm(2038)
  end
end
new(loader) click to toggle source
# File lib/puppet/environments.rb, line 364
def initialize(loader)
  @loader = loader
  @cache_expiration_service = Puppet::Environments::Cached.cache_expiration_service
  @cache = {}

  # Holds expiration times in sorted order - next to expire is first
  @expirations = SortedSet.new

  # Infinity since it there are no entries, this is a cache of the first to expire time
  @next_expiration = END_OF_TIME
end