commit: ba17518a0aff404e265f4aebec1257912ad2750c
parent: bf5aaefbb56a8ff8ac4e43874dacd794f3deda84
Author: rinpatch <rinpatch@sdf.org>
Date: Fri, 4 Jan 2019 23:56:42 +0000
Merge branch 'feature/rich-media-cachex' into 'develop'
rich media: use cachex to avoid flooding remote servers
See merge request pleroma/pleroma!629
Diffstat:
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
@@ -66,6 +66,17 @@ defmodule Pleroma.Application do
worker(
Cachex,
[
+ :rich_media_cache,
+ [
+ default_ttl: :timer.minutes(120),
+ limit: 5000
+ ]
+ ],
+ id: :cachex_rich_media
+ ),
+ worker(
+ Cachex,
+ [
:scrubber_cache,
[
limit: 2500
diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex
@@ -1,7 +1,14 @@
defmodule Pleroma.Web.RichMedia.Parser do
@parsers [Pleroma.Web.RichMedia.Parsers.OGP]
- def parse(url) do
+ if Mix.env() == :test do
+ def parse(url), do: parse_url(url)
+ else
+ def parse(url),
+ do: {:commit, Cachex.fetch!(:rich_media_cache, url, fn _ -> parse_url(url) end)}
+ end
+
+ defp parse_url(url) do
{:ok, %Tesla.Env{body: html}} = Pleroma.HTTP.get(url)
html |> maybe_parse() |> get_parsed_data()