Skip to content

Commit

Permalink
Correct purge_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanic committed Apr 23, 2019
1 parent 1618f18 commit 76ef266
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/forecastr/cache/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Forecastr.Cache.Worker do
# Client API
@spec start_link(Keyword.t()) :: {:ok, pid()}
def start_link([name: worker_name] = opts) do
GenServer.start_link(__MODULE__, %{name: worker_name, timer_ref: nil}, opts)
GenServer.start_link(__MODULE__, %{name: worker_name}, opts)
end

@spec get(atom(), String.t()) :: map() | nil
Expand Down Expand Up @@ -39,18 +39,14 @@ defmodule Forecastr.Cache.Worker do
def handle_call(
{:set, query, response, options},
_from,
%{name: worker_name, timer_ref: timer_ref} = state
%{name: worker_name} = state
) do
true = :ets.insert(worker_name, {query, response})
timer_ref = schedule_purge_cache(query, timer_ref, options)
{:reply, :ok, %{state | timer_ref: timer_ref}}
schedule_purge_cache(query, options)
{:reply, :ok, state}
end

def schedule_purge_cache(query, nil = _timer_ref, ttl: minutes),
do: Process.send_after(self(), {:purge_cache, query}, minutes)

def schedule_purge_cache(query, timer_ref, ttl: minutes) do
Process.cancel_timer(timer_ref)
def schedule_purge_cache(query, ttl: minutes) do
Process.send_after(self(), {:purge_cache, query}, minutes)
end

Expand Down

0 comments on commit 76ef266

Please sign in to comment.