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

Use object ids instead of hashes for Jekyll documents #17

Merged
merged 8 commits into from
May 26, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ spec/examples.txt
*.gem
Gemfile.lock
/tmp
vendor/bundle
.bundle
.jekyll-cache
26 changes: 24 additions & 2 deletions lib/jekyll-include-cache/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,35 @@ def path(context)

def key(path, params)
path_hash = path.hash
params_hash = params.hash
params_hash = quick_hash(params)
self.class.digest_cache[path_hash] ||= {}
self.class.digest_cache[path_hash][params_hash] ||= digest(path_hash, params_hash)
end

def quick_hash(params)
return params.hash unless params

md5 = Digest::MD5.new

params.sort.each do |_, value|
# Using the fact that Jekyll documents don't change during a build.
# Instead of calculating the hash of an entire document (expensive!)
# we just use its object id.
if value.is_a? Jekyll::Drops::Drop
md5.update value.object_id.to_s
else
md5.update value.hash.to_s
end
end

md5.hexdigest
end

def digest(path_hash, params_hash)
Digest::MD5.hexdigest("#{path_hash}#{params_hash}")
md5 = Digest::MD5.new
md5.update path_hash.to_s
md5.update params_hash.to_s
md5.hexdigest
end
end
end
9 changes: 6 additions & 3 deletions spec/jekyll-include-tag/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@
context "building the key" do
it "builds the key" do
key = subject.send(:key, "foo.html", "foo" => "bar", "foo2" => "bar2")
params = { "foo" => "bar", "foo2" => "bar2" }
expect(key).to eql(
subject.send(:digest, "foo.html".hash, { "foo" => "bar", "foo2" => "bar2" }.hash)
subject.send(:digest, "foo.html".hash, subject.send(:quick_hash, params))
)
end

it "builds the key based on the path" do
key = subject.send(:key, "foo2.html", "foo" => "bar", "foo2" => "bar2")
params = { "foo" => "bar", "foo2" => "bar2" }
expect(key).to eql(
subject.send(:digest, "foo2.html".hash, { "foo" => "bar", "foo2" => "bar2" }.hash)
subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, params))
)
end

it "builds the key based on the params" do
key = subject.send(:key, "foo2.html", "foo" => "bar")
expect(key).to eql(subject.send(:digest, "foo2.html".hash, { "foo" => "bar" }.hash))
params = { "foo" => "bar" }
expect(key).to eql(subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, params)))
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

Jekyll.logger.adjust_verbosity(:quiet => true)

Jekyll::Cache.base_dir = File.expand_path("../tmp", __dir__) if defined? Jekyll::Cache
Jekyll::Cache.cache_dir = File.expand_path("../tmp", __dir__) if defined? Jekyll::Cache

def fixture_path(fixture)
File.expand_path "./fixtures/#{fixture}", File.dirname(__FILE__)
Expand Down