@@ -78,13 +78,6 @@ defmodule CesiumLink.Links do
78
78
end
79
79
end
80
80
81
- defp safe_get_from_redis ( key ) do
82
- { :ok , CesiumLink.Standalone . get ( key ) }
83
- rescue
84
- exception ->
85
- { :error , exception }
86
- end
87
-
88
81
def list_unarchived_links_by_index_from_db do
89
82
Repo . all ( from l in Link , where: l . archived == false , order_by: [ asc: l . index ] )
90
83
end
@@ -121,6 +114,14 @@ defmodule CesiumLink.Links do
121
114
% Link { }
122
115
|> Link . changeset ( attrs )
123
116
|> 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
124
125
end
125
126
126
127
@ doc """
@@ -139,6 +140,14 @@ defmodule CesiumLink.Links do
139
140
link
140
141
|> Link . changeset ( attrs )
141
142
|> 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
142
151
end
143
152
144
153
@ doc """
@@ -155,6 +164,14 @@ defmodule CesiumLink.Links do
155
164
"""
156
165
def delete_link ( % Link { } = link ) do
157
166
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
158
175
end
159
176
160
177
@ doc """
@@ -223,4 +240,18 @@ defmodule CesiumLink.Links do
223
240
link
224
241
|> update_link ( % { visits: link . visits + 1 } )
225
242
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
226
257
end
0 commit comments