MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _generate_cache_key

Method _generate_cache_key

lib/sqlalchemy/sql/cache_key.py:349–393  ·  view source on GitHub ↗

return a 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. The

(self)

Source from the content-addressed store, hash-verified

347 return result
348
349 def _generate_cache_key(self) -> Optional[CacheKey]:
350 """return a cache key.
351
352 The cache key is a tuple which can contain any series of
353 objects that are hashable and also identifies
354 this object uniquely within the presence of a larger SQL expression
355 or statement, for the purposes of caching the resulting query.
356
357 The cache key should be based on the SQL compiled structure that would
358 ultimately be produced. That is, two structures that are composed in
359 exactly the same way should produce the same cache key; any difference
360 in the structures that would affect the SQL string or the type handlers
361 should result in a different cache key.
362
363 The cache key returned by this method is an instance of
364 :class:`.CacheKey`, which consists of a tuple representing the
365 cache key, as well as a list of :class:`.BindParameter` objects
366 which are extracted from the expression. While two expressions
367 that produce identical cache key tuples will themselves generate
368 identical SQL strings, the list of :class:`.BindParameter` objects
369 indicates the bound values which may have different values in
370 each one; these bound parameters must be consulted in order to
371 execute the statement with the correct parameters.
372
373 a :class:`_expression.ClauseElement` structure that does not implement
374 a :meth:`._gen_cache_key` method and does not implement a
375 :attr:`.traverse_internals` attribute will not be cacheable; when
376 such an element is embedded into a larger structure, this method
377 will return None, indicating no cache key is available.
378
379 """
380
381 bindparams: List[BindParameter[Any]] = []
382
383 _anon_map = anon_map()
384 key = self._gen_cache_key(_anon_map, bindparams)
385 if NO_CACHE in _anon_map:
386 return None
387 else:
388 assert key is not None
389 return CacheKey(
390 key,
391 bindparams,
392 _anon_map.get(CacheConst.PARAMS), # type: ignore[arg-type]
393 )
394
395
396class HasCacheKeyTraverse(HasTraverseInternals, HasCacheKey):

Callers 2

_generate_cache_keyMethod · 0.45

Calls 4

_gen_cache_keyMethod · 0.95
anon_mapClass · 0.85
CacheKeyClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected