rich media: use cachex to avoid flooding remote servers
authorWilliam Pitcock <nenolod@dereferenced.org>
Fri, 4 Jan 2019 23:23:47 +0000 (23:23 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Fri, 4 Jan 2019 23:32:01 +0000 (23:32 +0000)
lib/pleroma/application.ex
lib/pleroma/web/rich_media/parser.ex

index cb3e6b69b0c43f3e16ce5bf8ae60635cca21740a..ad27972091e4913e9e3683d8509cfd919b33739e 100644 (file)
@@ -63,6 +63,17 @@ defmodule Pleroma.Application do
           ],
           id: :cachex_object
         ),
+        worker(
+          Cachex,
+          [
+            :rich_media_cache,
+            [
+              default_ttl: :timer.minutes(120),
+              limit: 5000
+            ]
+          ],
+          id: :cachex_rich_media
+        ),
         worker(
           Cachex,
           [
index 477a381964b0a6f6da1dc776af119a97997f4a72..b88ed537103493eaa79075ec5ce6408240034054 100644 (file)
@@ -2,9 +2,13 @@ defmodule Pleroma.Web.RichMedia.Parser do
   @parsers [Pleroma.Web.RichMedia.Parsers.OGP]
 
   def parse(url) do
-    {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
+    Cachex.fetch!(:rich_media_cache, url, fn _ ->
+      {:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
 
-    html |> maybe_parse() |> get_parsed_data()
+      result = html |> maybe_parse() |> get_parsed_data()
+
+      {:commit, result}
+    end)
   end
 
   defp maybe_parse(html) do