Skip to content

Commit 86a9449

Browse files
committed
add component controlled cache
1 parent e94c1ed commit 86a9449

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

lib/view_component/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require "view_component/translatable"
1616
require "view_component/with_content_helper"
1717
require "view_component/use_helpers"
18+
require "view_component/cache_on"
1819

1920
module ViewComponent
2021
class Base < ActionView::Base
@@ -34,6 +35,7 @@ def config
3435
include ViewComponent::Slotable
3536
include ViewComponent::Translatable
3637
include ViewComponent::WithContentHelper
38+
include ViewComponent::CacheOn
3739

3840
RESERVED_PARAMETER = :content
3941
VC_INTERNAL_DEFAULT_FORMAT = :html

lib/view_component/cache_on.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module ViewComponent::CacheOn
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
def cache_key
8+
@vc_cache_args = vc_cache_args.map { |method| send(method) } if defined?(vc_cache_args)
9+
10+
@vc_cache_key = Digest::MD5.hexdigest(@vc_cache_args.join)
11+
end
12+
end
13+
14+
class_methods do
15+
def cache_on(*args)
16+
define_method(:vc_cache_args) { args }
17+
end
18+
19+
def call
20+
if cache_key
21+
Rails.cache.fetch(cache_key) { super }
22+
else
23+
super
24+
end
25+
end
26+
end
27+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p class='cache-component__cache-key'><%= cache_key %></p>
2+
<p class='cache-component__cache-message'><%= "#{foo} #{bar}" %></p>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class CacheComponent < ViewComponent::Base
4+
cache_on :foo, :bar
5+
6+
attr_reader :foo, :bar
7+
8+
def initialize(foo:, bar:)
9+
@foo = foo
10+
@bar = bar
11+
end
12+
end

test/sandbox/test/rendering_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,4 +1240,21 @@ def test_request_param
12401240

12411241
assert_text("foo")
12421242
end
1243+
1244+
def test_cache_component
1245+
component = CacheComponent.new(foo: "foo", bar: "bar")
1246+
render_inline(component)
1247+
1248+
assert_selector(".cache-component__cache-key", text: component.cache_key)
1249+
assert_selector(".cache-component__cache-message", text: "foo bar")
1250+
1251+
render_inline(CacheComponent.new(foo: "foo", bar: "bar"))
1252+
1253+
assert_selector(".cache-component__cache-key", text: component.cache_key)
1254+
1255+
render_inline(CacheComponent.new(foo: "foo", bar: "baz"))
1256+
1257+
refute_selector(".cache-component__cache-key", text: component.cache_key)
1258+
refute_selector(".cache-component__cache-message", text: "foo bar")
1259+
end
12431260
end

0 commit comments

Comments
 (0)