Specifies that a Query as called within a "lazy load" should load results from a cache.
| 154 | |
| 155 | |
| 156 | class RelationshipCache(FromCache): |
| 157 | """Specifies that a Query as called within a "lazy load" |
| 158 | should load results from a cache.""" |
| 159 | |
| 160 | propagate_to_loaders = True |
| 161 | |
| 162 | def __init__( |
| 163 | self, |
| 164 | attribute, |
| 165 | region="default", |
| 166 | cache_key=None, |
| 167 | expiration_time=None, |
| 168 | ignore_expiration=False, |
| 169 | ): |
| 170 | """Construct a new RelationshipCache. |
| 171 | |
| 172 | :param attribute: A Class.attribute which |
| 173 | indicates a particular class relationship() whose |
| 174 | lazy loader should be pulled from the cache. |
| 175 | |
| 176 | :param region: name of the cache region. |
| 177 | |
| 178 | :param cache_key: optional. A string cache key |
| 179 | that will serve as the key to the query, bypassing |
| 180 | the usual means of forming a key from the Query itself. |
| 181 | |
| 182 | """ |
| 183 | self.region = region |
| 184 | self.cache_key = cache_key |
| 185 | self.expiration_time = expiration_time |
| 186 | self.ignore_expiration = ignore_expiration |
| 187 | self._relationship_options = { |
| 188 | (attribute.property.parent.class_, attribute.property.key): self |
| 189 | } |
| 190 | |
| 191 | def _process_orm_context(self, orm_context): |
| 192 | current_path = orm_context.loader_strategy_path |
| 193 | |
| 194 | if current_path: |
| 195 | mapper, prop = current_path[-2:] |
| 196 | key = prop.key |
| 197 | |
| 198 | for cls in mapper.class_.__mro__: |
| 199 | if (cls, key) in self._relationship_options: |
| 200 | relationship_option = self._relationship_options[ |
| 201 | (cls, key) |
| 202 | ] |
| 203 | return relationship_option |
| 204 | |
| 205 | def and_(self, option): |
| 206 | """Chain another RelationshipCache option to this one. |
| 207 | |
| 208 | While many RelationshipCache objects can be specified on a single |
| 209 | Query separately, chaining them together allows for a more efficient |
| 210 | lookup during load. |
| 211 | |
| 212 | """ |
| 213 | self._relationship_options.update(option._relationship_options) |
no outgoing calls
no test coverage detected