bfee5a9e65564425834f7907557b7ec2fbf034c8
[anni] / docs / configuration / howto_set_richmedia_cache_ttl_based_on_image.md
1 # How to set rich media cache ttl based on image ttl
2 ## Explanation
3
4 Richmedia are cached without the ttl but the rich media may have image which can expire, like aws signed url.
5 In such cases the old image url (expired) is returned from the media cache.
6
7 So to avoid such situation we can define a module that will set ttl based on image.
8 The module must adopt behaviour `Pleroma.Web.RichMedia.Parser.TTL`
9
10 ### Example
11
12 ```exs
13 defmodule MyModule do
14   @behaviour Pleroma.Web.RichMedia.Parser.TTL
15
16   @impl Pleroma.Web.RichMedia.Parser.TTL
17   def ttl(data, url) do
18     image_url = Map.get(data, :image)
19     # do some parsing in the url and get the ttl of the image
20     # return ttl is unix time
21     parse_ttl_from_url(image_url)
22   end
23 end
24 ```
25
26 And update the config
27
28 ```exs
29 config :pleroma, :rich_media,
30   ttl_setters: [Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl, MyModule]
31 ```
32
33 > For reference there is a parser for AWS signed URL `Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl`, it's enabled by default.