cache_key.py 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. # sql/cache_key.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from __future__ import annotations
  8. import enum
  9. from itertools import zip_longest
  10. import typing
  11. from typing import Any
  12. from typing import Callable
  13. from typing import Dict
  14. from typing import Iterable
  15. from typing import Iterator
  16. from typing import List
  17. from typing import MutableMapping
  18. from typing import NamedTuple
  19. from typing import Optional
  20. from typing import Sequence
  21. from typing import Tuple
  22. from typing import Union
  23. from .visitors import anon_map
  24. from .visitors import HasTraversalDispatch
  25. from .visitors import HasTraverseInternals
  26. from .visitors import InternalTraversal
  27. from .visitors import prefix_anon_map
  28. from .. import util
  29. from ..inspection import inspect
  30. from ..util import HasMemoized
  31. from ..util.typing import Literal
  32. from ..util.typing import Protocol
  33. if typing.TYPE_CHECKING:
  34. from .elements import BindParameter
  35. from .elements import ClauseElement
  36. from .visitors import _TraverseInternalsType
  37. from ..engine.interfaces import _CoreSingleExecuteParams
  38. class _CacheKeyTraversalDispatchType(Protocol):
  39. def __call__(
  40. s, self: HasCacheKey, visitor: _CacheKeyTraversal
  41. ) -> _CacheKeyTraversalDispatchTypeReturn:
  42. ...
  43. class CacheConst(enum.Enum):
  44. NO_CACHE = 0
  45. NO_CACHE = CacheConst.NO_CACHE
  46. _CacheKeyTraversalType = Union[
  47. "_TraverseInternalsType", Literal[CacheConst.NO_CACHE], Literal[None]
  48. ]
  49. class CacheTraverseTarget(enum.Enum):
  50. CACHE_IN_PLACE = 0
  51. CALL_GEN_CACHE_KEY = 1
  52. STATIC_CACHE_KEY = 2
  53. PROPAGATE_ATTRS = 3
  54. ANON_NAME = 4
  55. (
  56. CACHE_IN_PLACE,
  57. CALL_GEN_CACHE_KEY,
  58. STATIC_CACHE_KEY,
  59. PROPAGATE_ATTRS,
  60. ANON_NAME,
  61. ) = tuple(CacheTraverseTarget)
  62. _CacheKeyTraversalDispatchTypeReturn = Sequence[
  63. Tuple[
  64. str,
  65. Any,
  66. Union[
  67. Callable[..., Tuple[Any, ...]],
  68. CacheTraverseTarget,
  69. InternalTraversal,
  70. ],
  71. ]
  72. ]
  73. class HasCacheKey:
  74. """Mixin for objects which can produce a cache key.
  75. This class is usually in a hierarchy that starts with the
  76. :class:`.HasTraverseInternals` base, but this is optional. Currently,
  77. the class should be able to work on its own without including
  78. :class:`.HasTraverseInternals`.
  79. .. seealso::
  80. :class:`.CacheKey`
  81. :ref:`sql_caching`
  82. """
  83. __slots__ = ()
  84. _cache_key_traversal: _CacheKeyTraversalType = NO_CACHE
  85. _is_has_cache_key = True
  86. _hierarchy_supports_caching = True
  87. """private attribute which may be set to False to prevent the
  88. inherit_cache warning from being emitted for a hierarchy of subclasses.
  89. Currently applies to the :class:`.ExecutableDDLElement` hierarchy which
  90. does not implement caching.
  91. """
  92. inherit_cache: Optional[bool] = None
  93. """Indicate if this :class:`.HasCacheKey` instance should make use of the
  94. cache key generation scheme used by its immediate superclass.
  95. The attribute defaults to ``None``, which indicates that a construct has
  96. not yet taken into account whether or not its appropriate for it to
  97. participate in caching; this is functionally equivalent to setting the
  98. value to ``False``, except that a warning is also emitted.
  99. This flag can be set to ``True`` on a particular class, if the SQL that
  100. corresponds to the object does not change based on attributes which
  101. are local to this class, and not its superclass.
  102. .. seealso::
  103. :ref:`compilerext_caching` - General guideslines for setting the
  104. :attr:`.HasCacheKey.inherit_cache` attribute for third-party or user
  105. defined SQL constructs.
  106. """
  107. __slots__ = ()
  108. _generated_cache_key_traversal: Any
  109. @classmethod
  110. def _generate_cache_attrs(
  111. cls,
  112. ) -> Union[_CacheKeyTraversalDispatchType, Literal[CacheConst.NO_CACHE]]:
  113. """generate cache key dispatcher for a new class.
  114. This sets the _generated_cache_key_traversal attribute once called
  115. so should only be called once per class.
  116. """
  117. inherit_cache = cls.__dict__.get("inherit_cache", None)
  118. inherit = bool(inherit_cache)
  119. if inherit:
  120. _cache_key_traversal = getattr(cls, "_cache_key_traversal", None)
  121. if _cache_key_traversal is None:
  122. try:
  123. assert issubclass(cls, HasTraverseInternals)
  124. _cache_key_traversal = cls._traverse_internals
  125. except AttributeError:
  126. cls._generated_cache_key_traversal = NO_CACHE
  127. return NO_CACHE
  128. assert _cache_key_traversal is not NO_CACHE, (
  129. f"class {cls} has _cache_key_traversal=NO_CACHE, "
  130. "which conflicts with inherit_cache=True"
  131. )
  132. # TODO: wouldn't we instead get this from our superclass?
  133. # also, our superclass may not have this yet, but in any case,
  134. # we'd generate for the superclass that has it. this is a little
  135. # more complicated, so for the moment this is a little less
  136. # efficient on startup but simpler.
  137. return _cache_key_traversal_visitor.generate_dispatch(
  138. cls,
  139. _cache_key_traversal,
  140. "_generated_cache_key_traversal",
  141. )
  142. else:
  143. _cache_key_traversal = cls.__dict__.get(
  144. "_cache_key_traversal", None
  145. )
  146. if _cache_key_traversal is None:
  147. _cache_key_traversal = cls.__dict__.get(
  148. "_traverse_internals", None
  149. )
  150. if _cache_key_traversal is None:
  151. cls._generated_cache_key_traversal = NO_CACHE
  152. if (
  153. inherit_cache is None
  154. and cls._hierarchy_supports_caching
  155. ):
  156. util.warn(
  157. "Class %s will not make use of SQL compilation "
  158. "caching as it does not set the 'inherit_cache' "
  159. "attribute to ``True``. This can have "
  160. "significant performance implications including "
  161. "some performance degradations in comparison to "
  162. "prior SQLAlchemy versions. Set this attribute "
  163. "to True if this object can make use of the cache "
  164. "key generated by the superclass. Alternatively, "
  165. "this attribute may be set to False which will "
  166. "disable this warning." % (cls.__name__),
  167. code="cprf",
  168. )
  169. return NO_CACHE
  170. return _cache_key_traversal_visitor.generate_dispatch(
  171. cls,
  172. _cache_key_traversal,
  173. "_generated_cache_key_traversal",
  174. )
  175. @util.preload_module("sqlalchemy.sql.elements")
  176. def _gen_cache_key(
  177. self, anon_map: anon_map, bindparams: List[BindParameter[Any]]
  178. ) -> Optional[Tuple[Any, ...]]:
  179. """return an optional cache key.
  180. The cache key is a tuple which can contain any series of
  181. objects that are hashable and also identifies
  182. this object uniquely within the presence of a larger SQL expression
  183. or statement, for the purposes of caching the resulting query.
  184. The cache key should be based on the SQL compiled structure that would
  185. ultimately be produced. That is, two structures that are composed in
  186. exactly the same way should produce the same cache key; any difference
  187. in the structures that would affect the SQL string or the type handlers
  188. should result in a different cache key.
  189. If a structure cannot produce a useful cache key, the NO_CACHE
  190. symbol should be added to the anon_map and the method should
  191. return None.
  192. """
  193. cls = self.__class__
  194. id_, found = anon_map.get_anon(self)
  195. if found:
  196. return (id_, cls)
  197. dispatcher: Union[
  198. Literal[CacheConst.NO_CACHE],
  199. _CacheKeyTraversalDispatchType,
  200. ]
  201. try:
  202. dispatcher = cls.__dict__["_generated_cache_key_traversal"]
  203. except KeyError:
  204. # traversals.py -> _preconfigure_traversals()
  205. # may be used to run these ahead of time, but
  206. # is not enabled right now.
  207. # this block will generate any remaining dispatchers.
  208. dispatcher = cls._generate_cache_attrs()
  209. if dispatcher is NO_CACHE:
  210. anon_map[NO_CACHE] = True
  211. return None
  212. result: Tuple[Any, ...] = (id_, cls)
  213. # inline of _cache_key_traversal_visitor.run_generated_dispatch()
  214. for attrname, obj, meth in dispatcher(
  215. self, _cache_key_traversal_visitor
  216. ):
  217. if obj is not None:
  218. # TODO: see if C code can help here as Python lacks an
  219. # efficient switch construct
  220. if meth is STATIC_CACHE_KEY:
  221. sck = obj._static_cache_key
  222. if sck is NO_CACHE:
  223. anon_map[NO_CACHE] = True
  224. return None
  225. result += (attrname, sck)
  226. elif meth is ANON_NAME:
  227. elements = util.preloaded.sql_elements
  228. if isinstance(obj, elements._anonymous_label):
  229. obj = obj.apply_map(anon_map) # type: ignore
  230. result += (attrname, obj)
  231. elif meth is CALL_GEN_CACHE_KEY:
  232. result += (
  233. attrname,
  234. obj._gen_cache_key(anon_map, bindparams),
  235. )
  236. # remaining cache functions are against
  237. # Python tuples, dicts, lists, etc. so we can skip
  238. # if they are empty
  239. elif obj:
  240. if meth is CACHE_IN_PLACE:
  241. result += (attrname, obj)
  242. elif meth is PROPAGATE_ATTRS:
  243. result += (
  244. attrname,
  245. obj["compile_state_plugin"],
  246. obj["plugin_subject"]._gen_cache_key(
  247. anon_map, bindparams
  248. )
  249. if obj["plugin_subject"]
  250. else None,
  251. )
  252. elif meth is InternalTraversal.dp_annotations_key:
  253. # obj is here is the _annotations dict. Table uses
  254. # a memoized version of it. however in other cases,
  255. # we generate it given anon_map as we may be from a
  256. # Join, Aliased, etc.
  257. # see #8790
  258. if self._gen_static_annotations_cache_key: # type: ignore # noqa: E501
  259. result += self._annotations_cache_key # type: ignore # noqa: E501
  260. else:
  261. result += self._gen_annotations_cache_key(anon_map) # type: ignore # noqa: E501
  262. elif (
  263. meth is InternalTraversal.dp_clauseelement_list
  264. or meth is InternalTraversal.dp_clauseelement_tuple
  265. or meth
  266. is InternalTraversal.dp_memoized_select_entities
  267. ):
  268. result += (
  269. attrname,
  270. tuple(
  271. [
  272. elem._gen_cache_key(anon_map, bindparams)
  273. for elem in obj
  274. ]
  275. ),
  276. )
  277. else:
  278. result += meth( # type: ignore
  279. attrname, obj, self, anon_map, bindparams
  280. )
  281. return result
  282. def _generate_cache_key(self) -> Optional[CacheKey]:
  283. """return a cache key.
  284. The cache key is a tuple which can contain any series of
  285. objects that are hashable and also identifies
  286. this object uniquely within the presence of a larger SQL expression
  287. or statement, for the purposes of caching the resulting query.
  288. The cache key should be based on the SQL compiled structure that would
  289. ultimately be produced. That is, two structures that are composed in
  290. exactly the same way should produce the same cache key; any difference
  291. in the structures that would affect the SQL string or the type handlers
  292. should result in a different cache key.
  293. The cache key returned by this method is an instance of
  294. :class:`.CacheKey`, which consists of a tuple representing the
  295. cache key, as well as a list of :class:`.BindParameter` objects
  296. which are extracted from the expression. While two expressions
  297. that produce identical cache key tuples will themselves generate
  298. identical SQL strings, the list of :class:`.BindParameter` objects
  299. indicates the bound values which may have different values in
  300. each one; these bound parameters must be consulted in order to
  301. execute the statement with the correct parameters.
  302. a :class:`_expression.ClauseElement` structure that does not implement
  303. a :meth:`._gen_cache_key` method and does not implement a
  304. :attr:`.traverse_internals` attribute will not be cacheable; when
  305. such an element is embedded into a larger structure, this method
  306. will return None, indicating no cache key is available.
  307. """
  308. bindparams: List[BindParameter[Any]] = []
  309. _anon_map = anon_map()
  310. key = self._gen_cache_key(_anon_map, bindparams)
  311. if NO_CACHE in _anon_map:
  312. return None
  313. else:
  314. assert key is not None
  315. return CacheKey(key, bindparams)
  316. @classmethod
  317. def _generate_cache_key_for_object(
  318. cls, obj: HasCacheKey
  319. ) -> Optional[CacheKey]:
  320. bindparams: List[BindParameter[Any]] = []
  321. _anon_map = anon_map()
  322. key = obj._gen_cache_key(_anon_map, bindparams)
  323. if NO_CACHE in _anon_map:
  324. return None
  325. else:
  326. assert key is not None
  327. return CacheKey(key, bindparams)
  328. class HasCacheKeyTraverse(HasTraverseInternals, HasCacheKey):
  329. pass
  330. class MemoizedHasCacheKey(HasCacheKey, HasMemoized):
  331. __slots__ = ()
  332. @HasMemoized.memoized_instancemethod
  333. def _generate_cache_key(self) -> Optional[CacheKey]:
  334. return HasCacheKey._generate_cache_key(self)
  335. class SlotsMemoizedHasCacheKey(HasCacheKey, util.MemoizedSlots):
  336. __slots__ = ()
  337. def _memoized_method__generate_cache_key(self) -> Optional[CacheKey]:
  338. return HasCacheKey._generate_cache_key(self)
  339. class CacheKey(NamedTuple):
  340. """The key used to identify a SQL statement construct in the
  341. SQL compilation cache.
  342. .. seealso::
  343. :ref:`sql_caching`
  344. """
  345. key: Tuple[Any, ...]
  346. bindparams: Sequence[BindParameter[Any]]
  347. # can't set __hash__ attribute because it interferes
  348. # with namedtuple
  349. # can't use "if not TYPE_CHECKING" because mypy rejects it
  350. # inside of a NamedTuple
  351. def __hash__(self) -> Optional[int]: # type: ignore
  352. """CacheKey itself is not hashable - hash the .key portion"""
  353. return None
  354. def to_offline_string(
  355. self,
  356. statement_cache: MutableMapping[Any, str],
  357. statement: ClauseElement,
  358. parameters: _CoreSingleExecuteParams,
  359. ) -> str:
  360. """Generate an "offline string" form of this :class:`.CacheKey`
  361. The "offline string" is basically the string SQL for the
  362. statement plus a repr of the bound parameter values in series.
  363. Whereas the :class:`.CacheKey` object is dependent on in-memory
  364. identities in order to work as a cache key, the "offline" version
  365. is suitable for a cache that will work for other processes as well.
  366. The given ``statement_cache`` is a dictionary-like object where the
  367. string form of the statement itself will be cached. This dictionary
  368. should be in a longer lived scope in order to reduce the time spent
  369. stringifying statements.
  370. """
  371. if self.key not in statement_cache:
  372. statement_cache[self.key] = sql_str = str(statement)
  373. else:
  374. sql_str = statement_cache[self.key]
  375. if not self.bindparams:
  376. param_tuple = tuple(parameters[key] for key in sorted(parameters))
  377. else:
  378. param_tuple = tuple(
  379. parameters.get(bindparam.key, bindparam.value)
  380. for bindparam in self.bindparams
  381. )
  382. return repr((sql_str, param_tuple))
  383. def __eq__(self, other: Any) -> bool:
  384. return bool(self.key == other.key)
  385. def __ne__(self, other: Any) -> bool:
  386. return not (self.key == other.key)
  387. @classmethod
  388. def _diff_tuples(cls, left: CacheKey, right: CacheKey) -> str:
  389. ck1 = CacheKey(left, [])
  390. ck2 = CacheKey(right, [])
  391. return ck1._diff(ck2)
  392. def _whats_different(self, other: CacheKey) -> Iterator[str]:
  393. k1 = self.key
  394. k2 = other.key
  395. stack: List[int] = []
  396. pickup_index = 0
  397. while True:
  398. s1, s2 = k1, k2
  399. for idx in stack:
  400. s1 = s1[idx]
  401. s2 = s2[idx]
  402. for idx, (e1, e2) in enumerate(zip_longest(s1, s2)):
  403. if idx < pickup_index:
  404. continue
  405. if e1 != e2:
  406. if isinstance(e1, tuple) and isinstance(e2, tuple):
  407. stack.append(idx)
  408. break
  409. else:
  410. yield "key%s[%d]: %s != %s" % (
  411. "".join("[%d]" % id_ for id_ in stack),
  412. idx,
  413. e1,
  414. e2,
  415. )
  416. else:
  417. pickup_index = stack.pop(-1)
  418. break
  419. def _diff(self, other: CacheKey) -> str:
  420. return ", ".join(self._whats_different(other))
  421. def __str__(self) -> str:
  422. stack: List[Union[Tuple[Any, ...], HasCacheKey]] = [self.key]
  423. output = []
  424. sentinel = object()
  425. indent = -1
  426. while stack:
  427. elem = stack.pop(0)
  428. if elem is sentinel:
  429. output.append((" " * (indent * 2)) + "),")
  430. indent -= 1
  431. elif isinstance(elem, tuple):
  432. if not elem:
  433. output.append((" " * ((indent + 1) * 2)) + "()")
  434. else:
  435. indent += 1
  436. stack = list(elem) + [sentinel] + stack
  437. output.append((" " * (indent * 2)) + "(")
  438. else:
  439. if isinstance(elem, HasCacheKey):
  440. repr_ = "<%s object at %s>" % (
  441. type(elem).__name__,
  442. hex(id(elem)),
  443. )
  444. else:
  445. repr_ = repr(elem)
  446. output.append((" " * (indent * 2)) + " " + repr_ + ", ")
  447. return "CacheKey(key=%s)" % ("\n".join(output),)
  448. def _generate_param_dict(self) -> Dict[str, Any]:
  449. """used for testing"""
  450. _anon_map = prefix_anon_map()
  451. return {b.key % _anon_map: b.effective_value for b in self.bindparams}
  452. def _apply_params_to_element(
  453. self, original_cache_key: CacheKey, target_element: ClauseElement
  454. ) -> ClauseElement:
  455. if target_element._is_immutable:
  456. return target_element
  457. translate = {
  458. k.key: v.value
  459. for k, v in zip(original_cache_key.bindparams, self.bindparams)
  460. }
  461. return target_element.params(translate)
  462. def _ad_hoc_cache_key_from_args(
  463. tokens: Tuple[Any, ...],
  464. traverse_args: Iterable[Tuple[str, InternalTraversal]],
  465. args: Iterable[Any],
  466. ) -> Tuple[Any, ...]:
  467. """a quick cache key generator used by reflection.flexi_cache."""
  468. bindparams: List[BindParameter[Any]] = []
  469. _anon_map = anon_map()
  470. tup = tokens
  471. for (attrname, sym), arg in zip(traverse_args, args):
  472. key = sym.name
  473. visit_key = key.replace("dp_", "visit_")
  474. if arg is None:
  475. tup += (attrname, None)
  476. continue
  477. meth = getattr(_cache_key_traversal_visitor, visit_key)
  478. if meth is CACHE_IN_PLACE:
  479. tup += (attrname, arg)
  480. elif meth in (
  481. CALL_GEN_CACHE_KEY,
  482. STATIC_CACHE_KEY,
  483. ANON_NAME,
  484. PROPAGATE_ATTRS,
  485. ):
  486. raise NotImplementedError(
  487. f"Haven't implemented symbol {meth} for ad-hoc key from args"
  488. )
  489. else:
  490. tup += meth(attrname, arg, None, _anon_map, bindparams)
  491. return tup
  492. class _CacheKeyTraversal(HasTraversalDispatch):
  493. # very common elements are inlined into the main _get_cache_key() method
  494. # to produce a dramatic savings in Python function call overhead
  495. visit_has_cache_key = visit_clauseelement = CALL_GEN_CACHE_KEY
  496. visit_clauseelement_list = InternalTraversal.dp_clauseelement_list
  497. visit_annotations_key = InternalTraversal.dp_annotations_key
  498. visit_clauseelement_tuple = InternalTraversal.dp_clauseelement_tuple
  499. visit_memoized_select_entities = (
  500. InternalTraversal.dp_memoized_select_entities
  501. )
  502. visit_string = (
  503. visit_boolean
  504. ) = visit_operator = visit_plain_obj = CACHE_IN_PLACE
  505. visit_statement_hint_list = CACHE_IN_PLACE
  506. visit_type = STATIC_CACHE_KEY
  507. visit_anon_name = ANON_NAME
  508. visit_propagate_attrs = PROPAGATE_ATTRS
  509. def visit_with_context_options(
  510. self,
  511. attrname: str,
  512. obj: Any,
  513. parent: Any,
  514. anon_map: anon_map,
  515. bindparams: List[BindParameter[Any]],
  516. ) -> Tuple[Any, ...]:
  517. return tuple((fn.__code__, c_key) for fn, c_key in obj)
  518. def visit_inspectable(
  519. self,
  520. attrname: str,
  521. obj: Any,
  522. parent: Any,
  523. anon_map: anon_map,
  524. bindparams: List[BindParameter[Any]],
  525. ) -> Tuple[Any, ...]:
  526. return (attrname, inspect(obj)._gen_cache_key(anon_map, bindparams))
  527. def visit_string_list(
  528. self,
  529. attrname: str,
  530. obj: Any,
  531. parent: Any,
  532. anon_map: anon_map,
  533. bindparams: List[BindParameter[Any]],
  534. ) -> Tuple[Any, ...]:
  535. return tuple(obj)
  536. def visit_multi(
  537. self,
  538. attrname: str,
  539. obj: Any,
  540. parent: Any,
  541. anon_map: anon_map,
  542. bindparams: List[BindParameter[Any]],
  543. ) -> Tuple[Any, ...]:
  544. return (
  545. attrname,
  546. obj._gen_cache_key(anon_map, bindparams)
  547. if isinstance(obj, HasCacheKey)
  548. else obj,
  549. )
  550. def visit_multi_list(
  551. self,
  552. attrname: str,
  553. obj: Any,
  554. parent: Any,
  555. anon_map: anon_map,
  556. bindparams: List[BindParameter[Any]],
  557. ) -> Tuple[Any, ...]:
  558. return (
  559. attrname,
  560. tuple(
  561. elem._gen_cache_key(anon_map, bindparams)
  562. if isinstance(elem, HasCacheKey)
  563. else elem
  564. for elem in obj
  565. ),
  566. )
  567. def visit_has_cache_key_tuples(
  568. self,
  569. attrname: str,
  570. obj: Any,
  571. parent: Any,
  572. anon_map: anon_map,
  573. bindparams: List[BindParameter[Any]],
  574. ) -> Tuple[Any, ...]:
  575. if not obj:
  576. return ()
  577. return (
  578. attrname,
  579. tuple(
  580. tuple(
  581. elem._gen_cache_key(anon_map, bindparams)
  582. for elem in tup_elem
  583. )
  584. for tup_elem in obj
  585. ),
  586. )
  587. def visit_has_cache_key_list(
  588. self,
  589. attrname: str,
  590. obj: Any,
  591. parent: Any,
  592. anon_map: anon_map,
  593. bindparams: List[BindParameter[Any]],
  594. ) -> Tuple[Any, ...]:
  595. if not obj:
  596. return ()
  597. return (
  598. attrname,
  599. tuple(elem._gen_cache_key(anon_map, bindparams) for elem in obj),
  600. )
  601. def visit_executable_options(
  602. self,
  603. attrname: str,
  604. obj: Any,
  605. parent: Any,
  606. anon_map: anon_map,
  607. bindparams: List[BindParameter[Any]],
  608. ) -> Tuple[Any, ...]:
  609. if not obj:
  610. return ()
  611. return (
  612. attrname,
  613. tuple(
  614. elem._gen_cache_key(anon_map, bindparams)
  615. for elem in obj
  616. if elem._is_has_cache_key
  617. ),
  618. )
  619. def visit_inspectable_list(
  620. self,
  621. attrname: str,
  622. obj: Any,
  623. parent: Any,
  624. anon_map: anon_map,
  625. bindparams: List[BindParameter[Any]],
  626. ) -> Tuple[Any, ...]:
  627. return self.visit_has_cache_key_list(
  628. attrname, [inspect(o) for o in obj], parent, anon_map, bindparams
  629. )
  630. def visit_clauseelement_tuples(
  631. self,
  632. attrname: str,
  633. obj: Any,
  634. parent: Any,
  635. anon_map: anon_map,
  636. bindparams: List[BindParameter[Any]],
  637. ) -> Tuple[Any, ...]:
  638. return self.visit_has_cache_key_tuples(
  639. attrname, obj, parent, anon_map, bindparams
  640. )
  641. def visit_fromclause_ordered_set(
  642. self,
  643. attrname: str,
  644. obj: Any,
  645. parent: Any,
  646. anon_map: anon_map,
  647. bindparams: List[BindParameter[Any]],
  648. ) -> Tuple[Any, ...]:
  649. if not obj:
  650. return ()
  651. return (
  652. attrname,
  653. tuple([elem._gen_cache_key(anon_map, bindparams) for elem in obj]),
  654. )
  655. def visit_clauseelement_unordered_set(
  656. self,
  657. attrname: str,
  658. obj: Any,
  659. parent: Any,
  660. anon_map: anon_map,
  661. bindparams: List[BindParameter[Any]],
  662. ) -> Tuple[Any, ...]:
  663. if not obj:
  664. return ()
  665. cache_keys = [
  666. elem._gen_cache_key(anon_map, bindparams) for elem in obj
  667. ]
  668. return (
  669. attrname,
  670. tuple(
  671. sorted(cache_keys)
  672. ), # cache keys all start with (id_, class)
  673. )
  674. def visit_named_ddl_element(
  675. self,
  676. attrname: str,
  677. obj: Any,
  678. parent: Any,
  679. anon_map: anon_map,
  680. bindparams: List[BindParameter[Any]],
  681. ) -> Tuple[Any, ...]:
  682. return (attrname, obj.name)
  683. def visit_prefix_sequence(
  684. self,
  685. attrname: str,
  686. obj: Any,
  687. parent: Any,
  688. anon_map: anon_map,
  689. bindparams: List[BindParameter[Any]],
  690. ) -> Tuple[Any, ...]:
  691. if not obj:
  692. return ()
  693. return (
  694. attrname,
  695. tuple(
  696. [
  697. (clause._gen_cache_key(anon_map, bindparams), strval)
  698. for clause, strval in obj
  699. ]
  700. ),
  701. )
  702. def visit_setup_join_tuple(
  703. self,
  704. attrname: str,
  705. obj: Any,
  706. parent: Any,
  707. anon_map: anon_map,
  708. bindparams: List[BindParameter[Any]],
  709. ) -> Tuple[Any, ...]:
  710. return tuple(
  711. (
  712. target._gen_cache_key(anon_map, bindparams),
  713. onclause._gen_cache_key(anon_map, bindparams)
  714. if onclause is not None
  715. else None,
  716. from_._gen_cache_key(anon_map, bindparams)
  717. if from_ is not None
  718. else None,
  719. tuple([(key, flags[key]) for key in sorted(flags)]),
  720. )
  721. for (target, onclause, from_, flags) in obj
  722. )
  723. def visit_table_hint_list(
  724. self,
  725. attrname: str,
  726. obj: Any,
  727. parent: Any,
  728. anon_map: anon_map,
  729. bindparams: List[BindParameter[Any]],
  730. ) -> Tuple[Any, ...]:
  731. if not obj:
  732. return ()
  733. return (
  734. attrname,
  735. tuple(
  736. [
  737. (
  738. clause._gen_cache_key(anon_map, bindparams),
  739. dialect_name,
  740. text,
  741. )
  742. for (clause, dialect_name), text in obj.items()
  743. ]
  744. ),
  745. )
  746. def visit_plain_dict(
  747. self,
  748. attrname: str,
  749. obj: Any,
  750. parent: Any,
  751. anon_map: anon_map,
  752. bindparams: List[BindParameter[Any]],
  753. ) -> Tuple[Any, ...]:
  754. return (attrname, tuple([(key, obj[key]) for key in sorted(obj)]))
  755. def visit_dialect_options(
  756. self,
  757. attrname: str,
  758. obj: Any,
  759. parent: Any,
  760. anon_map: anon_map,
  761. bindparams: List[BindParameter[Any]],
  762. ) -> Tuple[Any, ...]:
  763. return (
  764. attrname,
  765. tuple(
  766. (
  767. dialect_name,
  768. tuple(
  769. [
  770. (key, obj[dialect_name][key])
  771. for key in sorted(obj[dialect_name])
  772. ]
  773. ),
  774. )
  775. for dialect_name in sorted(obj)
  776. ),
  777. )
  778. def visit_string_clauseelement_dict(
  779. self,
  780. attrname: str,
  781. obj: Any,
  782. parent: Any,
  783. anon_map: anon_map,
  784. bindparams: List[BindParameter[Any]],
  785. ) -> Tuple[Any, ...]:
  786. return (
  787. attrname,
  788. tuple(
  789. (key, obj[key]._gen_cache_key(anon_map, bindparams))
  790. for key in sorted(obj)
  791. ),
  792. )
  793. def visit_string_multi_dict(
  794. self,
  795. attrname: str,
  796. obj: Any,
  797. parent: Any,
  798. anon_map: anon_map,
  799. bindparams: List[BindParameter[Any]],
  800. ) -> Tuple[Any, ...]:
  801. return (
  802. attrname,
  803. tuple(
  804. (
  805. key,
  806. value._gen_cache_key(anon_map, bindparams)
  807. if isinstance(value, HasCacheKey)
  808. else value,
  809. )
  810. for key, value in [(key, obj[key]) for key in sorted(obj)]
  811. ),
  812. )
  813. def visit_fromclause_canonical_column_collection(
  814. self,
  815. attrname: str,
  816. obj: Any,
  817. parent: Any,
  818. anon_map: anon_map,
  819. bindparams: List[BindParameter[Any]],
  820. ) -> Tuple[Any, ...]:
  821. # inlining into the internals of ColumnCollection
  822. return (
  823. attrname,
  824. tuple(
  825. col._gen_cache_key(anon_map, bindparams)
  826. for k, col, _ in obj._collection
  827. ),
  828. )
  829. def visit_unknown_structure(
  830. self,
  831. attrname: str,
  832. obj: Any,
  833. parent: Any,
  834. anon_map: anon_map,
  835. bindparams: List[BindParameter[Any]],
  836. ) -> Tuple[Any, ...]:
  837. anon_map[NO_CACHE] = True
  838. return ()
  839. def visit_dml_ordered_values(
  840. self,
  841. attrname: str,
  842. obj: Any,
  843. parent: Any,
  844. anon_map: anon_map,
  845. bindparams: List[BindParameter[Any]],
  846. ) -> Tuple[Any, ...]:
  847. return (
  848. attrname,
  849. tuple(
  850. (
  851. key._gen_cache_key(anon_map, bindparams)
  852. if hasattr(key, "__clause_element__")
  853. else key,
  854. value._gen_cache_key(anon_map, bindparams),
  855. )
  856. for key, value in obj
  857. ),
  858. )
  859. def visit_dml_values(
  860. self,
  861. attrname: str,
  862. obj: Any,
  863. parent: Any,
  864. anon_map: anon_map,
  865. bindparams: List[BindParameter[Any]],
  866. ) -> Tuple[Any, ...]:
  867. # in py37 we can assume two dictionaries created in the same
  868. # insert ordering will retain that sorting
  869. return (
  870. attrname,
  871. tuple(
  872. (
  873. k._gen_cache_key(anon_map, bindparams)
  874. if hasattr(k, "__clause_element__")
  875. else k,
  876. obj[k]._gen_cache_key(anon_map, bindparams),
  877. )
  878. for k in obj
  879. ),
  880. )
  881. def visit_dml_multi_values(
  882. self,
  883. attrname: str,
  884. obj: Any,
  885. parent: Any,
  886. anon_map: anon_map,
  887. bindparams: List[BindParameter[Any]],
  888. ) -> Tuple[Any, ...]:
  889. # multivalues are simply not cacheable right now
  890. anon_map[NO_CACHE] = True
  891. return ()
  892. _cache_key_traversal_visitor = _CacheKeyTraversal()