@@ -197,6 +197,18 @@ def update_user(user_id, name):
197197# For manually resetting entity pass table name
198198# for composite key pass tuple
199199cache.invalidate_entity(User.__table__, user_id)
200+
201+
202+
203+ # Complex queries
204+
205+ # Here we explicitly *have* to say that we stick to order entity to be tracked for changes
206+ @memory_cache (Order, id_key = lambda item : item[0 ].id)
207+ def get_orders_with_user (session : Session, order_id : str ) -> List[Row[Tuple[Order, User]]]:
208+ nonlocal call_count
209+ call_count += 1
210+ return session.query(Order, User).join(User).filter(Order.id == order_id).all()
211+
200212```
201213
202214#### Django Integration
@@ -355,6 +367,7 @@ Main class for creating cache decorators.
355367``` python
356368cache = EntityCache(
357369 backend = None , # CacheBackend instance (optional, will use in-memory if None)
370+ global_supported_id_types = (int , str , UUID ) # Tuple of primitive types that are considered valid entity IDs.
358371 locked_ttl = 3600 , # Default locked TTL in seconds, in case if set, decorator cannot override this value (optional)
359372 fail_on_missing_id = True , # Raise an error if an ID cannot be extracted from the result
360373 serializer = json.dumps, # Custom serializer function (optional)
@@ -375,7 +388,8 @@ The traditional decorator for caching function results:
375388``` python
376389@cache (
377390 entity = " user" , # Type of entity returned (string) or ORM model class (optional)
378- id_key = " id" , # Field name or callable resolved to entity ID, not relevant on flat lists (optional)
391+ id_key = None , # default = 'id'. Field name or callable resolved to entity ID, not relevant on flat lists (optional)
392+ supported_id_types = (int , str , UUID ) # Types that are treated as valid entity IDs when extracted
379393 cache_key = None , # Custom cache key for function (optional)
380394 normalize_args = False , # Whether to normalize arguments (optional)
381395 ttl = None , # Override default TTL, raises error if locked_ttl is set (optional)
@@ -404,24 +418,7 @@ def get_article(article_id):
404418
405419#### @cache .tracks()
406420
407- A more expressive alias for ` @cache() ` that clearly communicates the function's results will be cached and entity references will be tracked:
408-
409- ``` python
410- @cache.tracks (
411- entity = " user" , # Type of entity returned by this function (string or ORM model class)
412- id_key = " id" , # How to extract entity IDs from results
413- # All other parameters from @cache() are supported
414- )
415- def get_user (user_id ):
416- # ...
417-
418- # With ORM model class
419- from myapp.models import User # SQLAlchemy or Django model
420-
421- @cache.tracks (User) # Automatically uses table name and extracts primary key
422- def get_user (user_id ):
423- # ...
424- ```
421+ A more expressive alias for ` @cache() `
425422
426423#### @cache .invalidates()
427424
@@ -568,7 +565,9 @@ poetry install
568565
569566## TODO
570567- [ ] Automatic ORM model detection, @cache .orm()
571- - [ ] Refactory & Cleanup
568+ - [ ] MyPy Refactory & Cleanup
569+ - [ ] Introduce proper reverse index interface to not lock into Redis OSS comp
570+ - [ ] * By default RedisCompReverser should be used with multi-exec capability
572571- [ ] Stale refs sweep scheduler / CLI comand
573572- [ ] Tests
574573
0 commit comments