diff --git a/app/action.rb b/app/action.rb index a4139d7..45bc29b 100644 --- a/app/action.rb +++ b/app/action.rb @@ -22,8 +22,8 @@ module Adamantium handle_exception ROM::TupleCountMismatchError => :not_found handle_exception StandardError => :handle_error - def cache(key:, content:) - cacher.call(key: key, content: content, expiry: TimeMath.min.advance(Time.now, +10)) + def cache(key:, content_proc:, expiry: TimeMath.min.advance(Time.now, +10)) + cacher.call(key: key, content_proc: content_proc, expiry: expiry) end def not_found(_req, res, _exception) diff --git a/app/actions/recently_played/index.rb b/app/actions/recently_played/index.rb index 84c7f5e..836bcc3 100644 --- a/app/actions/recently_played/index.rb +++ b/app/actions/recently_played/index.rb @@ -7,7 +7,7 @@ module Adamantium include Deps["views.recently_played.index"] def handle(req, res) - res.body = cache(key: "recently_played", content: index.call.to_str) + res.body = cache(key: "recently_played", content_proc: -> { index.call.to_str }) end end end diff --git a/lib/adamantium/view_cache/cacher.rb b/lib/adamantium/view_cache/cacher.rb index 40e25a7..4a40639 100644 --- a/lib/adamantium/view_cache/cacher.rb +++ b/lib/adamantium/view_cache/cacher.rb @@ -3,18 +3,20 @@ require "json" module Adamantium module ViewCache class Cacher - def call(key:, content:, expiry:) + def call(key:, content_proc:, expiry:) cached_content = read(key: key) return cached_content if cached_content - data = JSON.generate(expire: expiry.to_i, content: content) + rendered_content = content_proc.() + + data = JSON.generate(expire: expiry.to_i, content: rendered_content) path = "#{key}.json" File.write(File.join(Hanami.app.root, "tmp", path), data) - content + rendered_content end private