From bcac887ebc9000d99c1727f1d6ed1f61dcefe998 Mon Sep 17 00:00:00 2001 From: Lukas Zapletal Date: Fri, 18 Jun 2021 10:36:04 +0200 Subject: [PATCH] Introduce cache and cleanup --- lib/facterdb.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/facterdb.rb b/lib/facterdb.rb index 885397ab..c0ac8a2b 100644 --- a/lib/facterdb.rb +++ b/lib/facterdb.rb @@ -11,6 +11,13 @@ def self.database @database ||= "[#{facterdb_fact_files.map { |f| read_json_file(f) }.join(',')}]\n" end + # @note Call this method at the end of test suite, for example via after(:suite), to reclaim back the memory required to hold json data and filter cache + def self.cleanup + @database = nil + Thread.current[:facterdb_last_filter_seen] = nil + Thread.current[:facterdb_last_facts] = nil + end + # @return [Boolean] - returns true if we should use the default facterdb database, false otherwise # @note If the user passes anything to the FACTERDB_SKIP_DEFAULTDB environment variable we assume # they want to skip the default db @@ -123,8 +130,16 @@ def self.valid_filters?(filters) # @return [Array] - array of hashes of facts # @param filter [Object] - The filter to convert to jgrep string - def self.get_facts(filter=nil) + def self.get_facts(filter=nil, cache=true) + if cache && filter && filter == Thread.current[:facterdb_last_filter_seen] + return Thread.current[:facterdb_last_facts] + end filter_str = generate_filter_str(filter) - JGrep.jgrep(database, filter_str).map { |hash| Hash[hash.map{ |k, v| [k.to_sym, v] }] } + result = JGrep.jgrep(database, filter_str).map { |hash| Hash[hash.map{ |k, v| [k.to_sym, v] }] } + if cache + Thread.current[:facterdb_last_filter_seen] = filter + Thread.current[:facterdb_last_facts] = result + end + result end end