summaryrefslogtreecommitdiff
path: root/examples/dogpile_caching
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-05-16 16:31:40 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2018-05-17 12:43:44 -0400
commit2c7f2954f793cbb4d991a870c1f725fc98e2e83f (patch)
tree7a3120e049660c0f924e3f718798e1b442e6a9a4 /examples/dogpile_caching
parentb9f428a589a1718efa20e5555be45ae3f767e89e (diff)
downloadsqlalchemy-2c7f2954f793cbb4d991a870c1f725fc98e2e83f.tar.gz
Update dogpile.cache example to be compatible with baked query.
Updated the dogpile.caching example to include new structures that accommodate for the "baked" query system, which is used by default within lazy loaders and some eager relationship loaders. The dogpile.caching "relationship_caching" and "advanced" examples were also broken due to :ticket:`4256`. The issue here is also worked-around by the fix in :ticket:`4128`. Note that this recipe requires I3f86fcb12a6a9a89aa308b335e75c25969bcc30e in order for the "advanced" example to work. Change-Id: I9d35417f1d6c1906555583b8225d3da7f81736f7 Fixes: #4258
Diffstat (limited to 'examples/dogpile_caching')
-rw-r--r--examples/dogpile_caching/caching_query.py43
1 files changed, 41 insertions, 2 deletions
diff --git a/examples/dogpile_caching/caching_query.py b/examples/dogpile_caching/caching_query.py
index 9ac0d431a..ed5a01f57 100644
--- a/examples/dogpile_caching/caching_query.py
+++ b/examples/dogpile_caching/caching_query.py
@@ -59,10 +59,37 @@ class CachingQuery(Query):
in the cache are not the same ones in the current Session.
"""
+ super_ = super(CachingQuery, self)
+
if hasattr(self, '_cache_region'):
- return self.get_value(createfunc=lambda: list(Query.__iter__(self)))
+ return self.get_value(createfunc=lambda: list(super_.__iter__()))
+ else:
+ return super_.__iter__()
+
+ def _execute_and_instances(self, context):
+ """override _execute_and_instances to pull results from dogpile
+ if the query is invoked directly from an external context.
+
+ This method is necessary in order to maintain compatibility
+ with the "baked query" system now used by default in some
+ relationship loader scenarios. Note also the
+ RelationshipCache._generate_cache_key method which enables
+ the baked query to be used within lazy loads.
+
+ .. versionadded:: 1.2.7
+ """
+ super_ = super(CachingQuery, self)
+
+ if context.query is not self and hasattr(self, '_cache_region'):
+ # special logic called when the Query._execute_and_instances()
+ # method is called directly from the baked query
+ return self.get_value(
+ createfunc=lambda: list(
+ super_._execute_and_instances(context)
+ )
+ )
else:
- return Query.__iter__(self)
+ return super_._execute_and_instances(context)
def _get_cache_plus_key(self):
"""Return a cache region plus key."""
@@ -227,4 +254,16 @@ class RelationshipCache(MapperOption):
self._relationship_options.update(option._relationship_options)
return self
+ def _generate_cache_key(self, path):
+ """Indicate to the lazy-loader strategy that a "baked" query
+ may be used by returning ``None``.
+
+ If this method is omitted, the default implementation of
+ :class:`.MapperOption._generate_cache_key` takes place, which
+ returns ``False`` to disable the "baked" query from being used.
+
+ .. versionadded:: 1.2.7
+
+ """
+ return None