summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-05-17 13:05:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-05-17 17:08:23 -0400
commit912fb6c2d54d7f2fcda654a8f7702d122e8b8d70 (patch)
treebc9fb26d88acc7631fce8eacbfd90d53253f2584 /lib/sqlalchemy
parent4c6917e1d68a8baab7efe10e9ce5e5c8187f65ca (diff)
downloadsqlalchemy-912fb6c2d54d7f2fcda654a8f7702d122e8b8d70.tar.gz
Add new configuration, inspection for baked queries
Added new flag :paramref:`.Session.enable_baked_queries` to the :class:`.Session` to allow baked queries to be disabled session-wide, reducing memory use. Also added new :class:`.Bakery` wrapper so that the bakery returned by :paramref:`.BakedQuery.bakery` can be inspected. Change-Id: I5657af7a99d2b24c89d6aee1343f432728e3f807
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/ext/baked.py32
-rw-r--r--lib/sqlalchemy/orm/session.py17
2 files changed, 43 insertions, 6 deletions
diff --git a/lib/sqlalchemy/ext/baked.py b/lib/sqlalchemy/ext/baked.py
index 95b618f3f..ba3c2aed0 100644
--- a/lib/sqlalchemy/ext/baked.py
+++ b/lib/sqlalchemy/ext/baked.py
@@ -28,6 +28,27 @@ import logging
log = logging.getLogger(__name__)
+class Bakery(object):
+ """Callable which returns a :class:`.BakedQuery`.
+
+ This object is returned by the class method
+ :meth:`.BakedQuery.bakery`. It exists as an object
+ so that the "cache" can be easily inspected.
+
+ .. versionadded:: 1.2
+
+
+ """
+ __slots__ = 'cls', 'cache'
+
+ def __init__(self, cls_, cache):
+ self.cls = cls_
+ self.cache = cache
+
+ def __call__(self, initial_fn, *args):
+ return self.cls(self.cache, initial_fn, args)
+
+
class BakedQuery(object):
"""A builder object for :class:`.query.Query` objects."""
@@ -42,14 +63,13 @@ class BakedQuery(object):
@classmethod
def bakery(cls, size=200, _size_alert=None):
- """Construct a new bakery."""
+ """Construct a new bakery.
- _bakery = util.LRUCache(size, size_alert=_size_alert)
+ :return: an instance of :class:`.Bakery`
- def call(initial_fn, *args):
- return cls(_bakery, initial_fn, args)
+ """
- return call
+ return Bakery(cls, util.LRUCache(size, size_alert=_size_alert))
def _clone(self):
b1 = BakedQuery.__new__(BakedQuery)
@@ -265,7 +285,7 @@ class Result(object):
def __iter__(self):
bq = self.bq
- if bq._spoiled:
+ if not self.session.enable_baked_queries or bq._spoiled:
return iter(self._as_query())
baked_context = bq._bakery.get(bq._cache_key, None)
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py
index 3585166f4..6186ac4f7 100644
--- a/lib/sqlalchemy/orm/session.py
+++ b/lib/sqlalchemy/orm/session.py
@@ -588,6 +588,7 @@ class Session(_SessionClassMethods):
_enable_transaction_accounting=True,
autocommit=False, twophase=False,
weak_identity_map=True, binds=None, extension=None,
+ enable_baked_queries=True,
info=None,
query_cls=query.Query):
r"""Construct a new Session.
@@ -661,6 +662,21 @@ class Session(_SessionClassMethods):
:class:`.sessionmaker` function, and is not sent directly to the
constructor for ``Session``.
+ :param enable_baked_queries: defaults to ``True``. A flag consumed
+ by the :mod:`sqlalchemy.ext.baked` extension to determine if
+ "baked queries" should be cached, as is the normal operation
+ of this extension. When set to ``False``, all caching is disabled,
+ including baked queries defined by the calling application as
+ well as those used internally. Setting this flag to ``False``
+ can significantly reduce memory use, however will also degrade
+ performance for those areas that make use of baked queries
+ (such as relationship loaders). Additionally, baked query
+ logic in the calling application or potentially within the ORM
+ that may be malfunctioning due to cache key collisions or similar
+ can be flagged by observing if this flag resolves the issue.
+
+ .. versionadded:: 1.2
+
:param _enable_transaction_accounting: Defaults to ``True``. A
legacy-only flag which when ``False`` disables *all* 0.5-style
object accounting on transaction boundaries, including auto-expiry
@@ -735,6 +751,7 @@ class Session(_SessionClassMethods):
self.autoflush = autoflush
self.autocommit = autocommit
self.expire_on_commit = expire_on_commit
+ self.enable_baked_queries = enable_baked_queries
self._enable_transaction_accounting = _enable_transaction_accounting
self.twophase = twophase
self._query_cls = query_cls