ActivityPub actions & side-effects in transaction
[akkoma] / lib / pleroma / object.ex
index f316f8b36c062018892a343a846747a035541df4..20aba4c15e36ec7820a17e58ce50acfa93b9471f 100644 (file)
@@ -145,18 +145,18 @@ defmodule Pleroma.Object do
   # Legacy objects can be mutated by anybody
   def authorize_mutation(%Object{}, %User{}), do: true
 
+  @spec get_cached_by_ap_id(String.t()) :: Object.t() | nil
   def get_cached_by_ap_id(ap_id) do
     key = "object:#{ap_id}"
 
-    Cachex.fetch!(:object_cache, key, fn _ ->
-      object = get_by_ap_id(ap_id)
-
-      if object do
-        {:commit, object}
-      else
-        {:ignore, object}
-      end
-    end)
+    with {:ok, nil} <- Cachex.get(:object_cache, key),
+         object when not is_nil(object) <- get_by_ap_id(ap_id),
+         {:ok, true} <- Cachex.put(:object_cache, key, object) do
+      object
+    else
+      {:ok, object} -> object
+      nil -> nil
+    end
   end
 
   def context_mapping(context) do