@@ -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
226257end
0 commit comments