Class: PuppetForge::LruCache

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_forge/lru_cache.rb

Overview

Implements a simple LRU cache. This is used internally by the V3::Base class to cache API responses.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 30) ⇒ LruCache

Returns a new instance of LruCache.

Parameters:

  • max_size (Integer) (defaults to: 30)

    the maximum number of items to cache. This can be overridden by setting the PUPPET_FORGE_MAX_CACHE_SIZE environment variable.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/puppet_forge/lru_cache.rb', line 21

def initialize(max_size = 30)
  raise ArgumentError, "max_size must be a positive integer" unless max_size.is_a?(Integer) && max_size > 0

  @max_size = ENV['PUPPET_FORGE_MAX_CACHE_SIZE'] ? ENV['PUPPET_FORGE_MAX_CACHE_SIZE'].to_i : max_size
  @cache = {}
  @lru = []
  @semaphore = Mutex.new
end

Instance Attribute Details

#max_sizeInteger (readonly)

Returns the maximum number of items to cache.

Returns:

  • (Integer)

    the maximum number of items to cache.



16
17
18
# File 'lib/puppet_forge/lru_cache.rb', line 16

def max_size
  @max_size
end

Class Method Details

.new_key(*string_args) ⇒ Object

Takes a list of strings (or objects that respond to #to_s) and returns a SHA256 hash of the strings joined with colons. This is a convenience method for generating cache keys. Cache keys do not have to be SHA256 hashes, but they must be unique.



11
12
13
# File 'lib/puppet_forge/lru_cache.rb', line 11

def self.new_key(*string_args)
  Digest(:SHA256).hexdigest(string_args.map(&:to_s).join(':'))
end

Instance Method Details

#clearObject

Clears the cache.



66
67
68
69
70
71
# File 'lib/puppet_forge/lru_cache.rb', line 66

def clear
  semaphore.synchronize do
    cache.clear
    lru.clear
  end
end

#get(key) ⇒ Object

Retrieves a value from the cache.

Parameters:

  • key (Object)

    the key to look up in the cache

Returns:

  • (Object)

    the cached value for the given key, or nil if the key is not present in the cache.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet_forge/lru_cache.rb', line 34

def get(key)
  if cache.key?(key)
    semaphore.synchronize do
      # If the key is present, move it to the front of the LRU
      # list.
      lru.delete(key)
      lru.unshift(key)
    end
    cache[key]
  end
end

#put(key, value) ⇒ Object

Adds a value to the cache.

Parameters:

  • key (Object)

    the key to add to the cache

  • value (Object)

    the value to add to the cache



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet_forge/lru_cache.rb', line 49

def put(key, value)
  semaphore.synchronize do
    if cache.key?(key)
      # If the key is already present, delete it from the LRU list.
      lru.delete(key)
    elsif cache.size >= max_size
      # If the cache is full, remove the least recently used item.
      cache.delete(lru.pop)
    end
    # Add the key to the front of the LRU list and add the value
    # to the cache.
    lru.unshift(key)
    cache[key] = value
  end
end