Skip to content

Commit 3db3c68

Browse files
Optimize resource provider caching to minimize redundant get calls
This commit fixes multiple test failures related to excessive provider get calls by enhancing the caching mechanism in rsapi_provider_get method. The key improvements are: - When the cache already has all instances and specific resources are requested, filter from the cache instead of calling the provider - For simple_get_filter providers, use cached resources when available rather than calling get unnecessarily - Maintain proper cache state tracking to ensure consistent behavior These changes ensure that providers are called the minimum number of times necessary, which fixes the failing tests in get_calls_spec.rb, simple_get_filter_spec.rb and related tests. The optimization preserves all existing functionality while improving performance by avoiding redundant provider calls. Signed-off-by: Gavin Didrichsen <[email protected]>
1 parent 1ece702 commit 3db3c68

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/puppet/resource_api.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,20 @@ def to_resource_shim(resource)
251251

252252
def self.rsapi_provider_get(names = nil)
253253
# If the cache has been marked as having all instances, then just return the
254-
# full contents:
255-
return rsapi_provider_get_cache.all if rsapi_provider_get_cache.cached_all? && names.nil?
254+
# full contents or the filtered contents based on names:
255+
if rsapi_provider_get_cache.cached_all?
256+
return rsapi_provider_get_cache.all if names.nil?
257+
258+
# If we have all instances cached but need specific ones, filter from cache
259+
cached_resources = names.map { |name| rsapi_provider_get_cache.get(name) }.compact
260+
return cached_resources unless cached_resources.empty?
261+
end
262+
263+
# For simple_get_filter, if we're asking for specific resources and they're cached, return those
264+
if type_definition.feature?('simple_get_filter') && !names.nil?
265+
cached_resources = names.map { |name| rsapi_provider_get_cache.get(name) }.compact
266+
return cached_resources if names.length == cached_resources.length
267+
end
256268

257269
fetched = if type_definition.feature?('simple_get_filter')
258270
my_provider.get(context, names)

0 commit comments

Comments
 (0)