From: rinpatch Date: Wed, 16 Jan 2019 07:26:01 +0000 (+0300) Subject: add caching X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=70b2bb6eded5597bd768d7dc8754c2c994561aee;p=akkoma add caching --- diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index ad2797209..5f9518914 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -84,6 +84,17 @@ defmodule Pleroma.Application do ], id: :cachex_scrubber ), + worker( + Cachex, + [ + :metadata_cache, + [ + limit: 2500, + default_ttl: :timer.minutes(15) + ] + ], + id: :cachex_metadata + ), worker( Cachex, [ diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata.ex index cf2b86aaa..a5a706b8f 100644 --- a/lib/pleroma/web/metadata.ex +++ b/lib/pleroma/web/metadata.ex @@ -2,6 +2,16 @@ defmodule Pleroma.Web.Metadata do alias Phoenix.HTML @parsers Pleroma.Config.get([:metadata, :providers], []) + + def get_cached_tags(params) do + # I am unsure how well ETS works with big keys + key = :erlang.term_to_binary(params) + + Cachex.fetch!(:metadata_cache, key, fn _key -> + {:commit, build_tags(params)} + end) + end + def build_tags(params) do Enum.reduce(@parsers, "", fn parser, acc -> rendered_html = diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 25e866c48..1ecd4aee1 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -534,7 +534,7 @@ defmodule Fallback.RedirectController do def redirector_with_meta(conn, params) do {:ok, index_content} = File.read(index_file_path()) - tags = Metadata.build_tags(params) + tags = Metadata.get_cached_tags(params) response = String.replace(index_content, "", tags) conn