Skip to content

Commit

Permalink
Fix disabling expiration for a single request with `CachedSession.req…
Browse files Browse the repository at this point in the history
…uest(..., expire_after=-1)`
  • Loading branch information
JWCook committed Feb 15, 2022
1 parent 04ff30b commit 261bda4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 10 additions & 6 deletions requests_cache/cache_control.py
Expand Up @@ -27,8 +27,9 @@

__all__ = ['DO_NOT_CACHE', 'CacheActions']

# May be set by either headers or expire_after param to disable caching
# May be set by either headers or expire_after param to disable caching or disable expiration
DO_NOT_CACHE = 0
NEVER_EXPIRE = -1
# Supported Cache-Control directives
CACHE_DIRECTIVES = ['immutable', 'max-age', 'no-cache', 'no-store']

Expand Down Expand Up @@ -139,7 +140,7 @@ def update_from_response(self, response: Response):

# Check headers for expiration, validators, and other cache directives
if directives.get('immutable'):
self.expire_after = -1
self.expire_after = NEVER_EXPIRE
else:
self.expire_after = coalesce(
directives.get('max-age'), directives.get('expires'), self.expire_after
Expand All @@ -156,7 +157,7 @@ def update_from_response(self, response: Response):
def get_expiration_datetime(expire_after: ExpirationTime) -> Optional[datetime]:
"""Convert an expiration value in any supported format to an absolute datetime"""
# Never expire
if expire_after is None or expire_after == -1:
if expire_after is None or expire_after == NEVER_EXPIRE:
return None
# Expire immediately
elif try_int(expire_after) == DO_NOT_CACHE:
Expand All @@ -173,10 +174,10 @@ def get_expiration_datetime(expire_after: ExpirationTime) -> Optional[datetime]:
return datetime.utcnow() + expire_after


def get_expiration_seconds(expire_after: ExpirationTime) -> Optional[int]:
def get_expiration_seconds(expire_after: ExpirationTime) -> int:
"""Convert an expiration value in any supported format to an expiration time in seconds"""
expires = get_expiration_datetime(expire_after)
return ceil((expires - datetime.utcnow()).total_seconds()) if expires else None
return ceil((expires - datetime.utcnow()).total_seconds()) if expires else NEVER_EXPIRE


def get_cache_directives(headers: Mapping) -> Dict:
Expand Down Expand Up @@ -242,7 +243,10 @@ def to_utc(dt: datetime):

def try_int(value: Any) -> Optional[int]:
"""Convert a value to an int, if possible, otherwise ``None``"""
return int(str(value)) if str(value).isnumeric() else None
try:
return int(value)
except (TypeError, ValueError):
return None


def url_match(url: str, pattern: str) -> bool:
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/test_session.py
Expand Up @@ -718,8 +718,8 @@ def test_remove_expired_responses__per_request(mock_session):
assert len(mock_session.cache.responses) == 1


def test_per_request__expiration(mock_session):
"""No per-session expiration is set, but then overridden with per-request expiration"""
def test_per_request__enable_expiration(mock_session):
"""No per-session expiration is set, but then overridden for a single request"""
mock_session.expire_after = None
response = mock_session.get(MOCKED_URL, expire_after=1)
assert response.from_cache is False
Expand All @@ -730,6 +730,15 @@ def test_per_request__expiration(mock_session):
assert response.from_cache is False


def test_per_request__disable_expiration(mock_session):
"""A per-session expiration is set, but then disabled for a single request"""
mock_session.expire_after = 60
response = mock_session.get(MOCKED_URL, expire_after=-1)
response = mock_session.get(MOCKED_URL, expire_after=-1)
assert response.from_cache is True
assert response.expires is None


def test_per_request__prepared_request(mock_session):
"""The same should work for PreparedRequests with CachedSession.send()"""
mock_session.expire_after = None
Expand Down

0 comments on commit 261bda4

Please sign in to comment.