From 70b2bb6eded5597bd768d7dc8754c2c994561aee Mon Sep 17 00:00:00 2001 From: rinpatch Date: Wed, 16 Jan 2019 10:26:01 +0300 Subject: [PATCH] add caching --- lib/pleroma/application.ex | 11 +++++++++++ lib/pleroma/web/metadata.ex | 10 ++++++++++ lib/pleroma/web/router.ex | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) 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 @@ def start(_type, _args) 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 @@ def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} 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