Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear cache #70

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/elixirus/healthcheck/health_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ defmodule Elixirus.Healthcheck.HealthWorker do
{:reply, service_status, state}
end

def handle_call({:refresh_status}, _from, %{service: service} = state) do
new_service_status = apply(service, :check_status, [])
new_state = Map.put(state, :status, new_service_status)
{:reply, new_service_status, new_state}
end

def handle_continue(:init_status, state), do: execute_check_status(state)

def handle_info(:check, state), do: execute_check_status(state)
Expand Down
1 change: 1 addition & 0 deletions lib/elixirus/healthcheck/healthcheck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ defmodule Elixirus.Healthcheck.Healthcheck do
"""

def get_service_status(service), do: GenServer.call(service, {:get_status})
def refresh_service_status(service), do: GenServer.call(service, {:refresh_status})
end
5 changes: 4 additions & 1 deletion lib/elixirus/healthcheck/services/librus_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ defmodule Elixirus.Healthcheck.Services.LibrusConnection do
timeout: 5_000,
recv_timeout: 5_000
) do
{:ok, _} ->
{:ok,
%HTTPoison.Response{
status_code: 302
}} ->
set_proxy(:down)
:up

Expand Down
2 changes: 1 addition & 1 deletion lib/elixirus_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<Heroicons.arrow_right_on_rectangle class="w-12 hover:cursor-pointer hover:text-red-600 hover:translate-x-2 transition duration-500" />
</button>

<.link navigate={~p"/student/refresh"}><Heroicons.arrow_path class="h-12 w-12"/></.link>
<.link navigate={~p"/student/refresh"}><Heroicons.arrow_path class="h-12 w-12" /></.link>
</:misc>
</.header>
</header>
Expand Down
28 changes: 17 additions & 11 deletions lib/elixirus_web/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,32 @@ defmodule ElixirusWeb.Helpers do
end

def cache_and_ttl_data(user_id, cache_type, data, expiry_time \\ 5) do
Cachex.get_and_update(:elixirus_cache, user_id, fn caches ->
Cachex.get_and_update(:elixirus_cache, user_id, fn caches ->
if caches != nil do
[cache_type | caches]
else
[cache_type | caches]
else
[cache_type]
end

end)

Cachex.put(:elixirus_cache, user_id <> cache_type, data)
Cachex.expire(:elixirus_cache, user_id <> cache_type, :timer.minutes(expiry_time))
end

def purge_user_cache(user_id) do
case Cachex.get(:elixirus_cache, user_id) do
{:ok, nil} -> :ok
{:ok, user_caches} -> Enum.each(user_caches, fn cache ->
Cachex.del(:elixirus_cache, user_id <> cache)
end)
_ -> :ok
def purge_user_cache(user_id) do
case Cachex.get(:elixirus_cache, user_id) do
{:ok, nil} ->
:ok

{:ok, user_caches} ->
Enum.each(user_caches, fn cache ->
Cachex.del(:elixirus_cache, user_id <> cache)
end)

_ ->
:ok
end

Cachex.del(:elixirus_cache, user_id)
:ok
end
Expand Down
9 changes: 4 additions & 5 deletions lib/elixirus_web/live/student_live/refresh.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
defmodule ElixirusWeb.StudentLive.Refresh do

defmodule ElixirusWeb.StudentLive.Refresh do
use ElixirusWeb, :live_view
import ElixirusWeb.Helpers, only: [purge_user_cache: 1]
def mount(_params, %{"user_id" => user_id}, socket) do

def mount(_params, %{"user_id" => user_id}, socket) do
purge_user_cache(user_id)
{:ok, push_navigate(socket, to: ~p"/")}
end

def render(assigns) do
def render(assigns) do
~H"""
<h1>cache cleared</h1>
"""
Expand Down
Loading