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

Method _gen_cache_key

lib/sqlalchemy/sql/cache_key.py:222–345  ·  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

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

Calls 5

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

Tested by

no test coverage detected