Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cache in schema loading #385

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/committee/drivers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'digest'

module Committee
module Drivers
# Gets a driver instance from the specified name. Raises ArgumentError for
Expand Down Expand Up @@ -36,13 +38,16 @@ def self.load_from_yaml(schema_path, parser_options: {})
# @param [String] schema_path
# @return [Committee::Driver]
def self.load_from_file(schema_path, parser_options: {})
case File.extname(schema_path)
when '.json'
load_from_json(schema_path, parser_options: parser_options)
when '.yaml', '.yml'
load_from_yaml(schema_path, parser_options: parser_options)
else
raise "Committee only supports the following file extensions: '.json', '.yaml', '.yml'"
@__load_from_file_cache ||= {}
@__load_from_file_cache[cache_key(schema_path, parser_options)] ||= begin
case File.extname(schema_path)
when '.json'
load_from_json(schema_path, parser_options: parser_options)
when '.yaml', '.yml'
load_from_yaml(schema_path, parser_options: parser_options)
else
raise "Committee only supports the following file extensions: '.json', '.yaml', '.yml'"
end
end
end

Expand Down Expand Up @@ -74,6 +79,17 @@ def self.load_from_data(hash, schema_path = nil, parser_options: {})
# TODO: in the future, pass `opts` here and allow optionality in other drivers?
driver.parse(hash)
end

class << self
private

def cache_key(schema_path, parser_options)
[
File.exist?(schema_path) ? Digest::MD5.hexdigest(File.read(schema_path)) : nil,
parser_options.hash,
].join('_')
end
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/drivers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@
end
assert_equal "Committee only supports the following file extensions: '.json', '.yaml', '.yml'", e.message
end

describe 'cache behavior' do
describe 'when loading the same file' do
it 'returns the same object when the options are identical' do
assert_equal(
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true}).object_id,
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true}).object_id,
)
end

it 'returns different objects if the options are different' do
refute_equal(
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true}).object_id,
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: false}).object_id,
)
end

it 'returns different objects if the file contents have changed' do
object_id = Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true}).object_id
original_file_contents = File.read(open_api_3_schema_path)
File.write(open_api_3_schema_path, original_file_contents + "\n")
refute_equal(
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true}).object_id,
object_id,
)
File.write(open_api_3_schema_path, original_file_contents)
end
end
end
end

describe 'load_from_json(schema_path)' do
Expand Down