Skip to content

Commit

Permalink
Merge pull request #17 from hydecorp/master
Browse files Browse the repository at this point in the history
Use object ids instead of hashes for Jekyll documents
  • Loading branch information
benbalter authored May 26, 2020
2 parents cae8abb + 1567ce6 commit 2168263
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
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

0 comments on commit 2168263

Please sign in to comment.