MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _gen_cache_key

Method _gen_cache_key

lib/sqlalchemy/sql/cache_key.py:224–347  ·  view source on GitHub ↗

return an optional cache key. The cache key is a tuple which can contain any series of objects that are hashable and also identifies this object uniquely within the presence of a larger SQL expression or statement, for the purposes of caching the resulting query.

(
        self, anon_map: anon_map, bindparams: List[BindParameter[Any]]
    )

Source from the content-addressed store, hash-verified

222
223 @util.preload_module("sqlalchemy.sql.elements")
224 def _gen_cache_key(
225 self, anon_map: anon_map, bindparams: List[BindParameter[Any]]
226 ) -> Optional[Tuple[Any, ...]]:
227 """return an optional cache key.
228
229 The cache key is a tuple which can contain any series of
230 objects that are hashable and also identifies
231 this object uniquely within the presence of a larger SQL expression
232 or statement, for the purposes of caching the resulting query.
233
234 The cache key should be based on the SQL compiled structure that would
235 ultimately be produced. That is, two structures that are composed in
236 exactly the same way should produce the same cache key; any difference
237 in the structures that would affect the SQL string or the type handlers
238 should result in a different cache key.
239
240 If a structure cannot produce a useful cache key, the NO_CACHE
241 symbol should be added to the anon_map and the method should
242 return None.
243
244 """
245
246 cls = self.__class__
247
248 id_, found = anon_map.get_anon(self)
249 if found:
250 return (id_, cls)
251
252 dispatcher: Union[
253 Literal[CacheConst.NO_CACHE],
254 _CacheKeyTraversalDispatchType,
255 ]
256
257 try:
258 dispatcher = cls.__dict__["_generated_cache_key_traversal"]
259 except KeyError:
260 # traversals.py -> _preconfigure_traversals()
261 # may be used to run these ahead of time, but
262 # is not enabled right now.
263 # this block will generate any remaining dispatchers.
264 dispatcher = cls._generate_cache_attrs()
265
266 if dispatcher is NO_CACHE:
267 anon_map[NO_CACHE] = True
268 return None
269
270 result: Tuple[Any, ...] = (id_, cls)
271
272 # inline of _cache_key_traversal_visitor.run_generated_dispatch()
273
274 for attrname, obj, meth in dispatcher(
275 self, _cache_key_traversal_visitor
276 ):
277 if obj is not None:
278 # TODO: see if C code can help here as Python lacks an
279 # efficient switch construct
280
281 if meth is STATIC_CACHE_KEY:

Callers 15

_generate_cache_keyMethod · 0.95
visit_inspectableMethod · 0.45
visit_multiMethod · 0.45
visit_multi_listMethod · 0.45
visit_prefix_sequenceMethod · 0.45
visit_table_hint_listMethod · 0.45

Calls 5

dispatcherClass · 0.85
get_anonMethod · 0.80
_generate_cache_attrsMethod · 0.80
apply_mapMethod · 0.45

Tested by

no test coverage detected