Cache expiry seconds

Write codeEasy16 min

Your task

A shared CDN edge has just stored a response and must decide how many seconds it may serve that copy before asking the origin again. The response's cache directives arrive as separate strings, each either a bare name such as public or a name and value joined by an equals sign such as max-age=600. A directive may carry surrounding spaces and any letter case. Apply these rules in this order and return the number of seconds. Return -1 when no-store or private appears anywhere, since a shared cache may not keep the response at all. Return 0 when no-cache appears, since every hit must be revalidated. Otherwise return the value of s-maxage, then the value of max-age, using the first occurrence of a repeated name and skipping any value that is not a run of digits. Otherwise return default_ttl. Ignore all other directives.

Examples

edge_cache_seconds(directives: list[str], default_ttl: int) -> int
Example 1
Inputedge_cache_seconds(["public", "max-age=600"], 60)
Output600
Example 2
Inputedge_cache_seconds(["max-age=600", "s-maxage=120"], 60)
Output120
Example 3
Inputedge_cache_seconds(["no-store"], 60)
Output-1