add caching
authorrinpatch <rinpatch@sdf.org>
Wed, 16 Jan 2019 07:26:01 +0000 (10:26 +0300)
committerrinpatch <rinpatch@sdf.org>
Wed, 16 Jan 2019 07:26:01 +0000 (10:26 +0300)
lib/pleroma/application.ex
lib/pleroma/web/metadata.ex
lib/pleroma/web/router.ex

index ad27972091e4913e9e3683d8509cfd919b33739e..5f9518914d20bfbf06b4fff63a1efbda8d13e31a 100644 (file)
@@ -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,
           [
index cf2b86aaa9a0790ca0e4fd232756c91abb29d945..a5a706b8fabfe2b8628d99a6bbced03d57c7d0d6 100644 (file)
@@ -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 =
index 25e866c4897746b96a66ed990632e2613c026368..1ecd4aee1a03a8eae334d0c7254eb335c9c1cc76 100644 (file)
@@ -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, "<!--server-generated-meta-->", tags)
 
     conn