Skip to content

Commit 79723a4

Browse files
committed
webrequests: Compress cache files
1 parent aa1199f commit 79723a4

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

swattool/webrequests.py

+36-14
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
"""Wrapper for requests module with cookies persistence and basic cache."""
44

5+
import gzip
56
import hashlib
67
import logging
78
import pathlib
89
import pickle
910
import time
10-
from typing import Any
11+
from typing import Any, Optional
1112

1213
import requests
1314

@@ -61,6 +62,7 @@ def _get_cache_file_candidates(self, url: str) -> list[pathlib.Path]:
6162
filestem = hashname.hexdigest()
6263

6364
candidates = [
65+
utils.CACHEDIR / f"{filestem}.gz",
6466
utils.CACHEDIR / filestem,
6567

6668
# For compatibility with old cache files
@@ -69,6 +71,32 @@ def _get_cache_file_candidates(self, url: str) -> list[pathlib.Path]:
6971

7072
return candidates
7173

74+
def _try_load_cache(self, cachefile: pathlib.Path, max_cache_age: int
75+
) -> Optional[str]:
76+
if max_cache_age < 0:
77+
use_cache = True
78+
else:
79+
age = time.time() - cachefile.stat().st_mtime
80+
use_cache = age < max_cache_age
81+
82+
if use_cache:
83+
if cachefile.suffix == ".gz":
84+
with gzip.open(cachefile, mode='r') as gzfile:
85+
return gzfile.read(-1).decode()
86+
else:
87+
with cachefile.open('r') as file:
88+
return file.read(-1)
89+
90+
return None
91+
92+
def _create_cache_file(self, cachefile: pathlib.Path, data: str):
93+
if cachefile.suffix == ".gz":
94+
with gzip.open(cachefile, mode='w') as gzfile:
95+
gzfile.write(data.encode())
96+
else:
97+
with cachefile.open('w') as file:
98+
file.write(data)
99+
72100
def get(self, url: str, max_cache_age: int = -1) -> str:
73101
"""Do a GET request."""
74102
cache_candidates = self._get_cache_file_candidates(url)
@@ -77,24 +105,18 @@ def get(self, url: str, max_cache_age: int = -1) -> str:
77105

78106
cache_olds = [file for file in cache_candidates if file.exists()]
79107
for cachefile in cache_olds:
80-
if max_cache_age < 0:
81-
use_cache = True
82-
else:
83-
age = time.time() - cachefile.stat().st_mtime
84-
use_cache = age < max_cache_age
108+
data = self._try_load_cache(cachefile, max_cache_age)
109+
if data:
110+
logger.debug("Loaded cache file for %s: %s", url, cachefile)
111+
return data
85112

86-
if use_cache:
87-
logger.debug("Loading cache file for %s: %s", url, cachefile)
88-
with cachefile.open('r') as file:
89-
return file.read(-1)
90-
else:
91-
cachefile.unlink()
113+
cachefile.unlink()
92114

93115
logger.debug("Fetching %s, cache file will be %s", url, cache_new_file)
94116
req = self.session.get(url)
95117
req.raise_for_status()
96-
with cache_new_file.open('w') as file:
97-
file.write(req.text)
118+
119+
self._create_cache_file(cache_new_file, req.text)
98120

99121
return req.text
100122

0 commit comments

Comments
 (0)