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
+6-7Lines changed: 6 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,8 +91,8 @@ memory_cache = EntityCache()
91
91
#### With Entity Tracking
92
92
93
93
```python
94
-
# Cache function results and track entity references
95
-
@cache('user') # "user" - tags a system-wide entity for unified tracking by their reference
94
+
# Cache function results and track entity reference for invalidation
95
+
@cache('user') # "user" - is entity used later *only for invalidation*
96
96
defget_user(user_id):
97
97
# Your database/interservice query here
98
98
return {"id": user_id, "name": f"User {user_id}"}
@@ -243,8 +243,8 @@ Now, imagine caching the varied outputs of these functions while maintaining inv
243
243
244
244
When a function is decorated with `@cache(entity="user")`:
245
245
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)
246
+
1. The decorator **caches the function result as usual**
247
+
2. It **extracts entity reference IDs** from any result (e.g., `[{"id": 42, ...}, ...]` or `[User(id=42,...), ...]`)
248
248
3. It **creates an reverse index** mapping for each entity to cache specific **function calls** containing it
249
249
250
250
When an entity changes:
@@ -257,7 +257,6 @@ When an entity changes:
257
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.
258
258
259
259
>❗ To ensure cache consistency across the system, please bear in mind these rules:
260
-
>* Maintain idempotency across all functions using the same cache key (cache key being - function or entity signature)
261
260
>* Ensure entity identity consistency - an entity with a specific ID must represent the identical data object across all system components.
262
261
263
262
## Why this library?
@@ -275,7 +274,7 @@ Write-through caching keeps data consistent but couples your read and cache laye
275
274
276
275
### What it does?
277
276
278
-
This library provides the classic and convenient read-through caching decorator for your functions with significant enhancement. When an entity referenced in those specific function calls is updated, the cache can be easily invalidated either automatically or manually, as long as you provide the reference to track it. It integrates easily into your existing codebase—unlike write-through caching—and supports a wide range of data structures, including Django ORM models, SQLAlchemy objects, or basic lists of dictionaries, with minimal overhead. This approach allows to use write-around caching very effectively by tracking all invalidation points across your platform while maintaining the same flexibility of read-through cache
277
+
This library provides the classic and convenient read-through caching decorator for your functions with significant enhancement. When an entity is updated, it traces all function calls containing this entity and clears the cache either automatically or manually, as long as you provide the reference to track it in function decorator. It integrates seamleassly into your existing codebase—unlike write-through caching—and supports a wide range of data structures, including Django ORM models, SQLAlchemy objects, or basic lists of dictionaries, with minimal overhead. This approach allows to use write-around caching very effectively by tracking all invalidation points across your platform while maintaining the same flexibility of read-through cache
279
278
280
279
## Advanced Usage
281
280
@@ -302,7 +301,7 @@ def get_filtered_users(user_ids): # Completely different function, but same log
0 commit comments