Merge branch 'feature/rich-media-cachex' into 'develop'

rich media: use cachex to avoid flooding remote servers

See merge request pleroma/pleroma!629
This commit is contained in:
rinpatch 2019-01-04 23:56:42 +00:00
commit ba17518a0a
2 changed files with 19 additions and 1 deletions

View 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,
[

View File

@ -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()