Skip to content

Commit cd8a39e

Browse files
committed
use class variable cache
1 parent c3ce0a8 commit cd8a39e

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed

lib/jekyll-include-cache.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
module JekyllIncludeCache
44
autoload :Tag, "jekyll-include-cache/tag"
5+
6+
def self.cache
7+
@cache ||= {}
8+
end
59
end
610

711
Liquid::Template.register_tag("include_cached", JekyllIncludeCache::Tag)

lib/jekyll-include-cache/tag.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ def render(context)
88
return unless path
99

1010
key = key(path, params)
11-
cached = cache(context)[key]
11+
cached = JekyllIncludeCache.cache[key]
1212

1313
if cached
14+
Jekyll.logger.debug "Include cache hit:", path
1415
cached
1516
else
16-
cache(context)[key] = super
17+
Jekyll.logger.debug "Include cache miss:", path
18+
JekyllIncludeCache.cache[key] = super
1719
end
1820
end
1921

@@ -28,9 +30,5 @@ def path(context)
2830
def key(path, params)
2931
Digest::MD5.hexdigest(path.to_s + params.to_s)
3032
end
31-
32-
def cache(context)
33-
context.registers[:cached_includes] ||= {}
34-
end
3533
end
3634
end

spec/jekyll-include-tag/tag_spec.rb

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:context) { Liquid::Context.new(environments, outer_scope, registers) }
1313

1414
subject { described_class.send(:new, tag_name, markup, nil) }
15-
let(:cache) { subject.send(:cache, context) }
15+
let(:cache) { JekyllIncludeCache.cache }
1616
let(:path) { subject.send(:path, context) }
1717
let(:parsed_params) { subject.parse_params(context) }
1818
let(:cache_key) { subject.send(:key, path, parsed_params) }
@@ -39,25 +39,6 @@
3939
end
4040
end
4141

42-
it "initializess the cache" do
43-
expect(cache).to be_a(Hash)
44-
expect(cache).to be_empty
45-
end
46-
47-
context "with something cached" do
48-
let(:registers) do
49-
{
50-
:site => site,
51-
:cached_includes => { "foo" => "bar" }
52-
}
53-
end
54-
55-
it "returns the cache" do
56-
expect(cache).to have_key("foo")
57-
expect(cache["foo"]).to eql("bar")
58-
end
59-
end
60-
6142
context "rendering" do
6243
before { subject.render(context) }
6344
let(:rendered) { subject.render(context) }
@@ -73,13 +54,8 @@
7354

7455
context "with the cache stubbed" do
7556
before { allow(subject).to receive(:key).and_return(cache_key) }
57+
before { cache[cache_key] = "Some other content\n" }
7658
let(:cache_key) { "asdf" }
77-
let(:registers) do
78-
{
79-
:site => site,
80-
:cached_includes => { cache_key => "Some other content\n" }
81-
}
82-
end
8359

8460
it "returns the cached value" do
8561
expect(rendered).to eql("Some other content\n")

spec/jekyll-include-tag_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
RSpec.describe JekyllIncludeCache do
2+
it "initializess the cache" do
3+
expect(described_class.cache).to be_a(Hash)
4+
expect(described_class.cache).to be_empty
5+
end
6+
7+
context "with something cached" do
8+
before do
9+
described_class.instance_variable_set("@cache", { "foo" => "bar" })
10+
end
11+
12+
it "returns the cache" do
13+
expect(described_class.cache).to have_key("foo")
14+
expect(described_class.cache["foo"]).to eql("bar")
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)