Skip to content

Commit

Permalink
Merge pull request #189 from dainst/clear_image_list_cache
Browse files Browse the repository at this point in the history
Clear image list cache
  • Loading branch information
the-last-pastafarian authored Dec 20, 2023
2 parents 461b54c + 8b56aa2 commit 0cbefb0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/lib/field_hub/file_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule FieldHub.FileStore do
@valid_file_variants Application.compile_env(:field_hub, :valid_file_variants)
@index_cache_name Application.compile_env(:field_hub, :file_index_cache_name)
@index_cache_expiration_ms 1000 * 60 * 60 * 24
# @index_cache_expiration_ms 3000

require Logger

Expand Down Expand Up @@ -320,7 +321,7 @@ defmodule FieldHub.FileStore do
end
end

defp clear_cache(project) do
def clear_cache(project) do
Cachex.del(@index_cache_name, project)
end
end
7 changes: 7 additions & 0 deletions server/lib/field_hub_web/live/project_show_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule FieldHubWeb.ProjectShowLive do
|> assign(:new_password, "")
|> assign(:confirm_project_name, "")
|> assign(:delete_files, false)
|> assign(:hide_cache_cleared_message, true)
|> read_project_doc()
}

Expand Down Expand Up @@ -116,6 +117,12 @@ defmodule FieldHubWeb.ProjectShowLive do
{:noreply, assign(socket, :new_password, password)}
end

def handle_event("delete_cache", _values, %{assigns: %{project: project}} = socket) do
{:ok, true} = FieldHub.FileStore.clear_cache(project)

{:noreply, assign(socket, :hide_cache_cleared_message, false)}
end

def handle_event(
"delete_form_change",
%{
Expand Down
7 changes: 7 additions & 0 deletions server/lib/field_hub_web/live/project_show_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@
</div>
</div>
</form>
<hr>
<h2>
<div class="row">
<div class="column column-80">Cache <small hidden={@hide_cache_cleared_message}><i>cleared</i></small> </div>

<button type="button" class="button column" phx-click="delete_cache">Clear cache</button>
</div>
</h2>
<hr>
<h2>Delete project</h2>
<form id="del_form" phx-change="delete_form_change">
Expand Down
17 changes: 17 additions & 0 deletions server/test/field_hub_web/live/project_show_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do
@user_password "test_password"

@admin_user Application.compile_env(:field_hub, :couchdb_admin_name)
@index_cache_name Application.compile_env(:field_hub, :file_index_cache_name)

test "redirect to login if not authenticated", %{conn: conn} do
# Test the authentication plug (http)
Expand Down Expand Up @@ -422,6 +423,22 @@ defmodule FieldHubWeb.ProjectShowLiveTest do
})
end

test "file index cache can be deleted through the interface", %{conn: conn} do
{:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}")

# TODO: evaluate why we have to do this
render(view)
assert {:ok, %{"o26" => _value}} = Cachex.get(@index_cache_name, @project)

html =
view
|> element("button", "Clear cache")
|> render_click()

assert html =~ "Cache <small><i>cleared</i></small>"
assert {:ok, nil} = Cachex.get(@index_cache_name, @project)
end

test "admin is able to delete a project's database", %{conn: conn} do
{:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}")

Expand Down

0 comments on commit 0cbefb0

Please sign in to comment.