From 94331ef108ad160faddb48cfc6a259c8a2497c99 Mon Sep 17 00:00:00 2001 From: Simon Leiner Date: Tue, 14 Mar 2023 18:23:40 +0100 Subject: Allow regexes for URL expiration patterns This allows for more fine-grained control over URL patterns than globbing in the rare cases where that is needed. --- docs/user_guide/expiration.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/user_guide/expiration.md b/docs/user_guide/expiration.md index d4b2fa5..9b6b50b 100644 --- a/docs/user_guide/expiration.md +++ b/docs/user_guide/expiration.md @@ -65,23 +65,25 @@ Examples: ``` (url-patterns)= -## Expiration With URL Patterns -You can use `urls_expire_after` to set different expiration values based on URL glob patterns: ```python >>> urls_expire_after = { ... '*.site_1.com': 30, ... 'site_2.com/resource_1': 60 * 2, ... 'site_2.com/resource_2': 60 * 60 * 24, +... re.compile(r'site_2.com/resource_\d'): 60 * 60 * 24 * 7, +... 'site_2.com/resource_*': 60 * 60, ... 'site_2.com/static': NEVER_EXPIRE, ... } >>> session = CachedSession(urls_expire_after=urls_expire_after) ``` **Notes:** -- `urls_expire_after` should be a dict in the format `{'pattern': expire_after}` +- `urls_expire_after` should be a dict in the format `{pattern': expire_after}` - `expire_after` accepts the same types as `CachedSession.settings.expire_after` -- Patterns will match request **base URLs without the protocol**, so the pattern `site.com/resource/` - is equivalent to `http*://site.com/resource/**` +- **Glob patterns** will match request **base URLs without the protocol**, so the pattern `site.com/resource/` + is equivalent to `http*://site.com/resource/**`. + For **regex patterns**, the **whole URL** will be matched, so you _can_ put restrictions on the protocol, e.g. + `re.compile(r'https://site.com/.*')`. - If there is more than one match, the first match will be used in the order they are defined - If no patterns match a request, `CachedSession.settings.expire_after` will be used as a default - See {ref}`url-filtering` for an example of using `urls_expire_after` as an allowlist -- cgit v1.2.1