Add basic user caching.
Expires after 5 seconds.
This commit is contained in:
parent
90b38fd51b
commit
03ddaead7e
7 changed files with 41 additions and 4 deletions
|
|
@ -14,6 +14,10 @@ defmodule Pleroma.Application do
|
|||
supervisor(Pleroma.Web.Endpoint, []),
|
||||
# Start your own worker by calling: Pleroma.Worker.start_link(arg1, arg2, arg3)
|
||||
# worker(Pleroma.Worker, [arg1, arg2, arg3]),
|
||||
supervisor(ConCache, [[
|
||||
ttl_check: :timer.seconds(1),
|
||||
ttl: :timer.seconds(5)
|
||||
], [name: :users]])
|
||||
]
|
||||
|
||||
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
|
||||
|
|
|
|||
|
|
@ -52,4 +52,17 @@ defmodule Pleroma.User do
|
|||
def following?(%User{} = follower, %User{} = followed) do
|
||||
Enum.member?(follower.following, User.ap_followers(followed))
|
||||
end
|
||||
|
||||
def get_cached_by_ap_id(ap_id) do
|
||||
ConCache.get_or_store(:users, "ap_id:#{ap_id}", fn() ->
|
||||
# Return false so the cache will store it.
|
||||
Repo.get_by(User, ap_id: ap_id) || false
|
||||
end)
|
||||
end
|
||||
|
||||
def get_cached_by_nickname(nickname) do
|
||||
ConCache.get_or_store(:users, "nickname:#{nickname}", fn() ->
|
||||
Repo.get_by(User, nickname: nickname) || false
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
Regex.scan(regex, text)
|
||||
|> List.flatten
|
||||
|> Enum.uniq
|
||||
|> Enum.map(fn ("@" <> match = full_match) -> {full_match, Repo.get_by(User, nickname: match)} end)
|
||||
|> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
|
||||
|> Enum.filter(fn ({_match, user}) -> user end)
|
||||
end
|
||||
|
||||
|
|
@ -205,7 +205,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
# For likes, fetch the liked activity, too.
|
||||
defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = Repo.get_by!(User, ap_id: actor)
|
||||
user = User.get_cached_by_ap_id(actor)
|
||||
[liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
|
||||
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity}))
|
||||
|
|
@ -213,8 +213,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
|
||||
defp activity_to_status(activity, opts) do
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = Repo.get_by!(User, ap_id: actor)
|
||||
mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
|
||||
user = User.get_cached_by_ap_id(actor)
|
||||
# mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
|
||||
mentioned_users = Enum.map(activity.data["to"], fn (ap_id) ->
|
||||
User.get_cached_by_ap_id(ap_id)
|
||||
end)
|
||||
|> Enum.filter(&(&1))
|
||||
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue