Repository navigation
packages/django-postgres-cache, flows: count cache keys in the database - #25808
Conversation
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #25808 +/- ##
=======================================
Coverage 92.40% 92.40%
=======================================
Files 1191 1191
Lines 80332 80352 +20
Branches 4351 4351
=======================================
+ Hits 74227 74250 +23
+ Misses 6039 6036 -3
Partials 66 66
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
authentik PR Installation instructions Instructions for docker-composeAdd the following block to your AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-5c586a23fecd6994d886404d84e1dc8d9b5dce1e
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)sAfterwards, run the upgrade commands from the latest release notes. Instructions for KubernetesAdd the following block to your authentik:
outposts:
container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
image:
repository: ghcr.io/goauthentik/dev-server
tag: gh-5c586a23fecd6994d886404d84e1dc8d9b5dce1eAfterwards, run the upgrade commands from the latest release notes. |
BeryJu
left a comment
There was a problem hiding this comment.
Makes sense
the only I dont like is adding custom methods to the cache backend since django.core.cache's cache object is not typed at all
✅ Deploy Preview for authentik-storybook ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for authentik-integrations ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Playwright e2eDownload this run's HTML report and traces, then open the report: gh run download 40249045805 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-reportInstallation instructionsInstructions for docker-composeAdd the following block to your AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-a1b703a9f9a127bc904b7b1df549709528c893d5
# Distroless image: AUTHENTIK_TAG=gh-a1b703a9f9a127bc904b7b1df549709528c893d5-distroless
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)sAfterwards, run the upgrade commands from the latest release notes. Instructions for KubernetesAdd the following block to your authentik:
outposts:
container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
image:
repository: ghcr.io/goauthentik/dev-server
tag: gh-a1b703a9f9a127bc904b7b1df549709528c893d5
# Distroless image: tag: gh-a1b703a9f9a127bc904b7b1df549709528c893d5-distrolessAfterwards, run the upgrade commands from the latest release notes. |
|
|
||
| def get_cache_count(self, flow: Flow) -> int: | ||
| """Get count of cached flows""" | ||
| return len(cache.keys(f"{cache_key(flow)}*")) |
There was a problem hiding this comment.
I'd rather add a keys_count method on the cache backend.
There was a problem hiding this comment.
thats what it was and I what I voted against since the django.core.cache.cache object is un-typed I dont like adding custom methods to it, but I also don't like directly calling the ORM here
There was a problem hiding this comment.
I mean, keys already isn't standard
There was a problem hiding this comment.
ok fair enough, TIL keys isn't standard, lets do the custom one then
…ckend keys() is already a method of our backend rather than Django's cache API, so counting belongs next to it instead of in a direct CacheEntry query in the flow API. Both methods share one filter, so a count always matches the length of the key list for the same pattern and version.
# Conflicts: # packages/django-postgres-cache/django_postgres_cache/backend.py
* main: (89 commits) providers/scim: scope-aware adoption (#26174) sources/ldap: respect accountExpires attributes (#26135) web/flows: never let continuous login block the final redirect (#26269) website: document password hash import restrictions (#24235) core: restrict imported password hashers (#24234) core, web: update translations (#27148) server: retry requests on connections closed by a recycled worker (#27275) packages/django-postgres-cache, flows: count cache keys in the database (#25808) events: simplify execution of event rules/notifications (#26873) core: bump github.com/pires/go-proxyproto from 0.15.0 to 0.16.0 (#27260) core: bump goauthentik/platform-ssh from 26.04-ak-0.72.3 to 26.04-ak-0.72.4 in /tests/e2e (#27263) core: bump goauthentik/selenium from 155.0-ak-0.72.3 to 155.0-ak-0.72.4 in /tests/e2e (#27261) core: bump astral-sh/uv from 0.13.5 to 0.13.6 in /lifecycle/container (#27264) core: bump jsonpatch from 1.35 to 1.36 (#27269) outposts: refresh channel groups on hello (#27136) core: bump certifi from 2026.10.19 to 2026.10.23 (#27266) core: bump pluggy from 1.6.0 to v1.7.0 (#27238) core: bump markupsafe from 3.0.3 to v3.0.4 (#27225) core: bump httptools from 0.8.0 to v0.9.0 (#27219) core: bump iniconfig from 2.3.0 to v2.3.1 (#27221) ...
Flow cache statistics currently download every matching cache key and then count the strings in Python. This asks PostgreSQL for the count directly, so the database returns one number regardless of how many keys match.
The cache backend gets a
keys_count()method next tokeys(). Both build their query through one private filter method, so a count always matches the length of the key list for the same pattern and version. Exact keys, prefix wildcards, complex patterns, key versions, and the inclusion of expired entries behave as they do forkeys(). The flow serializer'scache_count, the flowcache_infoendpoint, and the cached-flows monitoring gauge now usekeys_count(). Cache clearing still lists keys, because it needs them to delete. Flow execution and cache expiry do not change.The policy cache endpoint and gauge still count with
len(cache.keys(...)). They can move tokeys_count()in a follow-up.This is a modest optimization, not a large endpoint speedup. The timings below measure one count operation against PostgreSQL, without HTTP or browser rendering. Both versions make one query; the gain is skipping the key transfer and the Python list. Counts matched at every size. Figures are medians of three runs after warmup up to 1,000 keys; 10,000 is one run per version.
Testing: a new backend test checks
keys_count()againstkeys()for exact, prefix, complex, and missing patterns, a second key version, and an expired entry. The flow API tests checkcache_countandcache_infoagainst cached plans for two flows, another key version, an expired entry, and an unrelated key. The API schema does not change.