MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _generate_cache_attrs

Method _generate_cache_attrs

lib/sqlalchemy/sql/cache_key.py:149–219  ·  view source on GitHub ↗

generate cache key dispatcher for a new class. This sets the _generated_cache_key_traversal attribute once called so should only be called once per class.

(
        cls,
    )

Source from the content-addressed store, hash-verified

147
148 @classmethod
149 def _generate_cache_attrs(
150 cls,
151 ) -> Union[_CacheKeyTraversalDispatchType, Literal[CacheConst.NO_CACHE]]:
152 """generate cache key dispatcher for a new class.
153
154 This sets the _generated_cache_key_traversal attribute once called
155 so should only be called once per class.
156
157 """
158 inherit_cache = cls.__dict__.get("inherit_cache", None)
159 inherit = bool(inherit_cache)
160
161 if inherit:
162 _cache_key_traversal = getattr(cls, "_cache_key_traversal", None)
163 if _cache_key_traversal is None:
164 try:
165 assert issubclass(cls, HasTraverseInternals)
166 _cache_key_traversal = cls._traverse_internals
167 except AttributeError:
168 cls._generated_cache_key_traversal = NO_CACHE
169 return NO_CACHE
170
171 assert _cache_key_traversal is not NO_CACHE, (
172 f"class {cls} has _cache_key_traversal=NO_CACHE, "
173 "which conflicts with inherit_cache=True"
174 )
175
176 # TODO: wouldn't we instead get this from our superclass?
177 # also, our superclass may not have this yet, but in any case,
178 # we'd generate for the superclass that has it. this is a little
179 # more complicated, so for the moment this is a little less
180 # efficient on startup but simpler.
181 return _cache_key_traversal_visitor.generate_dispatch(
182 cls,
183 _cache_key_traversal,
184 "_generated_cache_key_traversal",
185 )
186 else:
187 _cache_key_traversal = cls.__dict__.get(
188 "_cache_key_traversal", None
189 )
190 if _cache_key_traversal is None:
191 _cache_key_traversal = cls.__dict__.get(
192 "_traverse_internals", None
193 )
194 if _cache_key_traversal is None:
195 cls._generated_cache_key_traversal = NO_CACHE
196 if (
197 inherit_cache is None
198 and cls._hierarchy_supports_caching
199 ):
200 util.warn(
201 "Class %s will not make use of SQL compilation "
202 "caching as it does not set the 'inherit_cache' "
203 "attribute to ``True``. This can have "
204 "significant performance implications including "
205 "some performance degradations in comparison to "
206 "prior SQLAlchemy versions. Set this attribute "

Callers 2

_preconfigure_traversalsFunction · 0.80
_gen_cache_keyMethod · 0.80

Calls 3

generate_dispatchMethod · 0.80
getMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected