Redis Cache and Session Internals
Session backend
codex_django.sessions.backends.redis
Redis-backed SessionBase implementation.
Wire this up as SESSION_ENGINE = "codex_django.sessions.backends.redis".
Storage contract:
- Session payload is stored as the Django encoded string produced by
:py:meth:
SessionBase.encode. No pickle is used — the project must rely on a JSON-basedSESSION_SERIALIZER(Django's defaultdjango.contrib.sessions.serializers.JSONSerializeris fine). - Redis key format:
{PROJECT_NAME}:{CODEX_SESSION_KEY_PREFIX}:{session_key}. - TTL equals
self.get_expiry_age()(driven bySESSION_COOKIE_AGE/ per-session overrides). - Redis failures propagate as
:py:class:
codex_platform.redis_service.exceptions.RedisConnectionError(orRedisServiceError). They are not swallowed — a broken Redis must never look like a valid empty session.
Legacy django-redis / pickled sessions are not readable by this backend
and do not need to be migrated — old keys are simply ignored and the user
re-authenticates.
Classes
SessionStore
Bases: SessionBase
Session store that persists session payloads in Redis as strings.
Instances are cheap and stateless with respect to Redis connections — the manager builds clients lazily.
Source code in src/codex_django/sessions/backends/redis.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
Functions
clear_expired()
classmethod
Redis expires keys on its own — nothing to sweep.
Source code in src/codex_django/sessions/backends/redis.py
184 185 186 187 | |
Functions
Cache backend
codex_django.cache.backends.redis
Redis-backed Django cache backend (JSON, no pickle).
Wire this up as::
CACHES = {
"default": {
"BACKEND": "codex_django.cache.backends.redis.RedisCache",
"LOCATION": REDIS_URL, # optional; falls back to settings.REDIS_URL
"KEY_PREFIX": PROJECT_NAME, # required for clear() to be namespace-scoped
"TIMEOUT": 300,
"OPTIONS": {
# Optional: "SERIALIZER": "my_app.cache.CustomSerializer",
},
}
}
Design highlights:
- Values are stored as UTF-8 JSON strings (see
:mod:
codex_django.cache.serializers). Non-JSON-native values raiseTypeError; there is no pickle fallback. adduses RedisSET NX EXatomically.clearusesSCAN + DELscoped to{key_prefix}:*. IfKEY_PREFIXis empty,clearrefuses to run — neverFLUSHDB.- Redis errors propagate to the caller as
:class:
codex_platform.redis_service.exceptions.RedisConnectionError/RedisServiceError— the cache does not silently degrade.
Classes
RedisCache
Bases: BaseCache
Django cache backend that stores JSON strings in Redis.
Source code in src/codex_django/cache/backends/redis.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
JSON serializer
codex_django.cache.serializers
Serializers for the Redis cache backend.
Default policy: strict JSON. Any value that cannot be encoded as JSON
raises :class:TypeError — the backend never silently falls back to
pickle. Callers that need to cache non-JSON-native values (datetime,
Decimal, UUID, set …) should use the helpers in
:mod:codex_django.cache.values to convert them explicitly.
A downstream project may swap the serializer via OPTIONS["SERIALIZER"]
(dotted path to a class implementing :class:Serializer).
Classes
Serializer
Bases: Protocol
Minimal cache serializer interface.
Source code in src/codex_django/cache/serializers.py
19 20 21 22 23 24 25 26 | |
JsonSerializer
Strict JSON serializer — UTF-8, no ensure_ascii escaping.
Source code in src/codex_django/cache/serializers.py
29 30 31 32 33 34 35 36 | |
CacheCoder
codex_django.cache.values
Explicit helpers for caching non-JSON-native Python values.
The Redis cache backend stores values as strict JSON and raises TypeError
for anything that json.dumps cannot handle (datetime, date,
Decimal, UUID, set …). Rather than smuggle magic type tags into
the serializer (which would leak into every Redis consumer and make reverting
impossible), we expose an explicit coder that callers invoke themselves::
from codex_django.cache.values import CacheCoder
cache.set("user:42:last_seen", CacheCoder.dump_datetime(user.last_seen), 3600)
raw = cache.get("user:42:last_seen")
last_seen = CacheCoder.load_datetime(raw) if raw else None
The dump() / load() helpers cover ad-hoc payloads (nested dict /
list / tuple); load() requires explicit hints because JSON
does not preserve Python types.
Classes
CacheCoder
Round-trip helpers for non-JSON-native values.
Every helper is a pure function — no Redis access, no Django settings lookup — so the class is importable and usable anywhere.
Source code in src/codex_django/cache/values.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
Functions
dump_bytes(value)
staticmethod
Encode bytes as a hex string (roundtrip-safe, ASCII).
Source code in src/codex_django/cache/values.py
113 114 115 116 | |
dump(value)
classmethod
Best-effort conversion of a nested structure to JSON-native types.
Recurses through dict / list / tuple. Any value of an
unknown type is returned as-is — JSON serialization will raise
TypeError on it later, which is the intended strict-mode signal.
Source code in src/codex_django/cache/values.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
Shared adapter
codex_django.core.redis.django_adapter
Shared Redis builders for Django session and cache backends.
Provides the minimal surface needed by codex_django.sessions and
codex_django.cache without going through BaseDjangoRedisManager.
We intentionally do NOT reuse BaseDjangoRedisManager here because
session/cache backends must fail loud on Redis outage; the _is_disabled
shortcut (used by domain managers in DEBUG mode) is wrong for them.
Functions
build_redis_client(url=None)
Return an async Redis client with decode_responses=True.
Strings are always returned as str (not bytes) to match the
behavior relied upon by Django's session/cache layers.
Source code in src/codex_django/core/redis/django_adapter.py
24 25 26 27 28 29 30 31 | |
build_redis_service(url=None)
Return a RedisService bound to an async Redis client.
Source code in src/codex_django/core/redis/django_adapter.py
34 35 36 | |
namespaced_key(prefix, key, *, project_name=None)
Build a colon-delimited Redis key: {PROJECT_NAME}:{prefix}:{key}.
Empty segments are omitted. Mirrors BaseDjangoRedisManager.make_key.
Source code in src/codex_django/core/redis/django_adapter.py
39 40 41 42 43 44 45 46 47 | |