base_cache
Generic base cache class for analytics results with Redis support.
Provides a thread-safe singleton cache that can store typed Pydantic models with Redis distributed caching and in-memory fallback. Supports stale-while-revalidate pattern when cache hydration is enabled.
RedisCache
Bases: ABC
Generic base class for Redis-backed singleton caches.
Provides common caching infrastructure with Redis distributed caching and in-memory fallback. Thread-safe with RLock pattern.
When cache hydration is enabled (via settings), implements stale-while-revalidate: - Normal TTL controls when background hydration refreshes the cache - Stale TTL controls maximum age before returning None (forcing computation) - Clients always receive cached data immediately while hydration runs in background
Subclasses must implement: - _get_cache_key_prefix(): Return the Redis key prefix for this cache type - _get_ttl(): Return the TTL in seconds for cache entries - _get_model_class(): Return the Pydantic model class for deserialization - _register_invalidation_callback(): Set up automatic invalidation hooks
Type parameter T must be a Pydantic BaseModel subclass.
Source code in src/horde_model_reference/analytics/base_cache.py
28 29 30 31 32 33 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 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 395 396 397 398 399 400 401 402 403 404 405 | |
__new__
Singleton pattern matching ModelReferenceManager.
_initialize
Initialize caching infrastructure.
Source code in src/horde_model_reference/analytics/base_cache.py
_get_cache_key_prefix
abstractmethod
Get the Redis key prefix for this cache type.
Returns:
-
str–Redis key prefix string (e.g., "horde:stats" or "horde:deletion_risk").
_get_ttl
abstractmethod
_get_model_class
abstractmethod
Get the Pydantic model class for deserialization.
Returns:
-
type[T]–Pydantic model class (e.g., CategoryStatistics or CategoryDeletionRiskResponse).
Source code in src/horde_model_reference/analytics/base_cache.py
_register_invalidation_callback
abstractmethod
Register callback with ModelReferenceManager for automatic invalidation.
Should call manager.backend.register_invalidation_callback() if available.
Source code in src/horde_model_reference/analytics/base_cache.py
_build_cache_key
_build_cache_key(
category: MODEL_REFERENCE_CATEGORY,
grouped: bool = False,
include_backend_variations: bool = False,
) -> str
Build cache key from category and options.
Parameters:
-
category(MODEL_REFERENCE_CATEGORY) –The model reference category.
-
grouped(bool, default:False) –Whether this is for grouped text models.
-
include_backend_variations(bool, default:False) –Whether backend variations are included.
Returns:
-
str–Cache key string.
Source code in src/horde_model_reference/analytics/base_cache.py
_get_redis_key
get
get(
category: MODEL_REFERENCE_CATEGORY,
grouped: bool = False,
include_backend_variations: bool = False,
allow_stale: bool | None = None,
) -> T | None
Get cached result for a category.
Checks Redis first (if available), then in-memory cache.
When cache hydration is enabled (settings.cache_hydration_enabled=True) and allow_stale is True (or None with hydration enabled), implements stale-while-revalidate: - Returns cached data even if past normal TTL - Only returns None if data exceeds stale_ttl (default 1 hour) - Background hydration is expected to refresh data before stale_ttl
Parameters:
-
category(MODEL_REFERENCE_CATEGORY) –The model reference category.
-
grouped(bool, default:False) –Whether to get grouped text models variant.
-
include_backend_variations(bool, default:False) –Whether backend variations are included.
-
allow_stale(bool | None, default:None) –Whether to return stale data beyond normal TTL. If None, defaults to True when hydration is enabled, False otherwise.
Returns:
-
T | None–Cached result or None if not cached or expired beyond stale TTL.
Source code in src/horde_model_reference/analytics/base_cache.py
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 | |
is_fresh
is_fresh(
category: MODEL_REFERENCE_CATEGORY,
grouped: bool = False,
include_backend_variations: bool = False,
) -> bool
Check if cached data is fresh (within normal TTL).
Useful for determining if background hydration should run.
Parameters:
-
category(MODEL_REFERENCE_CATEGORY) –The model reference category.
-
grouped(bool, default:False) –Whether to check grouped text models variant.
-
include_backend_variations(bool, default:False) –Whether backend variations are included.
Returns:
-
bool–True if fresh data exists within TTL, False otherwise.
Source code in src/horde_model_reference/analytics/base_cache.py
set
set(
category: MODEL_REFERENCE_CATEGORY,
result: T,
grouped: bool = False,
include_backend_variations: bool = False,
) -> None
Store result in cache.
Stores in both Redis (if available) and in-memory cache.
Parameters:
-
category(MODEL_REFERENCE_CATEGORY) –The model reference category.
-
result(T) –The computed result to cache.
-
grouped(bool, default:False) –Whether this is the grouped text models variant.
-
include_backend_variations(bool, default:False) –Whether backend variations are included.
Source code in src/horde_model_reference/analytics/base_cache.py
invalidate
invalidate(
category: MODEL_REFERENCE_CATEGORY,
grouped: bool | None = None,
include_backend_variations: bool | None = None,
) -> None
Invalidate cached results for a category.
Removes from both Redis and in-memory cache. If grouped is None, invalidates all grouped/ungrouped variants. If include_backend_variations is None, invalidates all variation states.
Parameters:
-
category(MODEL_REFERENCE_CATEGORY) –The model reference category to invalidate.
-
grouped(bool | None, default:None) –Whether to invalidate grouped variant (None = both).
-
include_backend_variations(bool | None, default:None) –Whether to invalidate variation states (None = both).
Source code in src/horde_model_reference/analytics/base_cache.py
clear_all
Clear all cached results.
Useful for testing or when a global cache reset is needed.
Source code in src/horde_model_reference/analytics/base_cache.py
get_cache_info
Get information about the current cache state.
Returns:
-
dict[str, int | float | bool | list[str]]–Dictionary with cache statistics including size, Redis status, TTL.