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