489f9ece8b531ee16e4d02469f739b8cf4e819bb
[akkoma] / docs / config / 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 moddule that will set ttl based no image.
8
9 The module must have a `run` function and it should be registered in the config.
10
11 ### Example
12
13 ```exs
14 defmodule MyModule do
15 def run(data, url) do
16 image_url = Map.get(data, :image)
17 # do some parsing in the url and get the ttl of the image
18 # ttl is unix time
19 ttl = parse_ttl_from_url(image_url)
20 Cachex.expire_at(:rich_media_cache, url, ttl * 1000)
21 end
22 end
23 ```
24
25 And update the config
26
27 ```exs
28 config :pleroma, :rich_media,
29 ttl_setters: [Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl, MyModule]
30 ```
31
32 > For reference there is a parser for AWS signed URL `Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl`, it's enabled by default.