Skip to content

Commit

Permalink
feat(Algolia.Api): cache requests (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen authored Oct 19, 2023
1 parent 70f8143 commit df19f22
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
18 changes: 15 additions & 3 deletions apps/algolia/lib/algolia/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Algolia.Api do
"""
alias Algolia.Config
require Logger
use RepoCache, ttl: :timer.hours(12)

defstruct [:host, :index, :action, :body]

Expand Down Expand Up @@ -39,9 +40,20 @@ defmodule Algolia.Api do
|> hackney_opts()
|> Keyword.put(:pool, @http_pool)

opts
|> generate_url(config)
|> HTTPoison.post(body, headers(config), hackney: hackney)
send_post_request = fn {body, config} ->
opts
|> generate_url(config)
|> HTTPoison.post(body, headers(config), hackney: hackney)
end

# If we're making a query for results using the same request body AND same
# %Algolia.Config{}, cache the response instead of making extra calls to the
# Algolia REST API
if action == "queries" do
cache({body, config}, send_post_request)
else
send_post_request.({body, config})
end
end

@spec generate_url(t, Config.t()) :: String.t()
Expand Down
3 changes: 1 addition & 2 deletions apps/algolia/lib/algolia/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ defmodule Algolia.Application do
def start(_type, _args) do
# List all child processes to be supervised
children = [
# Starts a worker by calling: Algolia.Worker.start_link(arg)
# {Algolia.Worker, arg},
Algolia.Api
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
3 changes: 2 additions & 1 deletion apps/algolia/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule Algolia.Mixfile do
{:util, in_umbrella: true},
{:httpoison, "~> 1.5"},
{:plug, "~> 1.14.2"},
{:bypass, "~> 1.0", only: :test}
{:bypass, "~> 1.0", only: :test},
{:repo_cache, in_umbrella: true}
]
end
end
6 changes: 4 additions & 2 deletions apps/algolia/test/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ defmodule Algolia.ApiTest do
@success_response ~s({"message" : "success"})

describe "post" do
test "sends a post request to /1/indexes/$INDEX/$ACTION" do
test "sends a post request once to /1/indexes/$INDEX/$ACTION" do
bypass = Bypass.open()

Bypass.expect(bypass, "POST", "/1/indexes/*/queries", fn conn ->
Bypass.expect_once(bypass, "POST", "/1/indexes/*/queries", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

case Poison.decode(body) do
Expand All @@ -29,6 +29,8 @@ defmodule Algolia.ApiTest do

assert {:ok, %HTTPoison.Response{status_code: 200, body: body}} = Algolia.Api.post(opts)
assert body == @success_response
# Can be called again with result from cache instead of hitting the API endpoint
assert {:ok, %HTTPoison.Response{status_code: 200, body: ^body}} = Algolia.Api.post(opts)
end

test "logs a warning if config keys are missing" do
Expand Down

0 comments on commit df19f22

Please sign in to comment.