Merge branch 'refactor/object-cache-deletion' into 'develop'

object cache deletion refactoring, part 2

See merge request pleroma/pleroma!409
This commit is contained in:
kaniini 2018-11-01 08:51:49 +00:00
commit 27a06bd440
7 changed files with 59 additions and 15 deletions

View file

@ -7,7 +7,7 @@ def run([nickname]) do
Mix.Task.run("app.start")
with %User{local: true} = user <- User.get_by_nickname(nickname) do
User.delete(user)
{:ok, _} = User.delete(user)
end
end
end

View file

@ -16,14 +16,30 @@ def start(_type, _args) do
supervisor(Pleroma.Web.Endpoint, []),
# Start your own worker by calling: Pleroma.Worker.start_link(arg1, arg2, arg3)
# worker(Pleroma.Worker, [arg1, arg2, arg3]),
worker(Cachex, [
:user_cache,
worker(
Cachex,
[
default_ttl: 25000,
ttl_interval: 1000,
limit: 2500
]
]),
:user_cache,
[
default_ttl: 25000,
ttl_interval: 1000,
limit: 2500
]
],
id: :cachex_user
),
worker(
Cachex,
[
:object_cache,
[
default_ttl: 25000,
ttl_interval: 1000,
limit: 2500
]
],
id: :cachex_object
),
worker(
Cachex,
[

View file

@ -37,7 +37,7 @@ def get_cached_by_ap_id(ap_id) do
else
key = "object:#{ap_id}"
Cachex.fetch!(:user_cache, key, fn _ ->
Cachex.fetch!(:object_cache, key, fn _ ->
object = get_by_ap_id(ap_id)
if object do
@ -56,8 +56,8 @@ def context_mapping(context) do
def delete(%Object{data: %{"id" => id}} = object) do
with Repo.delete(object),
Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
{:ok, true} <- Cachex.del(:user_cache, "object:#{id}") do
:ok
{:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
{:ok, object}
end
end
end

View file

@ -295,6 +295,7 @@ def update_and_set_cache(changeset) do
def invalidate_cache(user) do
Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
Cachex.del(:user_cache, "nickname:#{user.nickname}")
Cachex.del(:user_cache, "user_info:#{user.id}")
end
def get_cached_by_ap_id(ap_id) do
@ -656,7 +657,7 @@ def delete(%User{} = user) do
end
end)
:ok
{:ok, user}
end
def html_filter_policy(%User{info: %{"no_rich_text" => true}}) do

View file

@ -273,7 +273,7 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ tru
"to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
}
with Object.delete(object),
with {:ok, _} <- Object.delete(object),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity),
{:ok, _actor} <- User.decrease_note_count(user) do

View file

@ -42,7 +42,7 @@ test "ensures cache is cleared for the object" do
Object.delete(cached_object)
{:ok, nil} = Cachex.get(:user_cache, "object:#{object.data["id"]}")
{:ok, nil} = Cachex.get(:object_cache, "object:#{object.data["id"]}")
cached_object = Object.get_cached_by_ap_id(object.data["id"])

View file

@ -511,7 +511,7 @@ test ".delete deactivates a user, all follow relationships and all create activi
{:ok, _, _} = CommonAPI.favorite(activity.id, follower)
{:ok, _, _} = CommonAPI.repeat(activity.id, follower)
:ok = User.delete(user)
{:ok, _} = User.delete(user)
followed = Repo.get(User, followed.id)
follower = Repo.get(User, follower.id)
@ -551,4 +551,31 @@ test "html_filter_policy returns TwitterText scrubber when rich-text is disabled
assert Pleroma.HTML.Scrubber.TwitterText == User.html_filter_policy(user)
end
end
describe "caching" do
test "invalidate_cache works" do
user = insert(:user)
user_info = User.get_cached_user_info(user)
User.invalidate_cache(user)
{:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
{:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
{:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
end
test "User.delete() plugs any possible zombie objects" do
user = insert(:user)
{:ok, _} = User.delete(user)
{:ok, cached_user} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
assert cached_user != user
{:ok, cached_user} = Cachex.get(:user_cache, "nickname:#{user.ap_id}")
assert cached_user != user
end
end
end