diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-04-24 15:34:19 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-05-02 11:18:13 -0400 |
| commit | bbf644862ab05734d153d74abf59aa3492278563 (patch) | |
| tree | 0a563147603ff2906422ed1b07e9f5f03e7584c8 /examples | |
| parent | 7acf9af1ce74a0bda4c4d29af7da543b5c42b3f8 (diff) | |
| download | sqlalchemy-bbf644862ab05734d153d74abf59aa3492278563.tar.gz | |
Integrate new Result into ORM query
The next step in the 2.0 ORM changes is to have the
ORM integrate with the new Result object fully.
this patch uses Result to represent ORM objects rather
than lists. public API to get at this Result is not
added yet. dogpile.cache and horizontal sharding
recipe/extensions have small adjustments to accommodate
this change.
Callcounts have fluctuated, some slightly better and
some slightly worse. A few have gone up by a bit,
however as the codebase is still in flux it is anticipated
there will be some performance gains later on as
ORM fetching is refined to no longer need to accommodate
for extensive aliasing. The addition of caching
will then change the entire story.
References: #5087
References: #4395
Change-Id: If1a23824ffb77d8d58cf2338cf35dd6b5963b17f
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/dogpile_caching/caching_query.py | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/examples/dogpile_caching/caching_query.py b/examples/dogpile_caching/caching_query.py index 3d528b880..19e71ad4d 100644 --- a/examples/dogpile_caching/caching_query.py +++ b/examples/dogpile_caching/caching_query.py @@ -45,45 +45,21 @@ class CachingQuery(Query): self.cache_regions = regions Query.__init__(self, *args, **kw) - def __iter__(self): - """override __iter__ to pull results from dogpile - if particular attributes have been configured. - - Note that this approach does *not* detach the loaded objects from - the current session. If the cache backend is an in-process cache - (like "memory") and lives beyond the scope of the current session's - transaction, those objects may be expired. The method here can be - modified to first expunge() each loaded item from the current - session before returning the list of items, so that the items - 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(super_.__iter__())) - else: - return super_.__iter__() + # NOTE: as of 1.4 don't override __iter__() anymore, the result object + # cannot be cached at that level. 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 + """override _execute_and_instances to pull results from dogpile. """ super_ = super(CachingQuery, self) - if context.query is not self and hasattr(self, "_cache_region"): + if 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)) + createfunc=lambda: super_._execute_and_instances( + context + ).freeze() ) else: return super_._execute_and_instances(context) @@ -139,9 +115,14 @@ class CachingQuery(Query): ) if cached_value is NO_VALUE: raise KeyError(cache_key) + + # in 1.4 the cached value is a FrozenResult. merge_result + # accommodates this directly and updates the ORM entities inside + # the object to be merged. + # TODO: should this broken into merge_frozen_result / merge_iterator? if merge: cached_value = self.merge_result(cached_value, load=False) - return cached_value + return cached_value() def set_value(self, value): """Set the value in the cache for this query.""" |
