aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/object.ex
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2019-02-03 18:28:14 +0100
committerlain <lain@soykaf.club>2019-02-03 18:28:14 +0100
commit505a084058eeeed7d945b43630c97c38cafec656 (patch)
tree4622342d8a086b77418b21a349ca042b3f8faf4e /lib/pleroma/object.ex
parentd5d91ae689e14103551dd3622e208ea31e40c858 (diff)
downloadpleroma-505a084058eeeed7d945b43630c97c38cafec656.tar.gz
Still do caching in tests.
Diffstat (limited to 'lib/pleroma/object.ex')
-rw-r--r--lib/pleroma/object.ex43
1 files changed, 25 insertions, 18 deletions
diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex
index 1088bb5e4..7b46a3b05 100644
--- a/lib/pleroma/object.ex
+++ b/lib/pleroma/object.ex
@@ -42,24 +42,18 @@ defmodule Pleroma.Object do
# Legacy objects can be mutated by anybody
def authorize_mutation(%Object{}, %User{}), do: true
- if Mix.env() == :test do
- def get_cached_by_ap_id(ap_id) do
- get_by_ap_id(ap_id)
- end
- else
- 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)
- end
+ 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)
end
def context_mapping(context) do
@@ -90,4 +84,17 @@ defmodule Pleroma.Object do
{:ok, object}
end
end
+
+ def set_cache(%Object{data: %{"id" => ap_id}} = object) do
+ Cachex.put(:object_cache, "object:#{ap_id}", object)
+ {:ok, object}
+ end
+
+ def update_and_set_cache(changeset) do
+ with {:ok, object} <- Repo.update(changeset) do
+ set_cache(object)
+ else
+ e -> e
+ end
+ end
end