20 def userver_cache_control_hooks(self) -> typing.Dict[str, str]:
23 def pytest_plugin_registered(self, plugin, manager):
24 if not isinstance(plugin, types.ModuleType):
26 uhooks = getattr(plugin,
'USERVER_CACHE_CONTROL_HOOKS',
None)
29 if not isinstance(uhooks, dict):
31 f
'USERVER_CACHE_CONTROL_HOOKS must be dictionary: '
32 f
'{{cache_name: fixture_name}}, got {uhooks} instead',
34 for cache_name, fixture_name
in uhooks.items():
35 if cache_name
in self.
_hooks:
37 f
'USERVER_CACHE_CONTROL_HOOKS: hook already registered '
38 f
'for cache {cache_name}',
40 self.
_hooks[cache_name] = fixture_name
49 def invalidate_all(self) -> None:
52 def invalidate(self, caches: typing.Iterable[str]) ->
None:
57 def should_update_all_caches(self) -> bool:
61 def caches_to_update(self) -> typing.FrozenSet[str]:
66 def has_caches_to_update(self) -> bool:
68 return caches
is None or bool(caches)
70 def on_caches_updated(self, caches: typing.Iterable[str]) ->
None:
74 def on_all_caches_updated(self) -> None:
77 def assign_copy(self, other:
'InvalidationState') ->
None:
186 _userver_cache_control_context, _userver_cache_fixtures, request,
187) -> typing.Callable[[DaemonInstance], CacheControl]:
188 """Userver cache control handler.
190 To install per cache handler use USERVER_CACHE_CONTROL_HOOKS variable
191 in your pytest plugin:
194 USERVER_CACHE_CONTROL_HOOKS = {
195 'my-cache-name': 'my_cache_cc',
199 def my_cache_cc(my_cache_context):
200 def cache_control(request, state):
201 new_state = my_cache_context.get_state()
202 if state == new_state:
203 # Cache is already up to date, no need to update
206 # Request incremental update, if you cache supports it
207 request.incremental()
212 @ingroup userver_testsuite_fixtures
215 caches_disabled = set()
217 def userver_cache_control_disabled(
218 caches: typing.Sequence[str] =
None, *, reason: str,
220 if caches
is not None:
221 caches_disabled.update(caches)
225 for mark
in request.node.iter_markers(
'userver_cache_control_disabled'):
226 enabled = userver_cache_control_disabled(*mark.args, **mark.kwargs)
228 def get_cache_control(daemon: DaemonInstance):
229 context = _userver_cache_control_context.setdefault(daemon.id, {})
232 fixtures=_userver_cache_fixtures,
234 caches_disabled=caches_disabled,
237 return get_cache_control