@@ -1207,6 +1207,7 @@ def next(
12071207 - Unsafe methods (POST, PUT, DELETE, etc.) are written through to origin
12081208 - Multiple matching responses are sorted by Date header (most recent first)
12091209 - Age header is updated when serving from cache
1210+ - Request no-cache directive forces revalidation of cached responses
12101211
12111212 Examples:
12121213 --------
@@ -1229,6 +1230,18 @@ def next(
12291230 >>> next_state = idle.next(get_request, [cached_pair])
12301231 >>> isinstance(next_state, NeedRevalidation)
12311232 True
1233+
1234+ >>> # Need revalidation - request no-cache forces validation of fresh response
1235+ >>> idle = IdleClient(options=default_options)
1236+ >>> no_cache_request = Request(
1237+ ... method="GET",
1238+ ... url="https://example.com",
1239+ ... headers=Headers({"cache-control": "no-cache"})
1240+ ... )
1241+ >>> cached_pair = CompletePair(no_cache_request, fresh_response)
1242+ >>> next_state = idle.next(no_cache_request, [cached_pair])
1243+ >>> isinstance(next_state, NeedRevalidation)
1244+ True
12321245 """
12331246
12341247 # ============================================================================
@@ -1388,7 +1401,31 @@ def fresh_or_allowed_stale(pair: Entry) -> bool:
13881401 ready_to_use , need_revalidation = partition (filtered_pairs , fresh_or_allowed_stale )
13891402
13901403 # ============================================================================
1391- # STEP 7: Determine Next State Based on Available Responses
1404+ # STEP 7: Handle Request no-cache Directive
1405+ # ============================================================================
1406+ # RFC 9111 Section 5.2.1.4: no-cache Request Directive
1407+ # https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.4
1408+ #
1409+ # "The no-cache request directive indicates that a cache MUST NOT use a
1410+ # stored response to satisfy the request without successful validation on
1411+ # the origin server."
1412+ #
1413+ # When a client sends Cache-Control: no-cache in the request, it's explicitly
1414+ # requesting that the cache not use any stored response without first validating
1415+ # it with the origin server. This is different from the response no-cache directive,
1416+ # which applies to how responses should be cached.
1417+ request_cache_control = parse_cache_control (request .headers .get ("cache-control" ))
1418+
1419+ if request_cache_control .no_cache is True :
1420+ # Move all fresh responses to the revalidation queue
1421+ # This ensures that even fresh cached responses will be validated
1422+ # with the origin server via conditional requests (If-None-Match,
1423+ # If-Modified-Since) before being served to the client.
1424+ need_revalidation .extend (ready_to_use )
1425+ ready_to_use = []
1426+
1427+ # ============================================================================
1428+ # STEP 8: Determine Next State Based on Available Responses
13921429 # ============================================================================
13931430
13941431 if ready_to_use :
0 commit comments