Skip to content

Commit

Permalink
fix: add Auth context cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chasers committed Dec 13, 2023
1 parent 09e2a36 commit 99aaf81
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/logflare/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Logflare.Application do
alias Logflare.SourceSchemas
alias Logflare.Users
alias Logflare.Partners
alias Logflare.Auth

def start(_type, _args) do
env = Application.get_env(:logflare, :env)
Expand Down Expand Up @@ -40,6 +41,7 @@ defmodule Logflare.Application do
Partners.Cache,
Billing.Cache,
SourceSchemas.Cache,
Auth.Cache,
PubSubRates.Cache,
Logs.LogEvents.Cache,
Logs.RejectedLogEvents,
Expand Down Expand Up @@ -101,6 +103,7 @@ defmodule Logflare.Application do
Sources.Cache,
Billing.Cache,
SourceSchemas.Cache,
Auth.Cache,
PubSubRates.Cache,
Logs.LogEvents.Cache,

Expand Down
30 changes: 30 additions & 0 deletions lib/logflare/auth/cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Logflare.Auth.Cache do
@moduledoc """
Cache for Authorization context. The keys for this cache expire in the defined
Cachex `expiration`.
"""

require Cachex.Spec

alias Logflare.Auth

def child_spec(_) do
stats = Application.get_env(:logflare, :cache_stats, false)

%{
id: __MODULE__,
start:
{Cachex, :start_link,
[__MODULE__, [stats: stats, expiration: Cachex.Spec.expiration(default: 30_000)]]}
}
end

@spec verify_access_token(OauthAccessToken.t() | String.t(), String.t() | [String.t()]) ::
{:ok, User.t()} | {:error, term()}
def verify_access_token(access_token_or_api_key, scopes),
do: apply_repo_fun(__ENV__.function, [access_token_or_api_key, scopes])

defp apply_repo_fun(arg1, arg2) do
Logflare.ContextCache.apply_fun(Auth, arg1, arg2)
end
end
11 changes: 10 additions & 1 deletion lib/logflare/context_cache.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Logflare.ContextCache do
@moduledoc """
Read-through cache for hot database paths. This module functions as the entry point for
Read-through cache for hot database paths and/or functions. This module functions as the entry point for
contexts to have a cache of function calls.
e.g. `Logflare.Users.Cache` functions go through `apply_fun/3` and results of those
Expand All @@ -17,6 +17,15 @@ defmodule Logflare.ContextCache do
So we keep the value of the `Logflare.Users.Cache` as the key in the `Logflare.ContextCache`
and the value of our `Logflare.ContextCache` key is the key for our `Logflare.Users.Cache`.
## Memoization
This module can also be used to cache heavy functions or db calls hidden behind a 3rd party
library. See `Logflare.Auth.Cache` for an example. In this example, the `expiration` set in that
Cachex child_spec is handling the cache expiration.
In the case functions don't return a response with a primary key, or something else we can
bust the cache on, it will get reverse indexed with `select_key/1` as `:unknown`.
"""

require Logger
Expand Down
2 changes: 1 addition & 1 deletion lib/logflare_web/controllers/plugs/verify_api_access.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule LogflareWeb.Plugs.VerifyApiAccess do
is_private_route? = "private" in scopes

with {:ok, access_token_or_api_key} <- extracted,
{:ok, %User{} = owner} <- Auth.verify_access_token(access_token_or_api_key, scopes) do
{:ok, %User{} = owner} <- Auth.Cache.verify_access_token(access_token_or_api_key, scopes) do
{:ok, Users.Cache.preload_defaults(owner)}
else
# don't preload for partners
Expand Down

0 comments on commit 99aaf81

Please sign in to comment.