You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-5Lines changed: 11 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,10 +235,16 @@ The ORM integration is optional - SQLAlchemy and Django are not required depende
235
235
236
236
## How It Works
237
237
238
+
### Use-case
239
+
240
+
Let's say you have a lot of functions, all centered around returning same logical `User` entity, but in different shape and forms. All of them returns same `User` data.
241
+
242
+
Now, imagine caching the varied outputs of these functions while maintaining invalidation control: when a User changes, you want all related caches (unique function calls) to clear automatically, guided by events - a.k.a implement event-driven invalidation. Here, we cannot use basic cache decorator libraries as they lack the ability to link functions to the entities they involve. This is where this library got you covered.
243
+
238
244
When a function is decorated with `@cache(entity="user")`:
239
245
240
-
1. The decorator **caches the function result as we know it, nothing fancy*
241
-
2. It **extracts entity reference IDs** from the result (e.g., `{"id": 42, ...}`
246
+
1. The decorator **caches the function result as we know it*
247
+
2. It **extracts entity reference IDs** from any result (e.g., `{"id": 42, ...}` or list of `User`s)
242
248
3. It **creates an reverse index** mapping for each entity to cache specific **function calls** containing it
243
249
244
250
When an entity changes:
@@ -248,7 +254,7 @@ When an entity changes:
248
254
3. Only those **specific caches/function calls are invalidated**
249
255
250
256
251
-
Effectively, you end up using traditional caches that can be granularly invalidated within your ecosystem. This means you don't need to remember all the different ways an entity might be cached and change your 'read' codebase - just invalidate by entity ID, and all relevant caches are automatically cleared.
257
+
Effectively, you end up using traditional cache decorator that can be granularly invalidated within your ecosystem giving you near real-time data consistency. This means you don't need to remember all the different ways an entity might be cached and glue that into your read codebase - just invalidate by entity ID, and all relevant caches are automatically cleared.
252
258
253
259
>❗ To ensure cache consistency across the system, please bear in mind these rules:
254
260
>* Maintain idempotency across all functions using the same cache key (cache key being - function or entity signature)
0 commit comments