Class: Puppet::Environments::Cached Private

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Synchronized, EnvironmentLoader
Defined in:
lib/puppet/environments.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: DefaultCacheExpirationService, Entry, MRUEntry, NotCachedEntry

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EnvironmentLoader

#get!

Constructor Details

#initialize(loader) ⇒ Cached

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Cached.



357
358
359
360
361
# File 'lib/puppet/environments.rb', line 357

def initialize(loader)
  @loader = loader
  @cache_expiration_service = Puppet::Environments::Cached.cache_expiration_service
  @cache = {}
end

Class Method Details

.cache_expiration_serviceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



353
354
355
# File 'lib/puppet/environments.rb', line 353

def self.cache_expiration_service
  @cache_expiration_service_singleton || DefaultCacheExpirationService.new
end

.cache_expiration_service=(service) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



349
350
351
# File 'lib/puppet/environments.rb', line 349

def self.cache_expiration_service=(service)
  @cache_expiration_service_singleton = service
end

Instance Method Details

#clear(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)



442
443
444
445
446
# File 'lib/puppet/environments.rb', line 442

def clear(name)
  name = name.to_sym
  entry = @cache[name]
  clear_entry(name, entry) if entry
end

#clear_allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



450
451
452
453
454
455
456
457
458
459
# File 'lib/puppet/environments.rb', line 450

def clear_all
  super

  @cache.each_pair do |name, entry|
    clear_entry(name, entry)
  end

  @cache = {}
  Puppet::GettextConfig.delete_environment_text_domains
end

#entry(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/puppet/environments.rb', line 516

def entry(env)
  ttl = if (conf = get_conf(env.name))
          conf.environment_timeout
        else
          Puppet[:environment_timeout]
        end

  case ttl
  when 0
    NotCachedEntry.new(env)     # Entry that is always expired (avoids syscall to get time)
  when Float::INFINITY
    Entry.new(env)              # Entry that never expires (avoids syscall to get time)
  else
    MRUEntry.new(env, ttl)      # Entry that expires in ttl from when it was last touched
  end
end

#get(name) ⇒ Puppet::Node::Environment?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find a named environment

Parameters:

  • name (String, Symbol)

    The name of environment to find

Returns:



396
397
398
399
# File 'lib/puppet/environments.rb', line 396

def get(name)
  entry = get_entry(name)
  entry ? entry.value : nil
end

#get_conf(name) ⇒ Puppet::Setting::EnvironmentConf?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Attempt to obtain the initial configuration for the environment. Not all loaders can provide this.

Parameters:

  • name (String, Symbol)

    The name of the environment whose configuration we are looking up

Returns:

  • (Puppet::Setting::EnvironmentConf, nil)

    the configuration for the requested environment, or nil if not found or no configuration is available



494
495
496
497
498
# File 'lib/puppet/environments.rb', line 494

def get_conf(name)
  name = name.to_sym
  clear_if_expired(name, @cache[name])
  @loader.get_conf(name)
end

#guard(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



503
504
505
506
# File 'lib/puppet/environments.rb', line 503

def guard(name)
  entry = get_entry(name, false)
  entry.guard if entry
end

#listArray<Puppet::Node::Environment>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns All of the environments known to the loader.

Returns:



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/puppet/environments.rb', line 364

def list
  # Evict all that have expired, in the same way as `get`
  clear_all_expired

  # Evict all that was removed from disk
  cached_envs = @cache.keys.map!(&:to_sym)
  loader_envs = @loader.list.map!(&:name)
  removed_envs = cached_envs - loader_envs

  removed_envs.each do |env_name|
    Puppet.debug { "Environment no longer exists '#{env_name}'"}
    clear(env_name)
  end

  @loader.list.map do |env|
    name = env.name
    old_entry = @cache[name]
    if old_entry
      old_entry.value
    else
      add_entry(name, entry(env))
      env
    end
  end
end

#search_pathsArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A list of indicators of where the loader is getting its environments from.

Returns:

  • (Array<String>)

    The URIs of the load locations



391
392
393
# File 'lib/puppet/environments.rb', line 391

def search_paths
  @loader.search_paths
end

#unguard(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unguard an environment.



509
510
511
512
# File 'lib/puppet/environments.rb', line 509

def unguard(name)
  entry = get_entry(name, false)
  entry.unguard if entry
end