Skip to content

Commit

Permalink
use class variable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Dec 19, 2016
1 parent c3ce0a8 commit cd8a39e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
4 changes: 4 additions & 0 deletions lib/jekyll-include-cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module JekyllIncludeCache
autoload :Tag, "jekyll-include-cache/tag"

def self.cache
@cache ||= {}
end
end

Liquid::Template.register_tag("include_cached", JekyllIncludeCache::Tag)
10 changes: 4 additions & 6 deletions lib/jekyll-include-cache/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ def render(context)
return unless path

key = key(path, params)
cached = cache(context)[key]
cached = JekyllIncludeCache.cache[key]

if cached
Jekyll.logger.debug "Include cache hit:", path
cached
else
cache(context)[key] = super
Jekyll.logger.debug "Include cache miss:", path
JekyllIncludeCache.cache[key] = super
end
end

Expand All @@ -28,9 +30,5 @@ def path(context)
def key(path, params)
Digest::MD5.hexdigest(path.to_s + params.to_s)
end

def cache(context)
context.registers[:cached_includes] ||= {}
end
end
end
28 changes: 2 additions & 26 deletions spec/jekyll-include-tag/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:context) { Liquid::Context.new(environments, outer_scope, registers) }

subject { described_class.send(:new, tag_name, markup, nil) }
let(:cache) { subject.send(:cache, context) }
let(:cache) { JekyllIncludeCache.cache }
let(:path) { subject.send(:path, context) }
let(:parsed_params) { subject.parse_params(context) }
let(:cache_key) { subject.send(:key, path, parsed_params) }
Expand All @@ -39,25 +39,6 @@
end
end

it "initializess the cache" do
expect(cache).to be_a(Hash)
expect(cache).to be_empty
end

context "with something cached" do
let(:registers) do
{
:site => site,
:cached_includes => { "foo" => "bar" }
}
end

it "returns the cache" do
expect(cache).to have_key("foo")
expect(cache["foo"]).to eql("bar")
end
end

context "rendering" do
before { subject.render(context) }
let(:rendered) { subject.render(context) }
Expand All @@ -73,13 +54,8 @@

context "with the cache stubbed" do
before { allow(subject).to receive(:key).and_return(cache_key) }
before { cache[cache_key] = "Some other content\n" }
let(:cache_key) { "asdf" }
let(:registers) do
{
:site => site,
:cached_includes => { cache_key => "Some other content\n" }
}
end

it "returns the cached value" do
expect(rendered).to eql("Some other content\n")
Expand Down
17 changes: 17 additions & 0 deletions spec/jekyll-include-tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.describe JekyllIncludeCache do
it "initializess the cache" do
expect(described_class.cache).to be_a(Hash)
expect(described_class.cache).to be_empty
end

context "with something cached" do
before do
described_class.instance_variable_set("@cache", { "foo" => "bar" })
end

it "returns the cache" do
expect(described_class.cache).to have_key("foo")
expect(described_class.cache["foo"]).to eql("bar")
end
end
end

0 comments on commit cd8a39e

Please sign in to comment.