Source code for examples.dogpile_caching.relationship_caching
"""Illustrates how to add cache options onrelationship endpoints, so that lazyloads load from cache.Load a set of Person and Address objects, specifying thatrelated PostalCode, City, Country objects should be pulled from longterm cache."""importosfromsqlalchemyimportselectfromsqlalchemy.ormimportjoinedloadfrom.environmentimportrootfrom.environmentimportSessionfrom.modelimportcache_address_bitsfrom.modelimportPersonforpinSession.scalars(select(Person).options(joinedload(Person.addresses),cache_address_bits)).unique():print(p.format_full())print("\n\nIf this was the first run of relationship_caching.py, ""SQL was likely emitted to ""load postal codes, cities, countries.\n""If run a second time, assuming the cache is still valid, ""only a single SQL statement will run - all ""related data is pulled from cache.\n""To clear the cache, delete the file %r. \n""This will cause a re-load of cities, postal codes and countries on ""the next run.\n"%os.path.join(root,"cache.dbm"))