diff --git a/lib/solid/caching.ex b/lib/solid/caching.ex index c44a788..cc758fd 100644 --- a/lib/solid/caching.ex +++ b/lib/solid/caching.ex @@ -1,5 +1,5 @@ defmodule Solid.Caching do - @callback get(key :: term) :: {:ok, Template.t()} | {:error, :not_found} + @callback get(key :: term) :: {:ok, Solid.Template.t()} | {:error, :not_found} - @callback put(key :: term, Template.t()) :: :ok | {:error, term} + @callback put(key :: term, Solid.Template.t()) :: :ok | {:error, term} end diff --git a/lib/solid/tag/render.ex b/lib/solid/tag/render.ex index 335a1df..53f6822 100644 --- a/lib/solid/tag/render.ex +++ b/lib/solid/tag/render.ex @@ -56,16 +56,16 @@ defmodule Solid.Tag.Render do cache_key = :md5 |> :crypto.hash(template_str) |> Base.encode16(case: :lower) - template = + result = case apply(cache_module, :get, [cache_key]) do {:ok, cached_template} -> - cached_template + {:ok, cached_template} {:error, :not_found} -> parse_and_cache_partial(template_str, options, cache_key, cache_module) end - case template do + case result do {:ok, template} -> case Solid.render(template, binding_vars, options) do {:ok, rendered_text} -> @@ -81,14 +81,9 @@ defmodule Solid.Tag.Render do end defp parse_and_cache_partial(template_str, options, cache_key, cache_module) do - case Solid.parse(template_str, options) do - {:ok, template} = v -> - apply(cache_module, :put, [cache_key, template]) - - v - - {:error, _exception} = v -> - v + with {:ok, template} <- Solid.parse(template_str, options) do + apply(cache_module, :put, [cache_key, template]) + {:ok, template} end end end diff --git a/test/solid/tag/render_test.exs b/test/solid/tag/render_test.exs index db01733..f2d8fbb 100644 --- a/test/solid/tag/render_test.exs +++ b/test/solid/tag/render_test.exs @@ -25,7 +25,10 @@ defmodule Solid.Tag.RenderTest do @impl true def get(cache_key) do - Agent.get(__MODULE__, &Map.get(&1, cache_key, {:error, :not_found})) + case Agent.get(__MODULE__, &Map.get(&1, cache_key, {:error, :not_found})) do + {:error, :not_found} -> {:error, :not_found} + value -> {:ok, value} + end end def get_all() do @@ -66,6 +69,12 @@ defmodule Solid.Tag.RenderTest do assert %{calculated_cache_key => %Solid.Template{parsed_template: [text: ["hello there"]]}} == TestCache.get_all() + + assert {[text: [["hello there"]]], %Context{}} == + Render.render(parsed, %Context{}, + cache_module: TestCache, + file_system: {Test.SolidFileSystem, nil} + ) end test "must correctly return error from parsing" do