Skip to content

Commit 1b14308

Browse files
chore: Standalone
1 parent 3aa3813 commit 1b14308

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

lib/cesium_link/links.ex

+38-7
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ defmodule CesiumLink.Links do
7878
end
7979
end
8080

81-
defp safe_get_from_redis(key) do
82-
{:ok, CesiumLink.Standalone.get(key)}
83-
rescue
84-
exception ->
85-
{:error, exception}
86-
end
87-
8881
def list_unarchived_links_by_index_from_db do
8982
Repo.all(from l in Link, where: l.archived == false, order_by: [asc: l.index])
9083
end
@@ -121,6 +114,14 @@ defmodule CesiumLink.Links do
121114
%Link{}
122115
|> Link.changeset(attrs)
123116
|> Repo.insert()
117+
|> case do
118+
{:ok, link} ->
119+
safe_put_to_redis("links", list_unarchived_links_by_index_from_db())
120+
{:ok, link}
121+
122+
error ->
123+
error
124+
end
124125
end
125126

126127
@doc """
@@ -139,6 +140,14 @@ defmodule CesiumLink.Links do
139140
link
140141
|> Link.changeset(attrs)
141142
|> Repo.update()
143+
|> case do
144+
{:ok, link} ->
145+
safe_put_to_redis("links", list_unarchived_links_by_index_from_db())
146+
{:ok, link}
147+
148+
error ->
149+
error
150+
end
142151
end
143152

144153
@doc """
@@ -155,6 +164,14 @@ defmodule CesiumLink.Links do
155164
"""
156165
def delete_link(%Link{} = link) do
157166
Repo.delete(link)
167+
|> case do
168+
{:ok, link} ->
169+
safe_put_to_redis("links", list_unarchived_links_by_index_from_db())
170+
{:ok, link}
171+
172+
error ->
173+
error
174+
end
158175
end
159176

160177
@doc """
@@ -223,4 +240,18 @@ defmodule CesiumLink.Links do
223240
link
224241
|> update_link(%{visits: link.visits + 1})
225242
end
243+
244+
defp safe_get_from_redis(key) do
245+
{:ok, CesiumLink.Standalone.get(key)}
246+
rescue
247+
exception ->
248+
{:error, exception}
249+
end
250+
251+
defp safe_put_to_redis(key, value) do
252+
{:ok, CesiumLink.Standalone.put(key, value)}
253+
rescue
254+
exception ->
255+
{:error, exception}
256+
end
226257
end

lib/cesium_link/redis_cache.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ defmodule CesiumLink.Standalone do
33
Standalone redis cache.
44
"""
55
use Nebulex.Cache,
6-
otp_app: :safira,
6+
otp_app: :cesium_link,
77
adapter: NebulexRedisAdapter
88
end

lib/cesium_link_web/live/link_live/archived_live/index.ex

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule CesiumLinkWeb.ArchivedLive.Index do
22
use CesiumLinkWeb, :admin_live_view
33

44
alias CesiumLink.Links
5-
alias CesiumLink.Standalone
65

76
@impl true
87
def mount(_params, _session, socket) do
@@ -58,7 +57,6 @@ defmodule CesiumLinkWeb.ArchivedLive.Index do
5857
def handle_event("unarchive", %{"id" => id}, socket) do
5958
link = Links.get_link!(id)
6059
{:ok, _} = Links.unarchive_link(link)
61-
Standalone.put("links", Links.list_unarchived_links_by_index_from_db())
6260

6361
{:noreply,
6462
stream_delete(socket, :links, link)

lib/cesium_link_web/live/link_live/form_component.ex

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule CesiumLinkWeb.LinkLive.FormComponent do
22
use CesiumLinkWeb, :live_component
33

44
alias CesiumLink.Links
5-
alias CesiumLink.Standalone
65

76
@impl true
87
def render(assigns) do
@@ -59,7 +58,6 @@ defmodule CesiumLinkWeb.LinkLive.FormComponent do
5958
) do
6059
{:ok, link} ->
6160
notify_parent({:saved, link})
62-
Standalone.put("links", Links.list_unarchived_links_by_index_from_db())
6361

6462
{:noreply,
6563
socket
@@ -79,7 +77,6 @@ defmodule CesiumLinkWeb.LinkLive.FormComponent do
7977
) do
8078
{:ok, link} ->
8179
notify_parent({:saved, link})
82-
Standalone.put("links", Links.list_unarchived_links_by_index_from_db())
8380

8481
{:noreply,
8582
socket

lib/cesium_link_web/live/link_live/index.ex

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ defmodule CesiumLinkWeb.LinkLive.Index do
33

44
alias CesiumLink.Links
55
alias CesiumLink.Links.Link
6-
alias CesiumLink.Standalone
76

87
@impl true
98
def mount(_params, _session, socket) do
@@ -48,7 +47,6 @@ defmodule CesiumLinkWeb.LinkLive.Index do
4847
def handle_event("archive", %{"id" => id}, socket) do
4948
link = Links.get_link!(id)
5049
{:ok, _} = Links.archive_link(link)
51-
Standalone.put("links", Links.list_unarchived_links_by_index_from_db())
5250

5351
{:noreply,
5452
stream_delete(socket, :links, link)

0 commit comments

Comments
 (0)