Skip to content

Commit

Permalink
DRY cache separator
Browse files Browse the repository at this point in the history
  • Loading branch information
thbar committed Jun 5, 2024
1 parent 869c179 commit 97800f3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/unlock/lib/enforce_ttl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Unlock.EnforceTTL do
parts =
cache_key
|> String.replace_prefix(cache_prefix(), "")
|> String.split(":")
|> String.split(Unlock.Shared.cache_separator())

proxy_config_item(config, parts)
end
Expand Down
6 changes: 5 additions & 1 deletion apps/unlock/lib/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ defmodule Unlock.Shared do
def default_cache_expiration_seconds, do: 60

def cache_prefix, do: "resource:"
def cache_separator, do: ":"
def cache_key(resource_slug), do: "#{cache_prefix()}#{resource_slug}"
def cache_key(resource_slug, sub_item_identifier), do: "#{cache_key(resource_slug)}:#{sub_item_identifier}"

def cache_key(resource_slug, sub_item_identifier),
do: "#{cache_key(resource_slug)}#{cache_separator()}#{sub_item_identifier}"

def cache_name, do: Unlock.Cachex
def cache_entry(cache_key), do: Cachex.get!(cache_name(), cache_key)
def cache_ttl(cache_key), do: Cachex.ttl(cache_name(), cache_key)
Expand Down
2 changes: 1 addition & 1 deletion apps/unlock/lib/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Unlock.Telemetry do

def gbfs_request_event_names, do: @gbfs_request_types |> Enum.map(&gbfs_request_event_name/1)

@separator ":"
@separator Unlock.Shared.cache_separator()

def target_for_identifier(item_identifier) when is_binary(item_identifier) do
"proxy" <> @separator <> item_identifier
Expand Down

0 comments on commit 97800f3

Please sign in to comment.